本题要求编写程序,计算平方根序列1+2+3+⋯的前N项之和。可包含头文件math.h
,并调用sqrt
函数求平方根。
输入格式:
输入在一行中给出一个正整数N。
输出格式:
在一行中按照“sum = S”的格式输出部分和的值S,精确到小数点后两位。题目保证计算结果不超过双精度范围。
输入样例:
10
输出样例:
sum = 22.47
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[])
{
int n;
scanf("%d",&n); //不要管忘记 &
int i;
double s=0;
for(i=1;i<=n;i++){
s+=sqrt(i);
}
printf("sum = %.2f",s); //不要忘记 \n
return 0;
}