CCF-CSP考试历年真题题型分类
约瑟夫环
#include <iostream>
#include <queue>
using namespace std;
int n, k;
bool check(int x) // 如果出局则返回true
{
if (x % k == 0 || (x % 10) == k) return true;
return false;
}
int main()
{
cin >> n >> k;
queue<int> q;
for (int i = 1; i <= n; i ++ ) q.push(i);
int j = 1; // 从1开始报数
while (q.size() > 1)
{
int t = q.front();
q.pop();
if (!check(j)) q.push(t); // 如果这个数不用出局,那就再加到队尾
j ++ ;
}
cout << q.front() << endl;
return 0;
}