USTCOJ 1366,題見此處:http://acm.ustc.edu.cn/ustcoj/problem.php?id=1366
題目翻譯自soj.me,鏈接地址:http://soj.me/7675
#include <stdio.h>
int increment(int num, int mod)
{
num = (num + 1) % mod;
return num;
}
//每人從碗裏取一個石子,返回的i,是拿到碗,且碗中石子爲空的人的編號。
int take(char* peo, int bowl, int i, int n)
{
while (bowl--)
{
++peo[i];
i = increment(i, n);
}
return i;
}
main()
{
int n, p;
while (scanf("%d%d", &n, &p), n + p)
{
char peo[60] = {0};
int i, bowl;
i = 0; bowl = p;
while (1)
{
i = take(peo, bowl, i, n);
while (peo[i] == 0) //不斷將空碗下傳。
i= increment(i, n);
bowl = peo[i]; peo[i] = 0; //將標號爲i的人面前的石子放入碗中。
if (bowl == p) //若石子等於p石子數,表明已經完成選舉
break;
i = increment(i, n); //選舉爲完成,繼續下一輪,將碗傳到下一個人手裏
}
printf("%d\n", i);
}
return 0;
}