java数组实现约瑟夫

/**
 * <约瑟夫问题><br>
 * <规则:从第二个开始报数,一共报三下,第三个报的人出去。如果从2开始,顺序为2、3、4 。那么4就要出去>
 * 
 * @author snowday88
 */
public class ShouPa
{
    private int[] shouPa;
    
    // 要报的数
    private int num;
    
    public ShouPa(int size, int num)
    {
        shouPa = new int[size];
        for (int i = 0; i < size; i++)
        {
            shouPa[i] = i + 1;
        }
        this.num = num;
    }
    
    // 获得最后一个出列的
    public int getTheLast()
    {
        // 打印数组
        printArray();
        // 得到数组的长度
        int length = shouPa.length;
        // 定义index 从第二个数开始
        int index = 1;
        // 记录出列的个数
        int chuLie = 0;
        // 当数组只剩一个数的时候就停止循环
        while ((chuLie + 1) != length)
        {
            // 报数三下
            for (int i = 0; i < num; i++)
            {
                // 用来循环
                int cursor = index + i;
                
                // 判断是否越界了,如果越界就当作循环。
                if (cursor > (length - 1))
                {
                    cursor = (index + i) % (length - 1) - 1;
                    
                }
                
                // 如果当前数组元素是0则跳到下一个
                while (shouPa[cursor] == 0)
                {
                    index++;
                    cursor = index + i;
                    
                    if (index > length - 1)
                    {
                        index = (index % (length - 1)) - 1;
                    }
                    
                    if (cursor > (length - 1))
                    {
                        cursor = (cursor) % (length - 1) - 1;
                        
                    }
                    
                }
                
                // 出列
                if (i == 2)
                {
                    // 出列的数置为0
                    shouPa[cursor] = 0;
                    // index为出列的下一数的位置
                    index = index + num;
                    if (index > length - 1)
                    {
                        index = (index % (length - 1)) - 1;
                    }
                    
                    // 出列数加一
                    chuLie++;
                }
            }
            
        }
        
        // 得到最后一个数
        for (int i = 0; i < shouPa.length; i++)
        {
            if (shouPa[i] > 0)
            {
                System.out.println();
                System.out.print("最后出列的数是: ");
                return shouPa[i];
            }
        }
        return 0;
    }
    
    // 打印数组
    public void printArray()
    {
        for (int i : shouPa)
        {
            System.out.print(i + " ");
        }
    }
    
    public static void main(String[] args)
    {
        System.out.println(new ShouPa(7, 3).getTheLast());
    }
}

 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值