작성일 :

문제 링크

1284번 - 집 주소

설명

집 호수판의 너비를 계산하는 문제입니다. 숫자 1은 2cm, 숫자 0은 4cm, 나머지 숫자는 3cm의 너비를 가집니다. 숫자 사이에는 1cm의 여백이 있고, 양 끝에는 각각 1cm씩 총 2cm의 여백이 있습니다.


접근법

호수를 문자열로 읽어 각 자릿수의 너비를 더합니다. 양 끝 여백 2cm와 숫자 사이 여백을 함께 계산하여 총 너비를 구합니다. 0이 입력되면 종료합니다.



Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;

class Program {
  static int Width(char ch) {
    if (ch == '1') return 2;
    if (ch == '0') return 4;
    return 3;
  }

  static void Main() {
    string? s;
    while ((s = Console.ReadLine()) != null) {
      if (s == "0") break;
      var len = s.Length;
      var sum = 2 + (len - 1);
      foreach (var ch in s) sum += Width(ch);
      Console.WriteLine(sum);
    }
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
using namespace std;

int width(char ch) {
  if (ch == '1') return 2;
  if (ch == '0') return 4;
  return 3;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  string s;
  while (cin >> s) {
    if (s == "0") break;
    int len = s.size();
    int sum = 2 + (len - 1);
    for (char c : s) sum += width(c);
    cout << sum << "\n";
  }

  return 0;
}