Created
May 12, 2019 13:19
-
-
Save jongha/17e0c4d049deaea2cb806c0a81e06e8b to your computer and use it in GitHub Desktop.
UVa 100 - The 3n + 1 problem, https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=36
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.PrintWriter; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
new Main().go(); | |
} | |
private void go() { | |
Scanner in = new Scanner(System.in); | |
PrintWriter out = new PrintWriter(System.out, true); | |
while (in.hasNextInt()) { | |
int max = 0; | |
int s = in.nextInt(); | |
int e = in.nextInt(); | |
for (int i = Math.min(s, e); i <= Math.max(s, e); i++) { | |
int n = i; | |
int count = 0; | |
while (n != 1) { | |
n = (n % 2 == 1) ? 3 * n + 1 : n / 2; | |
++count; | |
} | |
++count; | |
if (count > max) | |
max = count; | |
} | |
out.printf("%d %d %d\n", s, e, max); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment