2016-07-05 Problem Solving►UVa UVa 130 - Roman Roulette Contents 1. Problem2. Solution3. Code Problem題目網址中文網址 Solution直接模擬,看哪個起點可以讓 Josephus 活下來。每次殺掉的位置會再從另一個位置補一個人進去,只需要記 Josephus 現在到哪了。 CodeUVa 13012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576#include<cstdio>#define N 101bool live[N];inline int getNext(int now, int n);int main(){ int n, k; while (scanf("%d%d", &n, &k) && n) { int start, i, pos; for (start = 1; start <= n; start++) { for (i = 0; i <= n; i++) live[i] = true; int now = start; bool flag = true; pos = 1; for (i = 1; i < n&&flag; i++) { int count = k; while (count) { count--; if (!count) { int temp = now; live[now] = false; if (now == pos)//將Josephus殺掉了 { flag = false; break; } //找人去埋,並換去剛殺的位子 for (int j = 0; j < k; j++) temp = getNext(temp, n); //如果有移動到Josephus if (temp == pos) pos = now; live[now] = true; live[temp] = false; } now = getNext(now, n); } } //Josephus還活著 if (live[pos]) break; } printf("%d\n", start); } return 0;}int getNext(int now, int n){ do { now++; if (now > n) now = 1; } while (!live[now]); return now;} Newer UVa 275 - Expanding Fractions Older Machine Learning Foundations - L1 Notes