猴子选大王(java普通数组实现)

本文介绍了一款通过编程实现的“猴子选大王”小游戏,游戏中猴子们通过报数方式淘汰对手直至决出胜者。文章详细展示了使用简单数组实现这一逻辑的过程,并探讨了不同循环条件下的程序行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:猴子选大王的小程序,有M只猴子彼此不服气都想争当猴王,打一架强者为王当然可行。但和平静气的解决才是王道,解决办法就是猴子们围成一圈,从1开始报数,每到规定的数N则将第N个猴子淘汰,然后继续从1开始报数继续按规则淘汰,胜者为王。最后剩下的猴子就是猴王,这样各凭运气,听天由命谁也不要抱怨。


使用简单数组的方式实现:
将编号存放在数组里,将下标作为报数时的移动,count变量为计数循环量,下标从0开始一直循环到数组的末尾时然后设置成数组的开始,使之变成一个循环数组,count变量从1开始,然后每到N时,将数组中对应的编号置0,然后count继续从1开始计数,当遇到编号为0的猴子时,表示该猴子已经淘汰,跳过该猴子下标继续移动。
确定使用的变量:
M=8 表示猴子的总数
N=3 表示淘汰规则的数字
猴子编号存储的数组monkeynum[]
0 1 2 3 4 5 6 7 下标
1 2 3 4 5 6 7 8 编号
king 存储猴王的编号
count 计数,名义上我们心中想的123然后123循环往复


程序代码:

//author:Monkey.D.Echo
//function:
//date:20170115 20:30

public class monkey{

    public static void main (String args[]){
        int M = 8;//猴子总数
        int N = 3;//规定淘汰的数字编号

        int monkeynum[] = new int[M];
        for(int i=0;i<monkeynum.length;i++){
            monkeynum[i]=i+1;
        }

        int count = 1;//循环计数
        int king = 0;//猴子大王的编号
        int index = 0;//数组的下标

        while(M!=0){
            //首先先把单数组变成一个循环数组,就是循环到数组的末尾时,把数组的下标制成数组的开始
            if(index==monkeynum.length){
                index=0;
            }
            //然后判断循环计数的情况
            if(count==N && monkeynum[index]!=0){
                monkeynum[index]=0;
                M--;
                count=1;
            }

            if(monkeynum[index]!=0){
                count++;
            }

            index++;

            for(int i=0;i<monkeynum.length;i++){
                System.out.print(monkeynum[i]+" ");
            }
            System.out.println(" ");
        }
        king = index;
        System.out.println("猴子的大王的编号是"+king);
    }
}

条件设置有问题的情况下出现的错误:

//author:Monkey.D.Echo
//function:
//date:20170115 20:30

public class monkey{

    public static void main (String args[]){
        int M = 8;//猴子总数
        int N = 3;//规定淘汰的数字编号

        int monkeynum[] = new int[M];
        for(int i=0;i<monkeynum.length;i++){
            monkeynum[i]=i+1;
        }

        int count = 1;//循环计数
        int king = 0;//猴子大王的编号
        int index = 0;//数组的下标

        while(M!=4){
            //首先先把单数组变成一个循环数组,就是循环到数组的末尾时,把数组的下标制成数组的开始
            if(index==monkeynum.length){
                index=0;
            }
            //然后判断循环计数的情况;判断条件的设定很重要
            //比如:在这里自己就设置错误
            if(count==N){
            monkeynum[index]=0;
            M--;
            count=1;
            }

            if(count!=N && monkeynum[index]!=0){
            count++;
            }

            index++;

            for(int i=0;i<monkeynum.length;i++){
                System.out.print(monkeynum[i]+" ");
            }
            System.out.println(" ");
        }
        king = index;
        System.out.println("猴子的大王的编号是"+king);
    }
}

当循环条件设置成M!=4时出现错误的原因:
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 0 4 5 6 7 8
1 2 0 4 5 6 7 8
1 2 0 4 5 6 7 8
1 2 0 4 5 0 7 8
1 2 0 4 5 0 7 8
1 2 0 4 5 0 7 8
0 2 0 4 5 0 7 8
0 2 0 4 5 0 7 8
0 2 0 4 5 0 7 8
0 2 0 4 5 0 7 8
0 2 0 4 0 0 7 8 count:3->count:1;index:5
0 2 0 4 0 0 7 8 count:1;index:6
0 2 0 4 0 0 7 8 count:2;index:7
0 2 0 4 0 0 7 8 count:3;index:1
0 2 0 4 0 0 7 8 count:3->count:1;monkeynum[index]=0;index:2;这里差了一个循环

相应的还有其他的方式:

//author:Monkey.D.Echo
//function:
//date:20170115 20:30

public class monkey{

    public static void main (String args[]){
        int M = 8;//猴子总数
        int N = 3;//规定淘汰的数字编号

        int monkeynum[] = new int[M];
        for(int i=0;i<monkeynum.length;i++){
            monkeynum[i]=i+1;
        }

        int count = 1;//循环计数
        int king = 0;//猴子大王的编号
        int index = 0;//数组的下标

        while(M!=0){
            if(index==monkeynum.length) index=0;
            if(count==N&&monkeynum[index]!=0){
                monkeynum[index]=0;
                M--;
                count++;
                if(count==N+1) count=1;
            }
            if(monkeynum[index]!=0){
                count++;
                if(count==N+1) count=1;
            }
            index++;

            for(int i=0;i<monkeynum.length;i++){
                System.out.print(monkeynum[i]+" ");
            }
            System.out.println(" ");
        }
        king = index;
        System.out.println("猴子的大王的编号是"+king);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值