1. 문제 풀이
package id110101;
/* @BEGIN_OF_SOURCE_CODE */
/* @JUDGE_ID: 75154 100 Java "" */
import java.io.*;
import java.util.*;
public class The3nPlus1 {
public static void main(String[] args) {
The3nPlus1 main = new The3nPlus1();
main.begin();
}
private void begin() {
String line;
StringTokenizer st;
long first, second;
long maxNumber;
while ((line = readLine(255)) != null && !line.isEmpty()) {
st = new StringTokenizer(line);
first = Long.parseLong(st.nextToken());
second = Long.parseLong(st.nextToken());
maxNumber = doCalc(first, second);
System.out.println(String.format("%d %d %d", first, second, maxNumber));
}
}
private long doCalc(long first, long second) {
long value = 0;
long maxNumber = 0;
long number = 0;
for (value = first; value < second + 1; value++ ) {
number = doLoop(value);
maxNumber = maxNumber > number ? maxNumber : number;
}
return maxNumber;
}
private long doLoop(long number) {
long value = 0;
long count = 1;
for (value = number; value > 0; ) {
if (value == 1) break;
if (value % 2 == 1) {
value = 3 * value + 1;
} else {
value = value / 2;
}
count++;
}
return count;
}
private static String readLine(int maxLg) {
byte lin[] = new byte[maxLg];
int lg = 0;
int car = -1;
String line = "";
try {
while (lg < maxLg) {
car = System.in.read();
// 10: Line feed 13: Carriage return
if ((car < 0) || (car == 10) ) {
break;
}
lin[lg++] += car;
}
} catch (IOException e) {
return null;
}
if ((car < 0) && (lg == 0)) return null;
line = new String(lin, 0, lg);
line = line.replaceAll("[\n\r]", "");
return line;
}
}
/* @END_OF_SOURCE_CODE */