0085 : Joseph's Potato
解説
Joseph's Potatoというゲームのルールに従って、最後に残る人の番号を答える問題。
一人になるまでループを使ってシミュレーションすればいい。
どの番号が残っているかという情報を記憶する配列があると楽。
プログラム
C
C++
|
+
|
... |
#include <iostream>
using namespace std;
int main() {
int m, n;
while (cin >> m >> n, m || n) {
int p[1000] = {0}, cnt = 1, res = m, num;
for (int i = 0; ; i++) {
if (res == 1 && p[i%m] == 0) {
num = i%m;
break;
}
if (p[i%m]) continue;
if (cnt == n) {
p[i%m] = 1;
res--;
cnt = 1;
} else {
cnt++;
}
}
cout << num+1 << endl;
}
return 0;
}
|
Java
最終更新:2013年01月14日 15:44