手记

约瑟夫环问题修改草稿

package yuesefu.oneslide;
import java.util.Scanner;
import java.util.Arrays;
public class Xuesefu {

public static void main(String[] args) {
    Scanner read=new Scanner(System.in);
    System.out.println("罪犯总数:\n");
    int num=read.nextInt();//罪犯总数
    int j=0;//处决名单记录
    int prisoner[]=new int[num];    
    for(int i=0;i<num;i++){
      prisoner[i]=0;//new   
    }
    num=num-1;

    System.out.println("开始的罪犯索引:\n");
    int rannum=read.nextInt();//开始的罪犯索引
    rannum-=1;
    System.out.println("输入处决底数:\n");
    int lucky=read.nextInt();//处决底数数
    read.close();
    int remain=num;//剩余罪犯数
    if(num==1){
        System.out.print("index=0;");

    }else{
        while(remain!=1){
            for(int i=0;i<lucky;i=i+1){
                int bool=0;
                while(bool==0){
                    rannum++;
                    if(rannum==num){
                        if(prisoner[rannum]==0){
                           bool=1;
                           rannum=0;
                        }

                    }else{
                        if(prisoner[rannum]==0){
                            bool=1;
                        }
                    }
                }

            }
            prisoner[rannum]=1;
            System.out.println("被杀死"+rannum);

            if(rannum==num){
                rannum=0;
            }else{
                rannum++;
            }
            remain--;
        }

    }

}

}

1人推荐
随时随地看视频
慕课网APP