1 /* 2 * Main.c 3 * C21-循环-21. 求交错序列前N项和 4 * Created on: 2014年8月18日 5 * Author: Boomkeeper 6 ***********测试通过******** 7 */ 8 9 #include <stdio.h> 10 11 int main(void){ 12 13 float numerator = 1; 14 float denominator = 1; 15 16 int N = 0;//前N项和 17 float sum = 1; 18 19 scanf("%i",&N); 20 21 int i,flag = 1; 22 for(i = 0;i < N-1;i++){ 23 flag = flag*(-1); 24 numerator++; 25 denominator += 2; 26 sum += flag*(numerator/denominator); 27 } 28 29 printf("%.3f\n",sum); 30 return 0; 31 }
题目链接:
http://pat.zju.edu.cn/contests/basic-programming/%E5%BE%AA%E7%8E%AF-21
.
循环-21. 求交错序列前N项和,布布扣,bubuko.com
时间: 2024-12-25 10:26:15