• 12月8号报数字游戏


    题目:

    n个人围成一圈,顺序排号。从第一个人开始报数(从13报数),凡报到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(" ");

        return 0;

    }

  • 相关阅读:
    MySQL的小Tips
    Linux中Eclipse下搭建Web开发环境
    SaaS的那些事儿
    典型的软件过程模型
    浅谈「敏捷」开发
    软件工程的本质
    堆和优先队列
    c 判断文件或文件夹是否存在,多种方法, 为什么从一开始就不直接来个统一的呢?
    大江河流尽
    jpg、png格式的图片转换成webp后颜色失真的问题
  • 原文地址:https://www.cnblogs.com/hmzxwky/p/5030611.html
Copyright © 2020-2023  润新知