백준~문풀 후 최적화 추가~/단계별로 풀어보기
1110 더하기 사이클
elesis
2021. 9. 16. 14:58
* BufferedReader, StringTokenizer, System.out.println
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(br.readLine());
br.close();
int nextNum = 0;
int c, f, cycle = 0;
c = (num/10 + num%10) %10;
nextNum = Integer.parseInt(String.valueOf(num%10)+String.valueOf(c));
cycle++;
while(num!=nextNum) {
f = (nextNum/10 + nextNum%10) %10;
nextNum = Integer.parseInt(String.valueOf(nextNum%10)+String.valueOf(f%10));
cycle++;
}
System.out.println(cycle);
}
}
-> 여기서는 System.out.println이 BufferedWriter보다 빨랐다.
더 빠르고 바른 로직
* do while
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int cnt = 0;
int copy = N;
do {
N = ((N % 10) * 10) + (((N / 10) + (N % 10)) % 10);
cnt++;
} while (copy != N);
System.out.println(cnt);
}
}
// T의 십의 자릿수
T = (N % 10) * 10
// T의 일의 자릿수
T = ((N / 10) + (N % 10)) % 10
10의자리 1의자리 나눠서 생각하지말고 합쳐서 봐도 되었다.