본문 바로가기
알고리즘/C++

45. 공주 구하기 (조세퍼스)

by vivi 2021. 4. 5.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main()
{
    //  freopen("input.txt", "rt", stdin);
    int n, k, i, tmp, cnt = 0;
    scanf("%d %d", &n, &k);

    vector<int> a;

    for (i = 1; i <= n; i++)
    {
        a.push_back(i);
    }

    vector<int>::iterator iter = a.begin();

    while (a.size() != 1)
    {

        cnt = (cnt + k - 1) % a.size();

        iter = (a.begin() + cnt);

        a.erase(iter);
    }

    printf("%d", a[0]);

    return 0;
}

 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main()
{
    //  freopen("input.txt", "rt", stdin);
    int n, k, pos = 0, bp = 0, cnt = 0, i;

    scanf("%d %d", &n, &k);

    vector<int> prince(n + 1);

    while (1)
    {
        pos++;
        if (pos > n)
            pos = 1;
        if (prince[pos] == 0)
        {
            cnt++;
            if (cnt == k)
            {
                prince[pos] = 1;
                cnt = 0;
                bp++;
            }
        }
        if (bp == n - 1)
            break;
    }

    for (i = 1; i <= n; i++)
        if (prince[i] == 0)
            printf("%d", i);

    return 0;
}

 

댓글