Skip to content

Instantly share code, notes, and snippets.

@valyagolev
Created April 1, 2010 10:34
Show Gist options
  • Save valyagolev/351639 to your computer and use it in GitHub Desktop.
Save valyagolev/351639 to your computer and use it in GitHub Desktop.
// pe205.cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
int* add_one(int* brs, int max, int count) {
for (int i = count-1; i >= 0; i--) {
if (brs[i] < max) {
brs[i]++;
for (int j = i+1; j < count; j++) {
brs[j] = 1;
}
return brs;
}
}
return brs;
}
int sum(int* brs, int count) {
int res = 0;
for (int i = 0; i < count; i++) {
res += brs[i];
}
return res;
}
unsigned long dice_sums(int** res, int max, int count)
{
unsigned long length = pow((double)max, count);
int* result = new int[length];
int* last_br = new int[count];
for (int i = 0; i < count; i++) {
last_br[i] = 1;
}
printf("Computing %u sums... ", length);
for (int i = 0; i < length; i++) {
result[i] = sum(last_br, count);
add_one(last_br, max, count);
}
printf("Ready.\n");
*res = result;
delete last_br;
return length;
}
int _tmain(int argc, _TCHAR* argv[])
{
int* first = NULL;
int* second = NULL;
unsigned long long fc = dice_sums(&first, 4, 9);
unsigned long long sc = dice_sums(&second, 6, 6);
unsigned long long total = 0;
unsigned long long fwon = 0;
int* cache = new int[40];
memset(cache, 0, sizeof(int) * 40);
printf("Computing %I64d * %I64d = %I64d games...\n", fc, sc, fc * sc);
getchar();
for (int i = 0; i < fc; i++) {
if (cache[first[i]] == 0)
for (int j = 0; j < sc; j++) {
if (first[i] > second[j])
cache[first[i]]++;
}
fwon += cache[first[i]];
total += sc;
if (i % 1000 == 0) printf(" i = %i\n", i);
}
printf("\nTotal games: %I64d. First won %I64d times. Probability = %0.7f", total, fwon, fwon / total);
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment