2504 多项式求和

刺骨的言语ヽ痛彻心扉 2022-05-26 04:09 262阅读 0赞

多项式求和

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

多项式描述如下:

1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 ……

先请你求出多项式前n项的和。

Input

第一行输入一个数T代表测试数据个数(T<=1000)。接下来T行每行1个数代表n(0<=n< 2^31)。

Output

对于每个输入样例,输出多项式和的结果(结果精确到小数点后两位)。每行输出一个结果。

Sample Input

  1. 2
  2. 1
  3. 2

Sample Output

  1. 1.00
  2. 0.50
  3. import java.util.Scanner;
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner cin = new Scanner(System.in);
  7. int n = cin.nextInt();
  8. while(n!=0) {
  9. n--;
  10. int x = cin.nextInt();
  11. double sum =0,ha;
  12. if(x>500) {x=500;}
  13. for(int i=0;i<x-1;i+=2) {
  14. ha = (i+1)*(i+2);
  15. sum+= 1.0/ha;
  16. }
  17. if(x%2!=0) {
  18. ha =x;
  19. sum+= 1.0/ha;
  20. }
  21. System.out.printf("%.2f\n",sum);
  22. }
  23. cin.close();
  24. }
  25. }

发表评论

表情:
评论列表 (有 0 条评论,262人围观)

还没有评论,来说两句吧...

相关阅读