12月8号报数字游戏
题目:
有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问一直到最后结束时的退出顺序?
#include <stdio.h>
#define kKilled -1
int main(int argc, const char * argv[]) {
int totalMan = 0;//记录总人数
int array[100] = {}; //保存编号
int killedNumber = 3;//要杀的编号
int currentNum = 0; //当前报数的编号
int currentKilledTotalMan = 0;//记录当前出局了多少人
printf("请输入人数:");
scanf("%d", &totalMan);
//编号-(从1-n将数字保存到数组里面)
for (int i = 0; i < totalMan; i++) {
array[i] = i+1;
}
//开始杀人
for (int i = 0; i <totalMan; i++) {
//判断i对应的这个人是否已经出局
if (array[i] != -1){
//没有出局
currentNum ++;//报数
//判断当前这个人是否是要出局的编号
if (currentNum == killedNumber){
printf("%d ", array[i]);
//这个人要出局
array[i] = -1;
//改变currentNum的值
currentNum = 0;
//出局人数++
currentKilledTotalMan ++;
if (currentKilledTotalMan == totalMan) {
//出局完了
break;
}
}
}
//判断是否到了结尾
if (i == (totalMan -1)) {
//让索引值从新回到起始点0
i = -1;
}
}
//输出
// for (int i = 0; i < totalMan; i++) {
// printf("%d ", array[i]);
// }
printf("\n");
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步