๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/27060)
< Vertical Histogram >
๋ฌธ์ ํ์ด
1) ๊ฐ ๋ฌธ์์ ๋ฑ์ฅ ํ์ ๊ตฌํ๊ธฐ
2) ํ์ ๊ตฌํ๊ธฐ ์ํด ๋ฑ์ฅ ํ์ ์ต๋๊ฐ ๊ตฌํ๊ธฐ
3) ๋ถํ์ํ ๋น์นธ์ ์ถ๋ ฅํ์ง ์๊ธฐ ์ํด ๊ฐ ํ์ ์ด ์ต๋๊ฐ ๊ตฌํ๊ธฐ
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 _270606_ { // Vertical Histogram
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int arr[] = new int[26];
for (int i = 0; i < 4; i++) {
String str = bf.readLine();
for (int j = 0; j < str.length(); j++) {
if (Character.isAlphabetic(str.charAt(j))) {
arr[str.charAt(j) - 'A'] += 1;
}
}
}
int max = 0;
for (int i = 0; i < 26; i++) {
max = Math.max(max, arr[i]);
}
int position[] = new int[max];
for (int i = 0; i < position.length; i++) {
for (int j = 0; j < 26; j++) {
if (arr[j] >= max) {
position[i] = j;
}
}
max -= 1;
}
max = position.length;
for (int i = 0; i < position.length; i++) {
for (int j = 0; j <= position[i]; j++) {
if (arr[j] >= max) {
bw.write("*");
if(j != position[i]) {
bw.write(" ");
}
} else {
bw.write(" ");
}
}
max-=1;
bw.write("\n");
}
for (int i = 0; i < 26; i++) {
bw.write((char) ('A' + i));
if(i!=25) {
bw.write(" ");
}
}
bw.flush();
}
}
๋ณ์)
arr : ์ํ๋ฒณ ๋ฑ์ฅ ํ์
str : ๋ฌธ์์ด
max : ์ํ๋ฒณ ๋ฑ์ฅ ํ์ ์ค ์ต๋๊ฐ
position : ๊ฐ ํ์ ์ด ์ต๋๊ฐ
4์ค์ ๋ฌธ์์ด์ ์ ๋ ฅ๋ฐ์ผ๋ฉด์ ์ํ๋ฒณ ๋ฑ์ฅ ํ์๋ฅผ ๋ฐฐ์ด arr์ ์ ์ฅํ๋ค. ํ ๊ฐ์ ๊ตฌํ๊ธฐ ์ํด arr ๊ฐ ์ค์์ ์ต๋๊ฐ์ ๊ตฌํ๋ค. ๋ถํ์ํ ๋น์นธ์ ์ถ๋ ฅํ์ง ์๊ธฐ ์ํด ๊ฐ ํ์ ์ด ์ต๋๊ฐ์ ๊ตฌํ๋ค.
ํ ๊ฐ๋งํผ ๋ฐ๋ณตํ์ฌ ์ด ์ต๋๊ฐ๊น์ง ํ์ํ๋ฉฐ ํด๋น๋๋ ์นธ์ "*"์ ์ถ๋ ฅํ๊ณ ํด๋น๋์ง ์๋ ์นธ์๋ ๊ณต๋ฐฑ์ ์ถ๋ ฅํ๋ค. ๋ง์ง๋ง ์ค์๋ ๋๋ฌธ์ ์ํ๋ฒณ์ ์์๋๋ก ์ถ๋ ฅํ๋ค.
'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 9523_Arithmetic with Morse (1) | 2025.07.15 |
---|---|
[Baekjoon] 14210_Kartomat (0) | 2025.07.14 |
[Baekjoon] 32076_Easy as ABC (2) | 2025.07.10 |
[Baekjoon] 3518_๊ณต๋ฐฑ์ ๋น-์นธ (1) | 2025.07.09 |
[Baekjoon] 16300_H to O (2) | 2025.07.08 |