[問題] ACM的The 3n+1 Problem

看板java作者 (史華利)時間14年前 (2011/10/17 16:53), 編輯推噓1(107)
留言8則, 4人參與, 最新討論串1/1
最近比較有時間,想試試看ACM的題目 但是寫了第一個The 3n+1 problem就卡關了 編譯會過,沒有任何錯誤訊息 自己餵測資答案都正確,但是上傳上去總是說Wrong Answer 我有注意到輸入的兩個數字的大小問題,但還是有錯 麻煩各位先進幫忙看一下究竟是哪邊出錯了,謝謝! =================================================================== import java.io.BufferedInputStream; import java.util.Scanner; public class Main { public static void main (String[] args){ // input1和input2為輸入數字,min和max為判斷後的起始值和終值 // max_count用來記錄最大的cycle length int i=0, tmp=0, input1=0, input2=0, min=0, max=0; int count=0, max_count=0; Scanner input = new Scanner(new BufferedInputStream(System.in)); // 用來確認有沒有吃字串進來 while(input.hasNext()){ input1 = input.nextInt(); input2 = input.nextInt(); // 先做兩個數字的判斷,避免輸出出錯 if(input1 > input2){ min = input2; max = input1; }else{ min = input1; max = input2; } max_count = 0; // 開始對min到max間的數字尋找cycle length並記錄最大值 for(i=min ; i<=max ; i++){ tmp = i; count=1; while(tmp!=1){ if(tmp%2==0) tmp = tmp/2; else tmp = 3*tmp+1; count++; } if(max_count<count) max_count = count; } //輸出最後結果 System.out.println(min + " " + max + " " + max_count); } } } =================================================================== -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.111.129.79

10/17 17:00, , 1F
The integers i and j must appear in the output in t
10/17 17:00, 1F

10/17 17:01, , 2F
same order in which they appeared in the input
10/17 17:01, 2F

10/17 17:03, , 3F
原來是這樣! 剛剛改過後AC了!! 感謝樓上的幫忙~
10/17 17:03, 3F

10/17 17:06, , 4F
不客氣, 繼續加油XD
10/17 17:06, 4F

10/17 20:46, , 5F
唉.....
10/17 20:46, 5F

10/17 22:47, , 6F
版主不好意思,改成這樣還是有違反版規嗎?
10/17 22:47, 6F

10/18 00:34, , 7F
下次把題目也po上來會比較好
10/18 00:34, 7F

10/24 01:45, , 8F
用動態規劃阿
10/24 01:45, 8F
文章代碼(AID): #1Ec-qKrk (java)