hdu 1141次方转log

来源:互联网 发布:社会工程学知乎手机号 编辑:程序博客网 时间:2024/06/06 02:26

题目:

Amtel has announced that it will release a 128-bit computer chip by 2010, a 256-bit computer by 2020, and so on, continuing its strategy of doubling the word-size every ten years. (Amtel released a 64-bit computer in 2000, a 32-bit computer in 1990, a 16-bit computer in 1980, an 8-bit computer in 1970, and a 4-bit computer, its first, in 1960.)
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开始是4位的芯片,以后每十年翻2倍,现在输入年份,找到最大的n,使得n!<=芯片内存。

如1960年,4!>2的4次,3!<2的4次,所以用3表示。

思路:因为到了2160年,它的位数已经是4*2的20次,用普通的数据肯定会超.

当前时间为y,芯片的位数为s=4<<(y-1960)/10;每左移1位,等于翻倍。

i!<2^(s),取对数,log1+log2+log...i<s*log2,求最大的i;对数函数log,以e(2.71828)为底,之前一直以为以2为底。。错了

#include<iostream>#include<cmath>using namespace std;int main(){    int y;    while (cin >> y&&y != 0)    {        y = (y - 1960) / 10;    int s = 4;    s <<= y;        double q = 0;        int i = 1;        while (q < s)        {            q += log(++i)/log(2);//对数函数log,以e(2.71828)为底        }        cout << i - 1 << endl;    }    return 0;}



原创粉丝点击