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, ...