作者Swarley (史華利)
看板java
標題[問題] ACM的The 3n+1 Problem
時間Mon Oct 17 16:53:37 2011
最近比較有時間,想試試看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
→ zhengdavy: The integers i and j must appear in the output in t 10/17 17:00
→ zhengdavy:same order in which they appeared in the input 10/17 17:01
→ Swarley:原來是這樣! 剛剛改過後AC了!! 感謝樓上的幫忙~ 10/17 17:03
→ zhengdavy:不客氣, 繼續加油XD 10/17 17:06
推 PsMonkey:唉..... 10/17 20:46
→ Swarley:版主不好意思,改成這樣還是有違反版規嗎? 10/17 22:47
→ zhengdavy:下次把題目也po上來會比較好 10/18 00:34
→ whimsical:用動態規劃阿 10/24 01:45