-
1110 - 더하기 사이클알고리즘 2023. 5. 24. 21:02728x90
https://www.acmicpc.net/problem/1110
문제요약
- 주어진 숫자의 각 자릿수를 더한다 ( 10 보다 작은 숫자는 앞자리에 0을 더하고 시작 )
- 더한 숫자의 1의 자리와 더해진 숫자의 1의 자리를 다시 합쳐 새로운 숫자를 만든다.
- 반복...
- 처음의 숫자가 나오기까지의 몇 번을 반복했는지 확인한다.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String input = reader.readLine(); String copy_input = input; if (input.equals("0")) { System.out.println("1"); return; } int cnt = 0; if (copy_input.length() == 1) { copy_input = copy_input + copy_input; input = "0" + input; cnt++; } while (true) { int a = Integer.parseInt(copy_input.split("")[0]); int b = Integer.parseInt(copy_input.split("")[1]); int c = a + b; if (("" + c).length() >= 2) { //2의자리 if (b == 0) { copy_input = (("" + c).substring(1)); } else { copy_input = b + "" + (("" + c).substring(1)); } } else { //1의자리 copy_input = b + "" + c; } cnt++; if (copy_input.equals(input)) { break; } } System.out.println(cnt); } }
느낀점
아직까지는 종이에 문제를 써보고 푸는 방법을 확인해보고,
원리를 파악하고나면 간단하게 코딩으로 옮겨가면서 예외적인 부분만 Debugging으로 체크하는 식으로 진행한다.
꾸준히 하면서 다른 방법도 찾아보고,
어느정도 암기가 필요한 것이 무엇인지 파악하고 하는 식으로 진행해야겠다.!
'알고리즘' 카테고리의 다른 글
1002 - 터렛 (0) 2023.05.24 1011 - Fly me to the Alpha Centauri (0) 2023.05.24 1929 - 소수 구하기 (0) 2023.05.24 10250 - ACM 호텔 (0) 2023.05.24 2869 - 달팽이는 올라가고 싶다 (0) 2023.05.23