Last active
December 9, 2019 14:40
-
-
Save jayden-lee/bf6d4f5e1edfb4b51cfe8201b03a3d35 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.Scanner; | |
/** | |
* 콘웨이 시퀀스 문제 | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Input"); | |
int startNum = scanner.nextInt(); | |
if (startNum < 1 || startNum > 100) { | |
throw new IllegalArgumentException("첫 번째 숫자는 0보다 크고 100보다 작아야 합니다. (0 < R < 100)"); | |
} | |
int lineNum = scanner.nextInt(); | |
if (lineNum < 1 || lineNum > 26) { | |
throw new IllegalArgumentException("출력할 라인 숫자는 0보다 크고 26보다 작아야 합니다. (0 < L <= 25)"); | |
} | |
String number = String.valueOf(startNum); | |
for (int i = 1; i < lineNum; i++) { | |
number = conwaySequence(number); | |
} | |
printResult(number); | |
} | |
private static String conwaySequence(String number) { | |
StringBuilder result = new StringBuilder(); | |
int count = 1; | |
char baseNum = number.charAt(0); | |
number = number.substring(1) + " "; | |
for (char compareNum : number.toCharArray()) { | |
if (compareNum == baseNum) { | |
count++; | |
} else { | |
result.append(count + "" + baseNum); | |
count = 1; | |
baseNum = compareNum; | |
} | |
} | |
return result.toString(); | |
} | |
private static void printResult(String result) { | |
System.out.println(); | |
System.out.println("Output"); | |
for (char num : result.toCharArray()) { | |
System.out.print(num + " "); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment