#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;
}
'알고리즘 > C++' 카테고리의 다른 글
47. 봉우리 (2차원 배열 탐색) (0) | 2021.04.05 |
---|---|
46. 멀티태스킹 (0) | 2021.04.05 |
44. 마구간 정하기 (이분검색 응용 : 결정 알고리즘) (0) | 2021.03.29 |
43. 뮤직비디오 (이분검색 응용 : 결정 알고리즘) (0) | 2021.03.29 |
42. 이분검색 (0) | 2021.03.29 |
댓글