码迷,mamicode.com
首页 > 其他好文 > 详细

(eden)性能评估

时间:2015-12-16 10:50:52      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

题目描述: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?

输出:For test, there is one line of input containing y.  For each test case, output a line giving the Factstone rating.

示例

输入: 1960

输出: 3

(解释:因为1960年计算机是支持4位,其中的最大的无符号数为(全为1):15;3的阶乘为6,4的阶乘为24,故这里的n=3)

Hint:

1.取对数避免大数计算,log(n*m) = log(n) + log(m)。

2.准确理解题意。

思路

y = y / 10 - 194;

求得2^y这个指数,得到该年应该得到多少位的机,然后2^(2^y) - 1即该年的最大无符号整数,但由于直接这样计算得到的数为inf无穷大,即计算机装不下,所以要经过处理,本来是得到2^(2^y) - 1即该年的最大无符号整数这个之后,判断它在哪两个阶乘范围之内n!~(n+1)!,然而这样计算又还要写阶乘函数,又有判断循环,耗时,所以采用

p! < 2^2^y
两边取对数
log(1)+log(2)+...+log(p) < (2^y)*log(2)
即采用whlie循环一直加直到加到某一个p0超过了后面则出循环,p0 - 1即为所求p

因为如果直接用(log(2^(2^y) - 1))会超过int float double类型的范围,所以只能够这样将减一忽略不计,因为并不会影响,从而得到只需要计算2^y即可;

代码

 1 #include<stdio.h>
 2 #include<math.h>
 3 int main() {
 4     int y, i;
 5     scanf("%d", &y);
 6     y = y / 10 - 194;
 7     double k = pow(2, y);
 8     i = 1;
 9     double tot = log(1);
10     while (tot < k * log(2)) {
11         i++;
12         tot = tot + log(i);
13     }
14     printf("%d\n", i - 1);
15 }

标答

 1 // from younglee.
 2 #include<stdio.h>
 3 #include<math.h>
 4 int main(void) {
 5     int year, num, n = 2;
 6     double sum = 0;
 7     // for input
 8     scanf("%d", &year);
 9  
10     // for processing.
11     num = pow(2, (year - 1960)/10 + 2);//也是求Index
12     while (sum <= num) {
13         sum += log(n) / log(2);//把log(2)移到另一边
14         ++n;
15     }
16  
17     // for output.
18     printf("%d\n", n - 2);//因为最后还加了1
19     return 0;
20 }

 

问题

①方法上面可以取对数以代替阶乘判断;

②要注意k是浮点数tot也是,不能够直接技术分享这样,k还是会很大的,装不下,所以要把减一舍去取对数,从而简化,

否则会出现技术分享这个情况;

需要学习

①思路,简化的方法;

②inf,nan。

 

(eden)性能评估

标签:

原文地址:http://www.cnblogs.com/iamxiaoyubei/p/5050355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!