个人技术分享

#include <stdio.h>
#define MAX_CALLER 3

void main() {
    int j = 0;
    int p_total;//人数
    int p_caller = 0;//每3人循环计数:1,2,3
    int 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,3

        if (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; //如果数到队尾,重新回到队伍排头
        else
            j++; //队列依次向后轮转
    }

    for (int k = 0; k < p_total; k++) {
        if (people[k] != 0)
            printf("第%d位游戏者,胜出\n", people[k]);
    }
}

运行结果:

本章C语言经典例题合集:http://t.csdnimg.cn/FK0Qg