【水】HDU 2009求数列的和

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 65536/32768 K (Java/Others)

Description
数列的定义如下:
数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和。
Input
输入数据有多组,每组占一行,由两个整数n(n<10000)和m(m<1000)组成,n和m的含义如前所述。
Output
对于每组输入数据,输出该数列的和,每个测试实例占一行,要求精度保留2位小数。
Sample Input

81 4
2 2

Sample Output

94.73
3.41

Hint
JGShining
Source
  C语言程序设计练习(二)  
Related problem
2010 2000 2005 2004 2012

求平方根可以使用<cmath>里面的sqrt函数求,用点循环知识就可以了。还有是输出格式问题注意下。

代码如下:

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main ()
{
double n,m;
while(cin>>n>>m)
{
double sum=n;
for(int i=1;i<m;i++)
{
    sum+=sqrt(n);
    n=sqrt(n);
}
cout<<fixed<<setprecision(2)<<sum<<endl;
}
}

猜你喜欢

转载自blog.csdn.net/qq_41627235/article/details/82789271