c#排队报数解法

最近在读MICHAEL MCMILLAN的《c#data structure》一书,发现书后习题并没有解答,于是自己做了下,很少基于c#写数据结构,所以觉得还是很有意思的。好,闲话少说,进入题目.

题目:

According to legend, the first century Jewish historian, Flavius Josephus,was captured along with a band of 40 compatriots by Roman soldiers duringthe Jewish–Roman war. The captured soldiers decided that they preferredsuicide to being captured and devised a plan for their demise. They were to form a circle and kill every third soldier until they were all dead. Joseph and one other decided they wanted no part of this and quickly calculated where they needed to place themselves in the circle so that they would bothsurvive. Write a program that allows you to place n people in a circle and specify that every m person will be killed. The program should determinethe number of the last person left in the circle.

呵呵,题目很长,其实意思很简单:就是n个人围一圈报数,已1到m报数,凡报道m的人退出圈子,最后留下的人是那个。

这是我的算法:


ContractedBlock.gif ExpandedBlockStart.gif Code
static void LastLeaver(int nums, int count)
        {
            
int[] num = new int[nums];        
            
for (int index = 0; index< num.Length; index++)
            {
                num[index] 
=index + 1;//从1-nums给每个人编号
            }
            
int m = 0//m为退出人数            
            int i = 0;//i为每次循环时的计数变量
            int k = 0;//k为按1,2,3count报数时的计数变量
            while (m < nums - 1)
            {
                
if (num[i] != 0)
                {
                    k
++;            
                }
                
if (k == count)
                {

                    num[i] 
= 0;
                    Console.WriteLine(
"{0}出列!", i + 1);
                    k 
= 0;
                    m
++;
                }
                i
++;
                
if (i == nums) i = 0;
            }
            
for (int j = 0; j < nums; j++)
            {
                
if (num[j] != 0)
                {
                    Console.WriteLine(
"最后留下的是:{0}", j + 1);
                }
            }
        }



 很简单吧。算法还有个改进,比如说要求留下最后2个人,3个人等,我们还可以改进下,设置一个leave变量,代表最后要留下的人数,代码如下:

ContractedBlock.gif ExpandedBlockStart.gif Code
 static void LastLeaver(int nums, int count,int leave)
        {
            
int[] num = new int[nums];
            
            
for (int index = 0; index< num.Length; index++)
            {
                num[index] 
=index + 1;//从1-nums给每个人编号
            }

            
int m = 0//m为退出人数
            
            
int i = 0;//i为每次循环时的计数变量

            
int k = 0;//k为按1,2,3count报数时的计数变量
            while (m < nums -leave)
            {
                
if (num[i] != 0)
                {
                    k
++;            
                }
                
if (k == count)
                {

                    num[i] 
= 0;
                    Console.WriteLine(
"{0}出列!", i + 1);
                    k 
= 0;
                    m
++;
                }
                i
++;
                
if (i == nums) i = 0;
            }
            
for (int j = 0; j < nums; j++)
            {
                
if (num[j] != 0)
                {
                    Console.WriteLine(
"最后留下的是:{0}", j + 1);
                }
            }
        }
    }

 

 

 

转载于:https://www.cnblogs.com/healer_zll/archive/2008/10/16/1312359.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值