#include <stdio.h>
#define MAX_CALLER 3void main() {int j = 0;int p_total;//人数int p_caller = 0;//每3人循环计数:1,2,3int p_exit = 0; //退出游戏的人数int people[255] = {0};//参与游戏人员名单printf("请输入参与游戏人数:");scanf("%d", &p_total);for (int i = 0; i < p_total; i++) {people[i] = i + 1;//给参与游戏人员按顺序进行编号}while (p_exit < p_total - 1) {//轮数:因为每次退出一人,则N-1轮淘汰即可完成p_caller++;//报数:1,2,3if (people[j] == 0) {//如果当前位置已经退出p_caller--;//报数回退一个}if (people[j] != 0 && p_caller == MAX_CALLER) {//如果数到3还未退出者people[j] = 0;//标记为退出状态p_caller = 0; //重新报数p_exit++;}if (j == p_total - 1)j = 0; //如果数到队尾,重新回到队伍排头elsej++; //队列依次向后轮转}for (int k = 0; k < p_total; k++) {if (people[k] != 0)printf("第%d位游戏者,胜出\n", people[k]);}
}
运行结果:
本章C语言经典例题合集:http://t.csdnimg.cn/FK0Qg