acm学习总结

最近做了些二分的题目总结下二分吧 突然发现零点定理也很适合二分。二分需要注意的便是精度和退出的条件有时候可以把二分的次数设为定值然后用一个变量来保存等循环结束即得到了最优答案
零点定理型

Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y,can you find its solution between 0 and 100;
Now please try your lucky.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has a real number Y (fabs(Y) <= 1e10);
Output
For each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100.
Sample Input
2
100
-4
Sample Output
1.6152
No solution!

这个函数在给定的定义域内是单调的所以上来变通过两端点判定是否有零点否则就不断地换区间就行了。

#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <algorithm>
using namespace std;
double go(int x)
{
    double c,d,e,l=0,r=100,mid;
    while(1)
    {
        mid=(l+r)/2;
        c=8*pow(l,4)+7*pow(l,3)+2*pow(l,2)+3*l+6-x;
        d=8*pow(r,4)+7*pow(r,3)+2*pow(r,2)+3*r+6-x;
        e=8*pow(mid,4)+7*pow(mid,3)+2*pow(mid,2)+3*mid+6-x;
        if(c*d>0)
            return -1;
        if(fabs(e)<1e-4)
            return mid;
        if(c*e<0)
            r=mid;
        if(d*e<0)
            l=mid;
    }

}
int main()
{
    int n,m;
    cin>>n;
    while(n--)
    {
        cin>>m;
        if(go(m)==-1)
            cout<<"No solution!"<<endl;
        else
        {
            printf("%.4f\n",go(m));
        }
    }
}

Now, here is a fuction:
F(x) = 6 * x7+8*x6+7x3+5*x2-yx (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)
Output
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
Sample Input
2
100
200
Sample Output
-74.4291
-178.8534

这个题和上面的体一样先求导倒数的零点就是最小值(倒数是增的零点左负右正极值就是最小值)革命尚未成功同志仍需努力

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值