美文网首页
Joseph Ring

Joseph Ring

作者: 綿綿_ | 来源:发表于2019-04-01 21:49 被阅读0次
package Joseph;
import java.util.Scanner;
public class JosephRing {
    static final int Num=41;
    static final int Kill=3;
    private static Scanner input;
    
    static void Joseph(int alive)
    {
        int[] man=new int[Num];
        int count=1;
        int i=0,pos=-1;
        while(count<=Num)
        {
            do {
                pos=(pos+1)%Num;
                if(man[pos]==0)
                    i++;
                if(i==Kill)
                {
                    i=0;
                    break;
                }
            }while(true);
            man[pos]=count;
            System.out.printf("the %2d-th man killed himself,"
                    +"the number is %2d :",pos+1,man[pos]);
            if(count%2==1)
            {
                System.out.print("-> ");
            }
            else
            {
                System.out.print("-> \n");
            }
            count++;
        }
        System.out.print("\n");
        System.out.printf("those %d people who wanna live should stand in:\n", alive);
        alive=Num-alive;
        for(i=0;i<Num;i++)
        {
            if(man[i]>alive)
            {
                System.out.printf("the original number:%d,the Josephring number:%d\n", i+1,man[i]); 
            }
        }
        System.out.print("\n");
    }
    public static void main(String[] args)
    {
        int alive;
        input = new Scanner(System.in);
        System.out.println("enter the number that need to keep alive");
        alive=input.nextInt();
        Joseph(alive);
    }
    
    

相关文章

网友评论

      本文标题:Joseph Ring

      本文链接:https://www.haomeiwen.com/subject/ducbbqtx.html