Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
85 lines (74 loc) · 1.8 KB
/
Copy pathMain.java
File metadata and controls
85 lines (74 loc) · 1.8 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Collections;
import java.util.Scanner;
class Main{
public static HashMap<Integer, Integer> M = new HashMap<Integer, Integer> ();
public static void main(String[] args) {
int [] in = read();
while(in != null ) {
M = new HashMap<Integer, Integer> ();
System.out.print(""+ in[0]+" "+in[1]+" ");
System.out.println(+algorithm(in[0],in[1]));
in = read();
}
}
public static int algorithm(int a, int b ){
int max =-1;
int n=1;
for (int i=a; i<=b; i++){
int bla = getCycle(i);
M.put(i,bla );
}
int res = Collections.max(M.values());
return res;
}
public static int getCycle(int n){
int tmp;
if (M.containsKey(n))
return M.get(n);
if (n==1)
return 1;
else if (n %2 == 0){
tmp = getCycle(n/2) +1;
M.put(n, tmp);
return tmp;
}
else if (n %2 == 1){
tmp = getCycle(3*n+1) +1;
M.put(n, tmp);
return tmp;
}
return -1;
}
public static int[] read(){
int [] input = new int[2];
try {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String line = stdin.readLine();
if (line.isEmpty()){
return null;
}
String [] result = line.split("\\s+");
int i=0;
for (String s: result){
input[i] = Integer.parseInt(s);
i++;
}
return input;
}
catch (java.io.IOException e) { e.printStackTrace() ;}
catch (NumberFormatException e) {e.printStackTrace() ; }
catch (Exception e) {e.printStackTrace() ;}
return null;
}
public static void readIn(){
Scanner in = new Scanner(System.in);
int n;
while(in.hasNextInt()) {
n = in.nextInt();
}
// System.out.printf("%4d, %6.2f, %s, %c\n", i, d, s, c);
}
}