Floating-Point Hazard
题目描述
Given the value of low, high you will have to find the value of the following expression:
If you try to find the value of the above expression in a straightforward way, the answer may be incorrect due to precision error.
输入
The input file contains at most 2000 lines of inputs. Each line contains two integers which denote the value of low, high (1 ≤ low ≤ high ≤ 2000000000 and high-low ≤ 10000). Input is terminated by a line containing two zeroes. This line should not be processed.
输出
For each line of input produce one line of output. This line should contain the value of the expression above in exponential format. The mantissa part should have one digit before the decimal point and be rounded to five digits after the decimal point. To be more specific the output should be of the form d.dddddE-ddd, here d means a decimal digit and E means power of 10. Look at the output for sample input for details. Your output should follow the same pattern as shown below.
样例输入
1 100
10000 20000
0 0
样例输出
3.83346E-015
5.60041E-015
题意:
给出上述公式 输入n和m分别对应low和high 求出值 并按照 d.dddddE-ddd 格式输出
思路:
令 f(x) = x ^ (1/3)
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
double sum;
while(scanf("%d%d",&n,&m)!=EOF){
if(n==0&&m==0){
break;
}
sum=0;
for(int i=n;i<=m;i++){
sum+=(1.0/3.0)*pow(1.0*i,-(2.0/3.0));
}
int ans=15;
while(sum>=10.0){ ///有可能sum的值大于10 如果直接输出 结果不是最简 所以要把后面的幂数
sum/=10.0; ///变化 使其最简
ans--;
}
printf("%.5lfE",sum); ///下面是控制输出格式 没找到好办法 只能这样复杂点了
if(ans>99){
printf("-%d
",ans);
}
else if(ans>9&&ans<=99){
printf("-0%d
",ans);
}
else{
printf("-00%d
",ans);
}
}
return 0;
}