循环-10. 求序列前N项和*

 1 /*
 2  * Main.c
 3  * C10-循环-10. 求序列前N项和
 4  *  Created on: 2014年7月30日
 5  *      Author: Boomkeeper
 6  *******部分通过*******
 7  */
 8 
 9 #include <stdio.h>
10 
11 int main(void) {
12 
13     double sum = 0.0; //记录前N项和
14     int numerator = 2,denominator = 1; //分子分母
15     int N; //题目中的N
16     int i;//循环使用
17     int tem = 0;
18 
19     scanf("%d", &N);
20 
21     if (N == 1)
22         printf("%.2f\n", (double) numerator / denominator);
23     else {
24         sum = (double) numerator / denominator;
25         for (i = N; i > 1; i--) {
26             tem = denominator;
27             denominator = numerator;
28             numerator = numerator + tem;
29             sum += (double) numerator / denominator;
30         }
31         printf("%.2f\n", sum);
32     }
33 
34     return 0;
35 }

不完美...

 

题目链接:

http://pat.zju.edu.cn/contests/basic-programming/%E5%BE%AA%E7%8E%AF-10

posted @ 2014-07-30 18:28  Boomkeeper  Views(628)  Comments(2Edit  收藏  举报
最好的流量统计 网站流量计数器