HDU 2899 牛顿迭代

来源:互联网 发布:软件下载免费 编辑:程序博客网 时间:2024/04/30 15:18

HDU 2899

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2899

题意:

给一个方程,方程中未知量x可以取遍0-100

求方程值得最小值。

思路:

对原方程求个导数,牛顿迭代求出导数为0的地方,导数为0的地方再算一遍f值即可。

源码:

#include <cstdio>

#include <cstring>

#include <cmath>

#include <cstdlib>

#include <algorithm>

#include <iostream>

using namespace std;

#define LL long long

const double eps = 1e-6;

//6 * x^7+8*x^6+7*x^3+5*x^2-y*x

LL y;

double Newton_iterative(double x)

{

    int cnt = 0;

    double x0 = x;

    while(cnt < 50){

        double f1 = ((((42 * x + 48) * x * x * x + 21) * x + 10) * x - y);

        double f0 = (((252 * x + 240) * x * x * x + 42) * x + 10);

        cnt++;

        x -= f1 / f0;

    }

    return ((((6 * x + 8) * x * x * x + 7) * x + 5 ) * x - y ) * x;

}

int main()

{

    int t;

    scanf("%d", &t);

    while(t--){

        double x = 1000000000;

        scanf("%I64d", &y);

        for(double u = 0.0 ; u <= 100 ; u++){

            x = min(x, Newton_iterative(u));

        }

        printf("%.4f\n", x);

    }

    return 0;

}

 

0 0
原创粉丝点击