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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 终于补完了《霍比特人》与《魔戒》 结尾与我而言却是悲剧 人类统治世界来临 精灵最终离开中土大地 连最后一个次...
- In a ring surrounded by spikes, in order to get the gem, ...