作者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
1F:→ zhengdavy: The integers i and j must appear in the output in t 10/17 17:00
2F:→ zhengdavy:same order in which they appeared in the input 10/17 17:01
3F:→ Swarley:原来是这样! 刚刚改过後AC了!! 感谢楼上的帮忙~ 10/17 17:03
4F:→ zhengdavy:不客气, 继续加油XD 10/17 17:06
5F:推 PsMonkey:唉..... 10/17 20:46
6F:→ Swarley:版主不好意思,改成这样还是有违反版规吗? 10/17 22:47
7F:→ zhengdavy:下次把题目也po上来会比较好 10/18 00:34
8F:→ whimsical:用动态规划阿 10/24 01:45