#杭电oj 2011 java
多重式求和
多个式的描述如下:
1-1/2 + 1/ 3-1 /4 + 1/ 5-1 /6 + ...
现在请你求出该多个式的前n项的和。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()) {
int first=sc.nextInt();//排头 表示 数字个数
int[] arr=new int[first];
//m个数字分别读入数组
for(int i=0;i<arr.length;i++) {
arr[i]=sc.nextInt();
}
//m个数字分别计算多重式之和
for(int i=0;i<arr.length;i++) {
double count=0;//结果
for(int j=1;j<=arr[i];j++) {
double t=(1.0/j);//临时保存1.0/j
if(j%2==0) {
t=(-1)*t;
}
count+=t;
}
System.out.println(String.format("%.2f", count));
}
}
}
}