這是ACM上面比較簡單的題目
程式功力尚須加強
import java.io.* ;
public class nnn1
{
public static void main(String args[]) throws IOException
{
System.out.println("Please input a number:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str2 = br.readLine();
String str1 = br.readLine();
int small_num = Integer.parseInt(str2);
int num = Integer.parseInt(str1);
System.out.println(small_num+" "+num);
int temp;
int max_count =0;//紀錄最大運算次數
if(small_num > num)//輸入數字大小順序對調皆可
{
temp = small_num;
small_num = num;
num = temp;
}
// System.out.println(small_num+" "+num);
for(int j=small_num;j<=num;j++)//每個數字的計算過程以及次數
{
int count =1;
int number = j;
System.out.println(number);
do
{
//System.out.println(num);
if ( number== 1 )
{break;}
if(number%2==1)
{
number = 3*number+1;
}
else
{
number = number/2;
}
count++;
}while(true);
System.out.println("count is "+count);
if(count > max_count)
{
max_count = count;
}
}//是forloop的大括號
System.out.println("Max count is "+max_count);
}
}
沒有留言:
張貼留言