Created
October 26, 2014 07:59
-
-
Save syobochim/3f7566e6d857b0ea4a8b to your computer and use it in GitHub Desktop.
クラッツ問題。うまく描けていない感じある
This file contains 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.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