解法:递归
import java.util.Scanner;
public class Main {
public double sum(int n) {
double s=0;
if(n==1) {
return 1;
}
else if(n%2==0) {
s=s-(1.000000/n);
return sum(n-1)-(1.0/n);
}else {
s=s+(1.0000000/n);
return 1.0/n+sum(n-1);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Main a=new Main();
Scanner scanner =new Scanner(System.in);
while(scanner.hasNext()) {
int x=0;
x=scanner.nextInt();
java.text.DecimalFormat df =new java.text.DecimalFormat("0.000000");
System.out.println( df.format(a.sum(x)));
}
}
}