작성일 :

문제 링크

5300번 - Fill the Rowboats!

설명

1부터 n까지 해적 번호를 출력하되, 6명마다 Go!를 붙이는 문제입니다.

마지막 묶음이 6명 미만이어도 Go!를 출력해야 합니다.


접근법

1부터 n까지 순회하며 번호를 출력합니다.

6으로 나누어떨어질 때마다 Go!를 출력합니다.

반복이 끝난 뒤 n이 6의 배수가 아니면 Go!를 한 번 더 출력합니다.


Code

C#

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

class Program {
  static void Main() {
    var n = int.Parse(Console.ReadLine()!);
    for (var i = 1; i <= n; i++) {
      Console.Write(i);
      Console.Write(" ");
      if (i % 6 == 0) {
        Console.Write("Go!");
        if (i != n) Console.Write(" ");
      }
    }
    if (n % 6 != 0) Console.Write("Go!");
    Console.WriteLine();
  }
}

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 n; cin >> n;
  for (int i = 1; i <= n; i++) {
    cout << i << " ";
    if (i % 6 == 0) {
      cout << "Go!";
      if (i != n) cout << " ";
    }
  }
  if (n % 6 != 0) cout << "Go!";
  cout << "\n";

  return 0;
}