练习2-13 求N分之一序列前N项和 (15 分)

练习2-13 求N分之一序列前N项和 (15 分)

输入在一行中给出一个正整数N。

输出格式:

在一行中按照“sum = S”的格式输出部分和的值S,精确到小数点后6位。题目保证计算结果不超过双精度范围。

输入样例:

6

输出样例:

sum = 2.450000


 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 
 5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 6 
 7 int main(int argc, char *argv[]) {
 8 int x;
 9 double s=0,y;
10 scanf("%d",&x);
11 
12 for(y=1;y<=x;y++){
13 s+=1.0/y;
14 }
15 printf("sum = %.6f
",s);
16 return 0;
17 }
 
 
原文地址:https://www.cnblogs.com/xxl-h/p/11111022.html