zoukankan      html  css  js  c++  java
  • (eden)性能评估

    题目描述: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
    ", 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 - 2);//因为最后还加了1
    19     return 0;
    20 }

    问题

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

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

    否则会出现这个情况;

    需要学习

    ①思路,简化的方法;

    ②inf,nan。

  • 相关阅读:
    微信开发(5):公众号消息与事件推送处理(转)
    微信开发(4):微信第三方开放平台的搭建(转)
    微信开发(3):微信公众号发现金红包功能开发,利用第三方SDK实现(转)
    微信开发(2):微信js sdk分享朋友圈,朋友,获取config接口注入权限验证(转)
    微信开发(1) :网页授权获取用户的基本信息 实现微信登录(转)
    java实现创建临时文件然后在程序退出时自动删除文件(转)
    Linux定时对日志批量打包Shell脚本及定时任务crontab 详细用法
    httpd2.4出现AH00025: configuration error
    apache启动失败
    软件工程中的反面模式(anti-pattern)
  • 原文地址:https://www.cnblogs.com/iamxiaoyubei/p/5050355.html
Copyright © 2011-2022 走看看