본문 바로가기
  • Let's study
PS/백준

[C++] 백준 5300: Fill the Rowboats!

by 코딩고수이고파 2023. 5. 10.

문제

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

 

5300번: Fill the Rowboats!

The output will be the number of each pirate separated by spaces, with the word ”Go!” after every 6th pirate, and after the last pirate.

www.acmicpc.net

 

해결방법

1. 1~n까지 숫자를 출력하되 6의 배수일 때마다 "Go!"를 출력한다.

2. n 출력 후 "Go!"를 출력해야 하는데 이 때 n이 6의 배수라면 for문에서의 "Go!" 출력과 중복되므로 출력하지 않는다.

 

코드

#include<iostream>

using namespace std;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin >> n;

	for (int i = 1; i <= n; i++) {
		cout << i << ' ';
		if (i%6==0)
			cout << "Go! ";
	}

	if(n%6!=0)
		cout << "Go! ";

	return 0;
}

'PS > 백준' 카테고리의 다른 글

[C++] 백준 6887: Squares  (0) 2023.05.12
백준 18698: The Walking Adam  (0) 2023.05.11
[C++] 백준 5357: Dedupe  (0) 2023.05.09
[C++] 백준 11257: IT Passport Examination  (0) 2023.05.08
[C++] 백준 3765: Celebrity jeopardy  (0) 2023.05.07

댓글