hdu-求数列的和

来源:互联网 发布:淘宝网站是怎么赚钱的 编辑:程序博客网 时间:2024/06/05 21:12

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


Problem Description
数列的定义如下:
数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和。
 

Input
输入数据有多组,每组占一行,由两个整数n(n<10000)和m(m<1000)组成,n和m的含义如前所述。
 

Output
对于每组输入数据,输出该数列的和,每个测试实例占一行,要求精度保留2位小数。
 

Sample Input
81 42 2
 

Sample Output
94.733.41

分析:

#include <iostream>#include <iomanip>#include <cmath>using namespace std;int main(){   double m;   int n;   while(cin>>m>>n)   {       double y=m;      for(int i=1;i<n;i++)      {          m=sqrt(m);          y+=m;      }      cout<<fixed<<setprecision(2)<<y<<endl;   }   return 0;}