用巢狀迴圈,計算1!+2!+...+n! 的總和用巢狀迴圈(2個for)計算1!+2!+...+n! 的總和計算 1/1!+1/2!+1/3!+...+1/n! 如果沒記錯的話 n!的算法是 1*2*3*..n,如果有錯要說喔import java.io.*;public class JavaHomeWork { public static void main(String[] args) throws IOException { int n; double sum = 0, plus; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("請輸入n值:"); n = Integer.parseInt(br.readLine()); for (int i = 1; i <= n; i++) { plus = 1; for (int j = 1; j <= i; j++) { plus = plus * j; } plus=1/plus; sum = sum + plus; } System.out.print("sum=" + sum); }}註:把上面紅字拿掉就是第1題,紅字留著是第2題 .msgcontent .wsharing ul li { text-indent: 0; } 分享 Facebook Plurk YAHOO! .
- Dec 25 Sun 2011 21:03
用巢狀迴圈,計算1!+2!+...+n! 的總和
close
全站熱搜
留言列表