문제 링크 : www.acmicpc.net/problem/2290
2290번: LCD Test
첫째 줄에 두 개의 정수 s와 n이 들어온다. (1 ≤ s ≤ 10, 0 ≤ n ≤ 9,999,999,999)이다. n은 LCD 모니터에 나타내야 할 수 이며, s는 크기이다.
www.acmicpc.net
문제
지민이는 새로운 컴퓨터를 샀다. 하지만 새로운 컴퓨터에 사은품으로 온 LC-디스플레이 모니터가 잘 안나오는 것이다. 지민이의 친한 친구인 지환이는 지민이의 새로운 모니터를 위해 테스트 할 수 있는 프로그램을 만들기로 하였다.
입력
첫째 줄에 두 개의 정수 s와 n이 들어온다. (1 ≤ s ≤ 10, 0 ≤ n ≤ 9,999,999,999)이다. n은 LCD 모니터에 나타내야 할 수 이며, s는 크기이다.
출력
길이가 s인 '-'와 '|'를 이용해서 출력해야 한다. 각 숫자는 모두 s+2의 가로와 2s+3의 세로로 이루어 진다. 나머지는 공백으로 채워야 한다. 각 숫자의 사이에는 공백이 한 칸 있어야 한다.
접근 방법
숫자 n을 가로로 분할해서 생각해보았다.
예를들어 123을 출력한다고 가정하고 s = 2라고 하자. 그러면 숫자 높이는 7이 될 것이다.
즉, 숫자를 가로로 7분할 한다고 생각해보면, 아래와 같이 나타낼 수 있다.
123의 첫번째 라인
123의 두번째 라인
...
123의 여섯번째 라인
123의 일곱번째 라인
또한 LCD 특성에 맞게 숫자를 만들기 위한 가로줄의 경우가 총 5가지 경우로 한정되어 있으므로 중복코드를 피하기 위해 아래 코드와 같이 함수를 따로 작성해주었다. (space, horizon, verRight, verLeft, verBoth)
// // SM_BOJ2290_LCD Test.cpp // Coding_Test_Practice // // Created by 김난영 on 2021/03/21. // Copyright © 2021 KimNanyoung. All rights reserved. // #include <iostream> #include <string> #include <cmath> #include <algorithm> using namespace std; void printSpace(int s){ //가로 길이 s+2, 공백만 출력 for(int i = 0; i<s+2; i++){ printf(" "); } } void printHorizon(int s){ //가로 길이 s+2, 양 옆만 공백, 가운데는 -로 채움 printf(" "); for(int i = 0; i<s; i++){printf("-");} printf(" "); } void printVerRight(int s){ //가로 길이 s+2, 오른쪽 마지막에 |출력 for(int i = 0; i<s+1; i++){printf(" ");} printf("|"); } void printVerLeft(int s){ //가로 길이 s+2, 왼쪽 처음에 |출력 printf("|"); for(int i = 0; i<s+1; i++){printf(" ");} } void printVerBoth(int s){ //양쪽 공백, 가운데 s만큼 | printf("|"); for(int i = 0; i<s; i++){printf(" ");} printf("|"); } void printOne(int s, int row){ if(row==1 || row==s+2 || row==2*s+3){ printSpace(s); } else{ printVerRight(s);} } void printTwo(int s, int row){ if(row==1 || row==s+2 || row==2*s+3){ printHorizon(s); } else if(2<=row && row<=s+1){ printVerRight(s); } else{ printVerLeft(s);} } void printThree(int s, int row){ if(row==1 || row==s+2 || row==2*s+3){ printHorizon(s);} else{ printVerRight(s); } } void printFour(int s, int row){ if(row==1 || row==2*s+3){ printSpace(s);} else if(row==s+2){ printHorizon(s); } else if(2<=row && row<=s+1){ printVerBoth(s); } else{ printVerRight(s); } } void printFive(int s, int row){ if(row==1 || row==s+2 || row==2*s+3){ printHorizon(s); } else if(2<=row && row<=s+1){ printVerLeft(s); } else{ printVerRight(s); } } void printSix(int s, int row){ if(row==1 || row==s+2 || row==2*s+3){ printHorizon(s); } else if(2<=row && row<=s+1){ printVerLeft(s); } else{ printVerBoth(s); } } void printSeven(int s, int row){ if(row==1){ printHorizon(s); } else if(row==s+2 || row==2*s+3){ printSpace(s); } else{ printVerRight(s); } } void printEight(int s, int row){ if(row==1 || row==s+2 || row==2*s+3){ printHorizon(s); } else{ printVerBoth(s); } } void printNine(int s, int row){ if(row==1 || row==s+2 || row==2*s+3){ printHorizon(s); } else if(2<=row && row<=s+1){ printVerBoth(s); } else{ printVerRight(s); } } void printZero(int s, int row){ if(row==1 || row==2*s+3){ printHorizon(s); } else if(row==s+2){ printSpace(s); } else{ printVerBoth(s); } } int main(){ int s,n; cin >> s >> n; int height = 2*s + 3; string N = to_string(n); for(int row=1; row<=height; row++){ for(int i = 0; i<N.length(); i++){ if(N[i]=='1'){ printOne(s, row); } else if(N[i]=='2'){ printTwo(s, row); } else if(N[i]=='3'){ printThree(s, row); } else if(N[i]=='4'){ printFour(s, row); } else if(N[i]=='5'){ printFive(s, row); } else if(N[i]=='6'){ printSix(s, row); } else if(N[i]=='7'){ printSeven(s, row); } else if(N[i]=='8'){ printEight(s, row); } else if(N[i]=='9'){ printNine(s, row); } else if(N[i]=='0'){ printZero(s, row); } printf(" "); } printf("\n"); } return 0; }

'Algorithm(BOJ) > Simulation' 카테고리의 다른 글
[C++] 백준 20055번 - 컨베이어 벨트 위의 로봇 (구현) (0) | 2021.03.24 |
---|---|
[C++] 백준 15685번 - 드래곤 커브 (구현 / 스택) (0) | 2021.03.21 |
[C++] 백준 14503번 - 로봇청소기(DFS) (0) | 2021.03.21 |
[C++] 백준 14890번 - 경사로 (삼성 SW 역량 테스트 기출) (0) | 2021.03.11 |
[C++] 백준 15662번 - 톱니바퀴2 (시뮬레이션/구현) (0) | 2021.03.11 |