求值:1!/n+3!/n*n*n+5!/n*n*n*n*n+....k!/n*n*n*n....n*n(有k個n);
阿新 • • 發佈:2019-02-18
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("請輸入k的值(奇數):"); int k = input.nextInt(); if (k % 2 == 1) { System.out.println("請輸入n的值(整數):"); int n = input.nextInt(); System.out.println("表示式的值為" + getSum(k, n)); } else { System.out.println("您輸入的不是奇數,請重新執行"); } } public static double getSum(int k, int n) { double sum = 0.0; int num = (k + 1) / 2; double[] a = new double[num]; double[] b = new double[num]; for (int i = 0; i < num; i++) { if (i == 0) { a[i] = 1; b[i] = n; } else { a[i] = a[i - 1] * (2 * i) * (2 * i + 1); b[i] = b[i - 1] * n * n; } sum += a[i] / b[i]; } return sum; } }