1128: 零起点学算法35——再求多项式(含浮点)

1128: 零起点学算法35——再求多项式(含浮点)

Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lld
Submitted: 2141  Accepted: 1002
[Submit][Status][Web Board]

Description

输入一个整数n,计算 
1+1/(1-3)+1/(1-3+5)+...+1/(1-3+5-...+2n-1)的值

Input

输入一个整数n(多组数据)

Output

出1+1/(1-3)+1/(1-3+5)+...+1/(1-3+5-...+2n-1)的值,保留2位小数(每组数据一行)

Sample Input

 
1

Sample Output

1.00

Source

 
 1 #include<stdio.h>
 2 int main(){
 3     int n;
 4     while(scanf("%d",&n)!=EOF){
 5          double num=0;
 6          
 7          for(int i=1;i<=n;i++){
 8              int flag=1,s=0;
 9               for(int j=1;j<=i;j++){
10                if(flag){
11                   s+=(2*j-1);flag=0;
12                }
13                 else{
14                   s-=(2*j-1);flag=1;
15                 }
16              }
17              num+=(1.0/s);
18          }
19          printf("%.2f
",num); 
20     }
21     return 0;
22 }

// 仔细思考, 两重循环, 尝试分解循环进行计算。 部分输出也是检验答案的好方法。

原文地址:https://www.cnblogs.com/dddddd/p/6680288.html