본문 바로가기

Algorithm/Implementation

(C++) - 백준(BOJ) 3578 : Holes

반응형

https://www.acmicpc.net/problem/3578

 

3578번: Holes

You may have seen a mechanic typewriter — such devices were widespread just 15 years ago, before computers replaced them. It is a very simple thing. You strike a key on the typewriter keyboard, the corresponding type bar rises, and the metallic letter mo

www.acmicpc.net

간단한 구현 문제였습니다.

📕 풀이방법

📔 입력 및 초기화

구멍의 개수 h를 선언 후 입력받습니다.

📔 풀이과정

4개의 경우가 있습니다. 

1. h = 0인 경우 최소 수는 1입니다.

2. h = 1인 경우 최소 수는 0입니다.

3. 이 외

  3-1. h가 홀수인 경우 : 수를 만들 때 가장 앞쪽에 0이 올 수 없으므로 가장 앞자리는 4가 오게 됩니다. 나머지는 h/2만큼 8이 나옵니다.

  3-2. h가 짝수인 경우 : h/2만큼 8이 오면 됩니다.

📔 정답출력

조건에 맞게 출력합니다.


📕 Code

#include <bits/stdc++.h>
using namespace std;
int h;
int main(){
  cin >> h;
  if(!h) {cout << 1; return 0;}
  if(h == 1) {cout << 0; return 0;}

  if(h % 2) cout << 4;
  for(int i = 0; i < h/2; i++) cout << 8;
}

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.