#13128: C++簡易參考解答


shawn2000100 (東華財金)

學校 : 國立東華大學
編號 : 57300
來源 : [27.53.168.5]
最後登入時間 :
2021-09-19 19:53:19
c083. 00130 - Roman Roulette -- UVa130 | From: [1.164.26.95] | 發表日期 : 2017-12-14 23:32

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

int main() {
    int n, k;

    while ( cin >> n >> k && n && k ) {
        if ( 1 == n )  {
            cout << "1" << endl;
            continue;
        }

        int p[n + 1], kill, temp = 1, total = n;

        for ( int i = 1; i <= n; i++ )
            p[i] = i;

        while ( total > 1 ) {
            int count = 0;

            while ( count < k ) {
                if ( temp > n )
                    temp = 1;

                if ( p[temp] )
                    count++;

                temp++;
            }

            kill = temp - 1;
            p[kill] = 0;
            count = 0;

            while ( count < k ) {
                if ( temp > n )
                    temp = 1;

                if ( p[temp] )
                    count++;

                temp++;
            }

            p[kill] = p[temp - 1];
            p[temp - 1] = 0;
            temp = kill + 1;
            total--;
        }


        if ( p[kill] == 1 )
            cout << "1" << endl;
        else
            cout << n - ( p[kill] - 1 ) + 1 << endl;
    }

    return 0;
}
 
ZeroJudge Forum