PE53

/* There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 235, 245, and 345 In combinatorics, we use the notation, 5C3 = 10. In general, nCr = n! r!(nr)! ,where r n, n! = n(n1)...321, and 0! = 1. It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066. How many, not necessarily distinct, values of nCr, for 1 n 100, are greater than one-million? 12345から3つ選ぶ選び方は10通りである. 123, 124, 125, 134, 135, 145, 234, 235, 245, 345. 組み合わせでは, 以下の記法を用いてこのことを表す: 5C3 = 10. 一般に, r ≤ n についてnCr = n!/(r!(n-r)!) である. ここで, n! = n×(n−1)×...×3×2×1, 0! = 1と階乗を定義する. n = 23になるまで, これらの値が100万を超えることはない: 23C10 = 1144066. 1 ≤ n ≤ 100について, 100万を超えるnCrは何通りか? */ #include <Windows.h> #include <iostream> using namespace std; bool Problem53(int n, int r) { unsigned long long tot = 1; int up = r+1; int down = 1; while (up!=1 || down!=1) { tot = tot * up / down; up++; down++; if (up>n) up=1; if (down>n-r) down=1; } if (tot>1000000) return true; return false; } int main() { DWORD start = GetTickCount(); int counter = 0; for (int n=1; n<=100; n++) for (int r=1; r<=n; r++) if (Problem53(n,r)) counter++; cout << counter << endl; DWORD end = GetTickCount(); std::cout << "the running time = " << end - start << " ms." << '/n'; return 0; } 

你可能感兴趣的:(c,UP)