Problem Description
数列的定义如下:
数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和。
Input
输入数据有多组,每组占一行,由两个整数你n(n<10000)和m(m<10000)组成,n和m的含义如前所述。
Output
对于每组输入数据,输出该数列的和,每个测试实例占一行,要求精度保留两位小数。
Sample Input
81 4
2 2
Sample Output
94.73
3.41
代码如下:
#include<stdio.h>
#include<math.h>
int main() {
int n, m;
double n1, total = 0;
while(scanf("%d %d", &n, &m) != EOF) {
total = 0;
n1 = n * 1.0;
total += n1;
for(int i = 0; i < m - 1; i++) {
n1 = sqrt(n1); //sqrt()函数的参数和返回值均是double型。注意n到n1的格式转化
total += n1;
}
printf("%.2f\n", total);
}
return 0;
}