题解 | #牛牛学数列2#
牛牛学数列3
http://www.nowcoder.com/practice/f65c726d081c4160a9356eabf0dc21d9
import java.util.*;
public class Main{
public static void main(String[] args)throws Exception{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.printf("%.3f",sum(n));
}
public static double sum(int n){
double sum = 0;
double sum_total = 0;
int c = -1;
for(int i = 1; i <= n; i++){
c *= -1;
sum += (2 * i - 1) * c;
sum_total += 1.0 / sum;
}
return sum_total;
}
}