猴子選大王

#include <iostream>
#include <list>
#include <vector>
using namespace std;
const int maxn = 100;
int data[maxn];


int main() {
    int t;
    cin>>t;
    while(t--) {
        int n,m;
        cin>>n>>m;
        list<int> L;
        for (int i=1;i<=n;i++) L.push_back(i);
        list<int>::iterator it;
        it = L.begin();
        int cnt=1;
        while(L.size()!= 1) {
             if(it==L.end()) 
             it = L.begin();
             if(cnt==m) {
                it=L.erase(it);
                if(it==L.end()) 
                it = L.begin();
                cnt=1;
             } else {
                it++;
                cnt++;
             }
        }
        cout<<*it<<endl; 
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章