[問題] ACM的The 3n+1 Problem
最近比較有時間,想試試看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
10/17 17:00, 1F
→
10/17 17:01, , 2F
10/17 17:01, 2F
→
10/17 17:03, , 3F
10/17 17:03, 3F
→
10/17 17:06, , 4F
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
10/18 00:34, 7F
→
10/24 01:45, , 8F
10/24 01:45, 8F