Skip to content

Instantly share code, notes, and snippets.

@syobochim
Created October 26, 2014 07:59
Show Gist options
  • Save syobochim/3f7566e6d857b0ea4a8b to your computer and use it in GitHub Desktop.
Save syobochim/3f7566e6d857b0ea4a8b to your computer and use it in GitHub Desktop.
クラッツ問題。うまく描けていない感じある
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class collatz {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int times = sc.nextInt();
List<Integer> result = new ArrayList<Integer>();
for (int i = 0; i < times; i++) {
result.add(countCollatz(sc.nextInt()));
}
result.stream()
.forEach(System.out::println);
}
public static int countCollatz(int target) {
int count = 0;
while (target != 1) {
if (target % 2 == 0) {
target = target / 2;
} else {
target = target * 3 + 1;
}
count++;
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment