(数学题)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

题目给出上下边界,low 和 high 求该公式的值。
比赛时和亨亨一直以为是卡精度的题,一直在调精度,最后也没能调出来结果,更别说输出格式的英语还看不太懂。比赛结束后查了一下,竟是利用导数的定义,直接求出原函数得出结果。。。:
这个是导数的定义公式:

在这里插入图片描述
题目中给的公式就可以化为:
在这里插入图片描述
所以原函数就是3次根号下 i:所以要求的就是3次根号下 i 的导数。即为1/3*i的负的2/3次方。(不要忘了分母的10的-15次方,需要乘到等号的左面,否则格式会错误)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn=1e4+7;
const int INF=1e9+7;
int main()
{
    ll n,m;
    while(~scanf("%lld%lld",&n,&m))
    {
        ll num=0;
        if(n==0&&m==0)
            return 0;
        double ans=0;
        for(ll i=n;i<=m;i++)
            ans+=pow(i,-1*2.0/3.0);
        ans/=3;                         //求导为1/3*f(x)的2/3次方,这里乘上1/3
        ll flag=0;
        if(ans>10)			
        {
            while(ans>10)			//如果结果大于10,变为保留等号前1为小数
            {
                ans/=10;
                num++;
                flag=1;
            }
        }
        else
        {
            while(ans<1)
            {
                ans*=10;
                num++;
                flag=2;
            }
        }
        ll t=15;
        if(flag==1)
            t-=num;
        else
            t+=num;
        printf("%.5lfE-0%lld\n",ans,t);					//自带的有10 的-15次方
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值