Amtel will use a new benchmark - the Factstone - to advertise the vastly improved capacity of its new chips. The Factstone rating is defined to be the largest integern such that n! can be represented as an unsigned integer in a computer word.
Given a year 1960 ≤ y ≤ 2160, what will be the Factstone rating of Amtel's most recently released chip?
There are several test cases. For each test case, there is one line of input containing y. A line containing 0 follows the last test case. For each test case, output a line giving the Factstone rating.
1960 1981 0
3 8
Charles L. A. Clarke and Gordon V. Cormack
这道题大致就是求出最大的n,条件是n!在2^N的氛围内。告诉你N。
这道题肯定是会超出int类型的。不过N的值是确定的十几二十个吧,所以可以一开始的时候用打表的方式打出所有的值。
这里打表是有技巧的,每次乘2之后,都要看看能不能再除以n+1,这样数据范围始终不会超了
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<string> #include<ctype.h> #include<algorithm> #include<queue> #include<stack> using namespace std; int a[25]={0}; int n,t=0; double shang; int go(int x) { int i; for (i=x+1; i<=2*x; i++) { shang*=2; if (shang/(n+1)>=1) { shang/=(n+1); n=n+1; } } a[t]=n; t++; if (t>22) return 0; go(2*x); } int main () { int y,i; shang=4*1.0/6.0; n=3; go(2); while(cin>>y) { if (y==0) break; cout<<a[(y-1960)/10]<<endl; } return 0; }