작성일 :

문제 링크

18245번 - 이상한 나라의 암호

설명

여러 줄의 문장이 주어질 때, i번째 줄은 첫 글자에서 시작해 i칸씩 건너뛰며 읽어 해석한 결과를 출력하는 문제입니다.


접근법

i번째 줄은 간격이 i칸이므로 실제로는 인덱스를 i+1씩 증가시키며 문자를 선택합니다.
각 줄에 대해 0번 인덱스부터 i+1 간격으로 문자를 모아 출력하면 됩니다.


Code

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
25
using System;
using System.Text;

class Program {
  static void Main() {
    var outSb = new StringBuilder();
    var idx = 1;

    while (true) {
      var line = Console.ReadLine();
      if (line == null) break;
      if (line == "Was it a cat I saw?") break;

      var step = idx + 1;
      var sb = new StringBuilder();
      for (var i = 0; i < line.Length; i += step)
        sb.Append(line[i]);

      outSb.AppendLine(sb.ToString());
      idx++;
    }

    Console.Write(outSb);
  }
}

C++

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

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

  string line;
  int idx = 1;
  while (getline(cin, line)) {
    if (line == "Was it a cat I saw?") break;
    int step = idx + 1;
    string res;
    for (int i = 0; i < (int)line.size(); i += step)
      res.push_back(line[i]);
    cout << res << "\n";
    idx++;
  }

  return 0;
}