close







JAVA做Hanoi tower及pseudo-random




立即點擊


製作1CreateaJavaprogramtosolvetheproblemofHanoitowerwhichgetsthenumberofdisks(N)fromthekeyboardandprintouttheexecutiontime.Testtheprogramfor:1.N=502.N=100製作2Writethecodetoprintouthowlongittakestogeneratethepseudo-randominteger11111iftheinitialseedis...顯示更多製作1CreateaJavaprogramtosolvetheproblemofHanoitowerwhichgetsthenumberofdisks(N)fromthekeyboardandprintouttheexecutiontime.Testtheprogramfor:1.N=502.N=100製作2Writethecodetoprintouthowlongittakestogeneratethepseudo-randominteger11111iftheinitialseedis100andtherangeofpseudo-randomintegersis0through20000.更新:我試著測試一下好像有錯需要怎麼改





請參考我的做法 import java.util.*; public class Y4391 { public static void main(String args[]) { int n; Scanner scan = new Scanner(System.in); System.out.print("Enter the number of disks: "); n = Integer.parseInt(scan.nextLine()); long start = System.currentTimeMillis(); move(n, 'A', 'B', 'C'); long end = System.currentTimeMillis(); System.out.println("Execution time: " + (end - start) / 1000D + " seconds"); // Pseudo-random pseudoRandomTime(100, 20000, 11111); } public static void move(int n, char a, char b, char c) { if (n == 1) ; // System.out.println("Disk " + n + " from " + a + " to " + c); else { move(n - 1, a, c, b); // System.out.println("Disk " + n + " from " + a + " to " + c); move(n - 1, b, a, c); } } public static void pseudoRandomTime(int seed, int high, int target) { long start = System.currentTimeMillis(); Random r = new Random(seed); while (true) { int x = r.nextInt(high); if (x == target) break; } long end = System.currentTimeMillis(); System.out.println("Pseudo-Random execution time: " + (end - start) / 1000D + " seconds"); } } 2013-04-02 17:05:46 補充: 我試著跑了一下, 應該沒有錯...不用改吧








以上文章來自奇摩知識家,如有侵犯請留言告知


https://tw.answers.yahoo.com/question/index?qid=20130328000016KK04391

C902071D09AF7660
arrow
arrow

    雞排妹伴遊 發表在 痞客邦 留言(0) 人氣()