2039 Problem K 迭代求立方根

389 篇文章 1 订阅
144 篇文章 2 订阅

问题 K: 迭代求立方根
时间限制: 1 Sec 内存限制: 32 MB
献花: 32 解决: 26
[献花][花圈][TK题库]
题目描述

立方根的逼近迭代方程是 y(n+1) = y(n)*2/3 + x/(3*y(n)*y(n)),其中y0=x.求给定的x经过n次迭代后立方根的值。

输入

输入有多组数据。
每组一行,输入x n。

输出

迭代n次后的立方根,double精度,保留小数点后面六位。

样例输入
4654684 1
65461 23
样例输出
3103122.666667
40.302088

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <ctime>
using namespace std;



int main()
{
#ifdef _DEBUG
    //freopen("data.txt", "r+", stdin);
    fstream cin("data.txt");
#endif // _DEBUG
    const int MaxN = 100010;
    double x, y[MaxN];
    int n;

    while (cin >> x >> n)
    {
        y[0] = x;
        for (int i = 1; i <= n; ++i)
            y[i] = y[i - 1] * 2 / 3 + x / (3 * y[i - 1] * y[i - 1]);
        printf("%.6f\n", y[n]);
    }

#ifdef _DEBUG
    cin.close();
#ifndef _CODEBLOCKS
    system("pause");
#endif // !_CODEBLOCKS
#endif // _DEBUG

    return 0;
}
/**************************************************************
    Problem: 2039
    User: Sharwen
    Language: C++
    Result: 升仙
    Time:2 ms
    Memory:2368 kb
****************************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值