-
Notifications
You must be signed in to change notification settings - Fork 11
/
UVa00100_The3n1problem.java
64 lines (57 loc) · 1.56 KB
/
UVa00100_The3n1problem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 36 (100 - The 3n + 1 problem) */
/* SUBMISSION: 07273132 */
/* SUBMISSION TIME: 2009-07-25 23:52:13 */
/* LANGUAGE: 2 */
import java.util.*;
public class UVa00100_The3n1problem
{
static int cycleLength(int n)
{
int cont = 1;
while (n != 1) {
if (n%2 == 1)
n = 3*n+1;
else
n = n/2;
cont++;
}
return cont;
}
public static void main (String args[]) // entry point from OS
{
try {
Scanner in = new Scanner(System.in);
String input;
StringTokenizer idata;
int i, j, max;
input = in.nextLine();
while (!input.equals(""))
{
idata = new StringTokenizer (input);
i = Integer.parseInt (idata.nextToken());
j = Integer.parseInt (idata.nextToken());
int a;
int b;
if (i < j) {
a = i;
b = j;
} else {
a = j;
b = i;
}
max = 0;
for (int k=a; k<=b; k++) {
int c = cycleLength(k);
if (c > max)
max = c;
}
System.out.println (i + " " + j + " " + max);
input = in.nextLine();
}
} catch (Exception e) {
System.exit(0);
}
}
}