[자바 백준 알고리즘] 문제 1110번 "더하기 사이클" (java backjoon 1110) 풀이
안녕하세요 ~ 백준 알고리즘 문제 1110번 입니다.
코드가 지저분한데 저같은경우는 integer 클래스 위주로 사용하여 문제를 해결하였습니다.
궁금한점 있으시면 댓글 달아 주세요~!
import java.util.Scanner;
public class Pro_1110b {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int num, new_num = 0;
String s;
int num_result;
int count = 0;
String str_result;
num = sc.nextInt();
if (num < 10)
s = "0" + Integer.toString(num);
else
s = Integer.toString(num);
// 결과 값을 int형말고 string 으로 생각
// s=26 s 서로 더한건 num_result = 8 , 새로운수 new_num 68
num_result = Integer.parseInt(s.substring(0, 1)) + Integer.parseInt(s.substring(1, 2));
str_result = Integer.toString(num_result);
while (true) {
// s는 초기값 .
// 새로운수 new_num = 68
if (num_result < 10)
new_num = Integer.parseInt(s.substring(1, 2) + str_result.substring(0, 1));
else
new_num = Integer.parseInt(s.substring(1, 2) + str_result.substring(1, 2));
if (num == new_num) {
count++;
break;
} else {
count++;
// s = "68" 이 들어간다.
if (new_num < 10)
s = "0" + Integer.toString(new_num);
else
s = Integer.toString(new_num);
// 6+8 = 14
num_result = Integer.parseInt(s.substring(0, 1)) + Integer.parseInt(s.substring(1, 2));
str_result = Integer.toString(num_result);
}
}
System.out.println(count);
sc.close();
}
}