๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 2757_์—‘์…€

๋ฟŒ์•ผ._. 2024. 7. 15. 16:59
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/2757)

< ์—‘์…€ >

 

๋ฌธ์ œ ํ’€์ด 

 

26์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€์™€ ๋ชซ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ต์„ ๊ตฌํ•œ๋‹ค.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class _2757_ { // ์—‘์…€

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		char alphabet[] = { 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
				'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y' };
		String str = "";
		while (!(str = bf.readLine()).equals("R0C0")) {
			String arr[] = str.split("R|C");

			String temp = "";
			int num = Integer.parseInt(arr[2]);
			while (num > 0) {
				temp += alphabet[(num % 26)];
				if (num % 26 == 0) {
					num = (num / 26) - 1;
				} else {
					num /= 26;
				}
			}
			String answer = "";
			for (int i = temp.length() - 1; i >= 0; i--) {
				answer += temp.charAt(i);
			}
			answer += arr[1];
			bw.write(answer + "\n");
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
alphabet : ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅํ•œ๋‹ค. (0๋ฒˆ์งธ๋Š” z๋กœ ์ €์žฅํ•œ๋‹ค)
str : ์ž…๋ ฅ๊ฐ’
arr : R๊ณผ C๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ž๋ฅธ ๊ฐ’
temp, num : ์—ด ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜
answer : ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ ์ƒ์—์„œ์˜ ์ด๋ฆ„

 

R0C0์ด ์•„๋‹ ๋•Œ๊นŒ์ง€ RnCm ํ˜•ํƒœ๋กœ ์ž…๋ ฅ๋ฐ›์•„ ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

1) R๊ณผ C๋ฅผ ๊ธฐ์ค€์œผ๋กœ split ํ•œ ๊ฐ’์„ arr์— ์ €์žฅ

2) arr[2]๋ฒˆ์งธ ๊ฐ’์„ num์— ์ €์žฅํ•œ ํ›„ num์ด 0๋ณด๋‹ค ํด ๋•Œ ๋ฐ˜๋ณตํ•œ๋‹ค. num์„ 26์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ ๊ฐ’์„ temp์— ์ €์žฅํ•˜๊ณ  26์œผ๋กœ ๋‚˜๋ˆˆ ๋ชซ์„ num์— ์ €์žฅํ•œ๋‹ค.

3) answer์— temp ๊ฐ’์„ ๋’ค์ง‘์–ด ์ €์žฅํ•œ ํ›„ arr[1] ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค.

 

์ตœ์ข… answer์„ ์ถœ๋ ฅํ•œ๋‹ค.