※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 7580번 문제인 Team Selection이다.
문제는 아래 링크를 확인하자.

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

 

7580번: Team Selection

In the first sample input, there are 9 students in the circle and every 3rd student is removed. Students are removed in the circle in the order 3, 6, 9, 4, 8, 5, 2, 7, 1. The last four students to be removed are therefore 5, 2, 7 and 1. These students form

www.acmicpc.net

이 문제는 요세푸스 문제(Josephus Problem)와 같은 상황에서 마지막으로 남는 한 명이 아닌 네 명을 찾는 문제이다.

 

요세푸스 문제에 대한 설명은 다음 글(링크)를 참고하자.

 

0-based 기준으로 0번에서부터 K명을 세기 시작하는 상황에서 이전 차례에서 제외된 사람은 0번 바로 이전에 있었던 사람이 됨을 이용해 총 네 명을 역추적해나가자.

 

아래는 제출한 소스코드이다.

#include <iostream>
using namespace std;

int N, K;

void solve() {
	int ans1 = 0, ans2 = 1, ans3 = 2, ans4 = 3;
	ans1 = (ans1 + K) % 1;
	ans1 = (ans1 + K) % 2;
	ans2 = (ans2 + K) % 2;
	ans1 = (ans1 + K) % 3;
	ans2 = (ans2 + K) % 3;
	ans3 = (ans3 + K) % 3;
	ans1 = (ans1 + K) % 4;
	ans2 = (ans2 + K) % 4;
	ans3 = (ans3 + K) % 4;
	ans4 = (ans4 + K) % 4;
	for (int i = 5; i <= N; i++) {
		ans1 = (ans1 + K) % i;
		ans2 = (ans2 + K) % i;
		ans3 = (ans3 + K) % i;
		ans4 = (ans4 + K) % i;
	}

	cout << ans4 + 1 << ' ' << ans3 + 1 << ' ' << ans2 + 1 << ' ' << ans1 + 1 << '\n';
}

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

	cin >> N >> K;
	while (N) {
		solve();
		cin >> N >> K;
	}
}
728x90

+ Recent posts