[SWEA] 극한의 청소 작업

문제 보러가기

제한사항

이외의 제한사항은 없다.

첫번째 생각

1층부터 10층까지 중에 4층이 없다는 것은 수를 9진수로 표현함과 같다고 할 수 있다.

따라서 9진수로 층수를 표현 해 준 뒤, 10진수로 변환해주어 계산하면 구할 수 있다.

단, 0층은 존재하지 않으므로 지상 ~층부터 지하 ~층까지 존재한다면 결과값에서 -1을 해주어 계산한다.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for(int tc=1;tc<=T;tc++) {
			long A = sc.nextLong();
			long B = sc.nextLong();
			long AA = toNine(A);
			long BB = toNine(B);
			long AAA = toDigit(AA);
			long BBB = toDigit(BB);
			long res = BBB-AAA;
			
			if( (AAA<0 && BBB<0) || (AAA>0 && BBB>0) )
			{ } else {
				res = res - 1;
			}

			System.out.println("#"+tc+" "+res);
		}
		
	}
	
	public static long toNine(long A) {
		boolean minus = false;; 
		if( A < 0 ) {
			minus = true;
		}
		String s = Math.abs(A)+"";
		StringBuilder sb = new StringBuilder("");
		if ( minus ) {
			sb.append("-");
		}
		for(int i=0; i<s.length();i++) {
			if( (s.charAt(i)-'0') > 4 ) {
				sb.append(s.charAt(i)-'1');
			} else {
				sb.append(s.charAt(i)-'0');
			}
		}
		return Long.parseLong(sb.toString());
	}
	
	public static long toDigit(long A) {
		boolean minus = false;; 
		if( A < 0 ) {
			minus = true;
		}
		String s = A+"";
		Long res = (long) 0;
		Long cipher = (long) 0;
		
		for(int i=s.length()-1; i>=0;i--) {
			if ( s.charAt(i) == '-')
				continue;
			res += (s.charAt(i)-'0') * (long)Math.pow(9, cipher++);
		}
		if ( minus ) {
			res = res * -1;
		}
		return res;
	}
}