HDU-2011

多项式求和

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 66759    Accepted Submission(s): 38429


Problem Description

多项式的描述如下:
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ...
现在请你求出该多项式的前n项的和。

 


 

Input

输入数据由2行组成,首先是一个正整数m(m<100),表示测试实例的个数,第二行包含m个正整数,对于每一个整数(不妨设为n,n<1000),求该多项式的前n项的和。

 


 

Output

对于每个测试实例n,要求输出多项式前n项的和。每个测试实例的输出占一行,结果保留2位小数。

 


 

Sample Input

2
1 2

 


 

Sample Output

1.00
0.50

 


 

Author

lcy

 


 

Source

C语言程序设计练习(二)

 


 

Recommend

JGShining   |   We have carefully selected several similar problems for you:  2000 1008 2081 2510 2509 

 

 

 

源代码:

import java.util.Scanner;

public class Main {

 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  int n = sc.nextInt();
  sc.nextLine();
  while (n-- > 0) {
   int m = sc.nextInt();
   double sum = 0;
   for (int i = 2; i <= m; i++) {
    double temp = 1.0 / i;
    if (i % 2 == 0) {
     temp = -temp;
    }
    sum += temp;
   }
   System.out.printf("%.2f", sum+1);
   System.out.println();
  }
 }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值