求调
查看原帖
求调
1511991
lingerpetals楼主2024/11/18 23:10
#include<iostream>
using namespace std;

static int count = 0;

void josephus(int a[], int &m, int &n, const int &ret){
	if(count == ret) return;
	if(m > n) m %= n;
		count++; 
		cout<<a[m - 1]<<" ";
		int* newArray = new int [n - 1];
		for(int i = m, j = 0; i < n; ++i, ++j){
			newArray[j] =  a[i];
		}
		for(int i = 0, j = n-m; i < m - 1; ++i, ++j){
			newArray[j] = a[i];
		}
		n--;
		josephus(newArray, m, n, ret);
}

int main(){
	int n, m;
	cin>>n>>m;
	const int ret = n;
	int a[n];
	for(int i = 0; i < n; ++i){
		a[i] = i + 1;
	}
	josephus(a, m, n, ret);
	return 0;
} 
2024/11/18 23:10
加载中...