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 integer n 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
在该题中 将x-bit (2进制的位数)转化为一个整数时 会出现超时。 可以用log(2) 来表示这个数,同样将n!转化为 log(1)+log(2)+log(3)......log(n) 来比较得出结果
<多个数相乘时,如果超出范围,可以使用log相加来表示>
#include <stdio.h> #include <math.h> int main() { int y , i , j ; double sum1 , sum2 ; while(scanf("%d", &y) && y) { sum1 = 4 ; sum2 = 0 ; y = (y-1960)/10; for(i = 1 ; i <= y ; i++) sum1 *= 2 ; for(i = 1 ; i <= sum1 ; i++) sum2 += log(2); sum1 = sum2 ; sum2 = 0 ; for(j = 1 ; ; j++) { sum2 += log(j) ; if(sum2 >= sum1) break; } printf("%d\n", j-1); } }