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