Algorithm/Baekjoon

[백준 1158] C++ - 요세푸스 순열

say! 2024. 7. 21. 17:46
728x90

Queue를 이용!

다른 분 풀이를 참고했다.😮

이런 식으로 뒤로 넣고 pop하고 반복

//백준 1158 - 요세푸스 문제
#include <cstdio>
#include <queue>
using namespace std;

int main(){
  int n, k;
  scanf("%d %d", &n, &k);
  queue<int> q; // 큐 생성

  for(int i = 1; i <= n; i++) // 큐에 정수 push
    q.push(i);
  
  // 요세푸스 순열 출력
  printf("<");
  while(!q.empty()){  // 큐가 empty일 때까지 반복
    for(int i = 0; i < k-1; i++){
      q.push(q.front());
      q.pop();
    }
    
    if(q.size() == 1){
      printf("%d>", q.front());
    }
    else {
      printf("%d, ", q.front());
    }
    q.pop();
  }
  
  return 0;
}

 

 

'Algorithm > Baekjoon' 카테고리의 다른 글

[백준 5430] C++  (0) 2024.07.21
[백준 17952] C++  (0) 2024.07.21
[백준 10845] C++ - 큐 Queue  (0) 2024.07.21
[백준 10866] C++ - 덱 Deque  (0) 2024.07.21
🟡미완 [백준 1074] C++  (0) 2024.07.20