问题 J: 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. 

思路: 根据求导定义。设f(i)=i^(1/3),     则f(i+10^-15)-f(i)/10^-15=f'(i); 

# include <iostream>
# include <cstdio>
#include<cmath>
# define ll long long
#define num 1e9
using namespace std;
int main(){
    ll lo,hi;
    double ans;
    while(scanf("%lld%lld",&lo,&hi),hi,lo){
        ans=0;
        for(ll i=lo;i<=hi;i++){
            ans+=pow(i,-1*2.0/3.0);
        }
        ans/=3;
        ll cnt=15;
        
        if (ans-1.0<=0.00000001) {
            while(ans-1.0<=0.00000001){
                ans*=10;
                cnt++;
            }
        }
        else{
            while(ans-10.0>=0.00000001){
                ans/=10;
                cnt--;
            }
        }
        printf("%.5lfE-%03lld\n",ans,cnt);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiao_you_you/article/details/89681866