网站首页布局设计互联网行业前景分析
web/
2025/9/29 4:51:13/
文章来源:
网站首页布局设计,互联网行业前景分析,云南省建设注册考试中心网站,北京交通管制信息网站10个人围成一圈#xff0c;从1到10编号#xff0c;从1开始数#xff0c;数到3或3的倍数的位置#xff0c;则该位置的人出局#xff0c;求最后剩下哪一个号#xff1f; 数组解法#xff1a; 数组存放数组#xff1a;a[10]存在1到10编号人 数组遍历到尾部又从头遍历…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[] anew int[10];9 for(int i0;i10;i){
10 a[i]i1;
11 }
12 System.out.println(运行到这里);
13 int globalNum0;
14 int counta.length;
15 while(count1){//退出外循环的条件
16 for(int i0;ia.length;i){
17
18 while(a[i]0){//跳过已出局的人globalNum不统计
19 i;
20 }
21 globalNum;
22 if(globalNum%30){
23 a[i]0;
24 count--;
25 }
26 }
27 }
28 System.out.println();
29 for(int i0;ia.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.indexindex;
12 nextnull;
13 }
14
15 }
16
17 public static void main(String[] args) {
18 链表解法 rnew 链表解法();
19 链表解法.Node nr.new Node();
20 链表解法.Node headr.new Node(1);//链表头节点
21 链表解法.Node nowhead;
22 int len0;//初始化的时候记录链表长度
23 //初始化链表
24 for(int i2;i10;i){
25 链表解法.Node noder.new Node(i);
26 //Node是r的成员属性当new Node(1),又new Node(1)时
27 //如果合法则调用new Node(1)时不知道调用哪个
28
29 now.nextnode;
30 System.out.print(now.index、);
31 nownow.next;
32 if(i10)
33 System.out.println(now.index);
34 len;
35 }
36
37 链表解法.Node presenthead;
38 while(present.next!null){
39 System.out.print(present.index、);
40 presentpresent.next;
41 if(present.nextnull){
42 present.nexthead;
43 System.out.println(present.index);
44 System.out.println(构建循环链表成功present.index的下一个节点值:present.next.index);
45 break;
46 }
47 }
48 链表解法.Node present1head;
49 int count1len;
50 System.out.println(再次打印链表);
51 while(present1.next!nullcount11){
52 System.out.print(present1.index、);
53 present1present1.next;
54 if(present1.index10){
55 System.out.println(present1.index);
56 }
57 count1--;
58 }
59 //约瑟夫斯问题求解
60 链表解法.Node phead;
61 int countlen;
62 int global30;
63 while(true){
64 while(p.index!0){
65 global3;
66 System.out.print([global3、p.index]);
67 if(global3%30){
68 System.out.println(global global3删除 p.index);
69 p.index0;
70 count--;
71 }
72 if(count2){
73 break;
74 }
75 pp.next;
76 }
77 pp.next;
78 if(count2){
79 break;
80 }
81 }
82
83 链表解法.Node present2head;
84 int count2len;
85
86 while(present2.next!nullcount21){
87 System.out.print(present2.index、);
88 present2present2.next;
89 count2--;
90 }
91 }
92
93 } View Code 转载于:https://www.cnblogs.com/dengrong/p/8520291.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/83712.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!