P1996 约瑟夫问题

https://www.luogu.com.cn/problem/P1996

#include <bits/stdc++.h>
using namespace std;
int a[10005], x, t, h, n, m, c;
void push(int x){
	a[t]=x;
	t++;
}
int back(){
	return a[t - 1];
}
int front(){
	return a[h];
}
void pop(){
	h ++;
}
int size(){
	return t - h;
}
int main(){
	cin >>n>>m;
	for (int i = 1;i <= n;i ++){
		push(i);
	}
	while(size()){
		c ++;
		if (c == m){
			c = 0;
			cout <<front()<<" ";
			pop();
		}
		else{
			push(front());
			pop();
		} 
	}
	return 0;
}
posted @ 2025-11-10 20:35  FayeTY  阅读(3)  评论(0)    收藏  举报