elesis's haunt

1110 더하기 사이클 본문

백준~문풀 후 최적화 추가~/단계별로 풀어보기

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의자리 나눠서 생각하지말고 합쳐서 봐도 되었다.

'백준~문풀 후 최적화 추가~ > 단계별로 풀어보기' 카테고리의 다른 글

2562 최댓값  (0) 2021.09.23
10818 최소, 최대  (0) 2021.09.16
10951 A+B - 4  (0) 2021.09.14
10952 A+B - 5  (0) 2021.09.13
10871 X보다 작은 수  (0) 2021.09.13
Comments