【年少の风】1/1を求めます!-1/3!+1/5!-1/7!……+(-1)^(n+1)/(2*n-1))
- import java.util.*;
- public class Test {
-
- public static void main(String[] args) {
- float sum = 1;
- int temp = 1;
- int n = 1;
- float flag = -1;
-
- System.out.print(" n :");
- Scanner sc = new Scanner(System.in);
- n = sc.nextInt();
-
- for(int i = 2; i <= n; i++){
- temp *= (2*i-1)*(2*i-2);
- flag *= -1;
- sum += flag/temp;
- }
- System.out.println("sum = " + sum);
- }
- }