日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/20 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 约瑟夫斯问题-java版数组解法和链表解法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

10個人圍成一圈,從1到10編號,從1開始數,數到3或3的倍數的位置,則該位置的人出局,求最后剩下哪一個號?

?

數組解法:

數組存放數組:a[10]存在1到10編號人

數組遍歷到尾部又從頭遍歷:遍歷數組--->內循環。設置一個外循環--->使得數組可以從頭遍歷,而且從1開始的的遞增數字。while循環實現

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

退出外部循環的條件:只剩下一人。

具體代碼如下:

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

?單向鏈表解法(不刪除元素):

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

?

轉載于:https://www.cnblogs.com/dengrong/p/8520291.html

總結

以上是生活随笔為你收集整理的约瑟夫斯问题-java版数组解法和链表解法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。