【題解】ZeroJudge b551: 2. 羅馬羅盤

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=b551

#include <iostream>
#include <vector>
using namespace std;

int n, k;
vector <int> v;

int main() {
    while (cin >> n >> k){
        v.clear();
        for (int i = 1; i <= n; i++){
            v.push_back(i);
        }
        int pos = -1;
        for (int i = n; i >= 2; i--){
            pos = (pos+k) % i;
            int nxt = pos+k;
            if (nxt >= i) nxt = (nxt-i) % (i-1);
            if (nxt >= pos && (pos+k) >= i) nxt++;
            v[pos] = v[nxt];
            v.erase(v.begin()+nxt);
            if (nxt < pos) pos--;
        }
        cout << 1+(1-v[0]+n)%n << "\n";
    }
}
分享本文 Share with friends