Description
你聽說過約瑟夫問題嗎?問題大致如下:首先n個人圍成一個圈,標記爲1到n號。接着,從1號開始報數(從1開始),然後2號報數,然後3號。。。當有人報到到m時,這個人就要踢出比賽,然後從被踢出的人的下一個人開始,重新報數(從1開始)。這樣經過n-1次後,就只剩下了一個人,問最後剩下的那個人是幾號?
Input
第1行爲T,表示有T組數據;
第2行到第T+1開始,每行輸入n和m,n表示有幾個人,m爲上述的每報數m次就要踢出一個人
1=<n<=100, 1=<m<=100
Output
一個數,表示最後剩下了幾號
Sample Input
2 5 3 6 4
Sample Output
4 5
思路:emm用vector模擬的
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5;
typedef long long LL;
int main(void)
{
LL T;cin>>T;
vector<int>table;
while(T--)
{
table.clear();
LL n,m;cin>>n>>m;
for(LL i=0;i<n;i++) table.push_back(i);
LL pos=0;
while(table.size())
{
pos=(pos+m-1)%table.size();
table.erase(table.begin()+pos);
}
cout<<table[0]+1<<endl;
}
return 0;
}