新的linux系统下写的第一个C++程序。(哎compile error了两次,log2(x)化成logx/log2,logx是以自然对数为底的)

​http://poj.org/problem?id=2661​

转化题意:求解最大的n, 满足

POJ 2661 Factstone Benchmark (log2的故事)_浮点数

转化:

POJ 2661 Factstone Benchmark (log2的故事)_浮点数_02

开始不敢用浮点数,直接走向了素因子的错误道路。。。其实有时浮点运算还是可以信赖的。

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const double eps=1e-7;
int main()
{
int y;
while(cin>>y&&y){ //log2(1) =0 但是 1占了一个字节,
int len=1<<((y-1960)/10+2); //2^4 5字节
double sum=0;
int i;
for(i=1;sum-len<=eps;i++){ //当跳出循环时 又多了一位
sum+=log(double(i))/log(2.0);
}
printf("%d\n",i-2);
}
return 0;
}