작성일 :

문제 링크

13773번 - Olympic Games

설명

여러 연도가 주어질 때, 각 연도에 대해 올림픽 개최 여부를 출력하는 문제입니다.

1916, 1940, 1944년은 전쟁으로 취소되었고, 1896년부터 4년 주기로 여름 올림픽이 열립니다. 2020년까지는 개최되었고, 이후는 도시가 미정입니다.


접근법

먼저, 1916, 1940, 1944년이면 Games cancelled를 출력합니다.

다음으로, 1896년 이후이면서 4의 배수인 해는 여름 올림픽 해입니다. 2020년 이하면 Summer Olympics, 이후면 No city yet chosen을 출력합니다.

이후, 그 외의 해는 No summer games를 출력합니다. 입력이 0이면 종료합니다.


Code

C#

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

namespace Solution {
  class Program {
    static void Main(string[] args) {
      while (true) {
        var line = Console.ReadLine();
        if (line == null) break;
        var y = int.Parse(line);
        if (y == 0) break;

        if (y == 1916 || y == 1940 || y == 1944) {
          Console.WriteLine($"{y} Games cancelled");
        } else if (y >= 1896 && y % 4 == 0) {
          if (y <= 2020) Console.WriteLine($"{y} Summer Olympics");
          else Console.WriteLine($"{y} No city yet chosen");
        } else Console.WriteLine($"{y} No summer games");
      }
    }
  }
}

C++

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

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

  int y;
  while (cin >> y) {
    if (y == 0) break;
    if (y == 1916 || y == 1940 || y == 1944) {
      cout << y << " Games cancelled\n";
    } else if (y >= 1896 && y % 4 == 0) {
      if (y <= 2020) cout << y << " Summer Olympics\n";
      else cout << y << " No city yet chosen\n";
    } else cout << y << " No summer games\n";
  }

  return 0;
}