2012-03-05 19:13:45Morris
[UVA][JAVA][System.in.read] 371 - Ackermann Functions
public class Main {
public static void main(String[] args) {
int L, H, i, tmp;
while(true) {
L = parseInt();
H = parseInt();
if(L > H) {
tmp = L;
L = H;
H = tmp;
}
if(L == 0 && H == 0)
break;
int longest = 0, v = 0;
for(i = L; i <= H; i++) {
tmp = function((long)i);
if(tmp > longest) {
longest = tmp;
v = i;
}
}
System.out.printf("Between %d and %d, %d generates the longest sequence of %d values.\n",
L, H, v, longest);
}
}
public static int function(long n) {
int length = 0;
do {
if((n&1) != 0)
n = n*3+1;
else
n /= 2;
length++;
} while(n != 1);
return length;
}
public static int parseInt() {
int i, value = 0, tmp;
try {
tmp = System.in.read();
while(tmp < '0' || tmp > '9')
tmp = System.in.read();
value = tmp-'0';
tmp = System.in.read();
while(tmp >= '0' && tmp <= '9') {
value = value * 10 + tmp-'0';
tmp = System.in.read();
}
} catch(Exception e) {}
return value;
}
}
public static void main(String[] args) {
int L, H, i, tmp;
while(true) {
L = parseInt();
H = parseInt();
if(L > H) {
tmp = L;
L = H;
H = tmp;
}
if(L == 0 && H == 0)
break;
int longest = 0, v = 0;
for(i = L; i <= H; i++) {
tmp = function((long)i);
if(tmp > longest) {
longest = tmp;
v = i;
}
}
System.out.printf("Between %d and %d, %d generates the longest sequence of %d values.\n",
L, H, v, longest);
}
}
public static int function(long n) {
int length = 0;
do {
if((n&1) != 0)
n = n*3+1;
else
n /= 2;
length++;
} while(n != 1);
return length;
}
public static int parseInt() {
int i, value = 0, tmp;
try {
tmp = System.in.read();
while(tmp < '0' || tmp > '9')
tmp = System.in.read();
value = tmp-'0';
tmp = System.in.read();
while(tmp >= '0' && tmp <= '9') {
value = value * 10 + tmp-'0';
tmp = System.in.read();
}
} catch(Exception e) {}
return value;
}
}