约瑟夫斯问题-java版数组解法和链表解法

10我的围成一圈,从1到10编号,从1开始数,数到3或3的倍数的位置,则该位置的人出局,求最后剩下哪个号?node

 

数组解法:数组

数组存放数组:a[10]存在1到10编号人ide

数组遍历到尾部又从头遍历:遍历数组--->内循环。设置一个外循环--->使得数组能够从头遍历,并且从1开始的的递增数字。while循环实现this

数到3或3的倍数的位置出局:a[i]=0;spa

退出外部循环的条件:只剩下一人。code

具体代码以下:blog

 1 package algorithm.约瑟夫斯;  2 
 3 public class 数组解法 {  4 
 5     
 6     public static void main(String[] args) {  7         // TODO 自动生成的方法存根
 8         int[] a=new int[10];  9         for(int i=0;i<10;i++){ 10             a[i]=i+1; 11  } 12         System.out.println("运行到这里"); 13         int globalNum=0; 14         int count=a.length; 15         while(count>1){//退出外循环的条件
16             for(int i=0;i<a.length;i++){ 17                 
18                 while(a[i]==0){//跳过已出局的人,globalNum不统计
19                     i++; 20  } 21                 globalNum++; 22                 if(globalNum%3==0){ 23                     a[i]=0; 24                     count--; 25  } 26  } 27  } 28  System.out.println(); 29         for(int i=0;i<a.length;i++){ 30             if(a[i]!=0) 31  System.out.print(a[i]); 32  } 33  } 34 
35 }
View Code

 单向链表解法(不删除元素):it

 1 package algorithm.约瑟夫斯;  2 
 3 public class 链表解法 {  4     
 5     public class Node{  6         public int index;  7         public Node next;  8         
 9         public Node(){} 10         public Node(int index){ 11             this.index=index; 12             next=null; 13  } 14         
15  } 16     
17     public static void main(String[] args) { 18         链表解法 r=new 链表解法(); 19         链表解法.Node n=r.new Node(); 20         链表解法.Node head=r.new Node(1);//链表头节点
21         链表解法.Node now=head; 22         int len=0;//初始化的时候记录链表长度 23         //初始化链表
24         for(int i=2;i<=10;i++){ 25             链表解法.Node node=r.new Node(i); 26             //Node是r的成员属性,当new Node(1),又new Node(1)时, 27                                   //若是合法,则调用new Node(1)时不知道调用哪一个
28             
29             now.next=node; 30             System.out.print(now.index+""); 31             now=now.next; 32             if(i==10) 33                 System.out.println(now.index); 34             len++; 35  } 36         
37         链表解法.Node present=head; 38         while(present.next!=null){ 39             System.out.print(present.index+""); 40             present=present.next; 41             if(present.next==null){ 42                 present.next=head; 43                 System.out.println(present.index); 44                 System.out.println("构建循环链表成功!"+present.index+"的下一个节点值:"+present.next.index); 45                 break; 46  } 47  } 48         链表解法.Node present1=head; 49         int count1=len; 50         System.out.println("再次打印链表:"); 51         while(present1.next!=null&&count1>=1){ 52             System.out.print(present1.index+""); 53             present1=present1.next; 54             if(present1.index==10){ 55                 System.out.println(present1.index); 56  } 57             count1--; 58  } 59         //约瑟夫斯问题求解
60         链表解法.Node p=head; 61         int count=len; 62         int global3=0; 63         while(true){ 64             while(p.index!=0){ 65                 global3++; 66                 System.out.print("["+global3+""+p.index+"]"); 67                 if(global3%3==0){ 68                    System.out.println("global= "+global3+"删除 "+p.index); 69                    p.index=0; 70                    count--; 71  } 72                 if(count<2){ 73                     break; 74  } 75                 p=p.next; 76  } 77             p=p.next; 78             if(count<2){ 79                 break; 80  } 81  } 82         
83         链表解法.Node present2=head; 84         int count2=len; 85         
86         while(present2.next!=null&&count2>1){ 87             System.out.print(present2.index+""); 88             present2=present2.next; 89             count2--; 90  } 91  } 92 
93 }
View Code
相关文章
相关标签/搜索