定义双向循环链表,解决如下的问题:
有 n 个孩子顺时针站成一圈,其编号为:1,2,3,... n
从1号孩子开始顺时针数数,每个孩子数一个数,遇到7的倍数或数字中含有7,则该孩子不出声,只拍一下手,数数的方向逆转,下一个孩子数下一个数字。
例如:1,2,3...6,拍手,则接下来,6号孩子数8,5号孩子数9 ....
结果:
有 n 个孩子顺时针站成一圈,其编号为:1,2,3,... n
从1号孩子开始顺时针数数,每个孩子数一个数,遇到7的倍数或数字中含有7,则该孩子不出声,只拍一下手,数数的方向逆转,下一个孩子数下一个数字。
例如:1,2,3...6,拍手,则接下来,6号孩子数8,5号孩子数9 ....
请模拟该过程,如果有20个孩子,求哪个孩子要数100。
public class Node<T> {
T data;
Node prior;
Node next;
Node(T data, Node<T> prior, Node<T> next) {
this.data = data;
this.prior = prior;
this.next = next;
}
Node() {
this.next = null;
this.prior = null;
}
public void insertFirst(Node<T> node) {
node.next = this.next;
this.next = node;
this.next.next.prior = node;
node.prior = this;
}
/*public void deleteFirst() {
this.next = this.next.next;
this.next.prior = this;
}
public void tranversDList() {
Node<T> node = this.next;
while (node != null) {
System.out.println("The data of this node is " + node.data);
node = node.next;
}
}*/
public void showNode() {
System.out.println("The data of this node is:" + this.data);
}
}
import java.util.Scanner;
public class Test {
//判断方向
static boolean direction(int number,boolean direct)
{
if (number%7==0)
direct=!direct;
else
while (number>10)
{
int j=number/10;
number%=10;
if (number==7)
{
direct=!direct;
break;
}
number=j;
}
return direct;
}
//主函数
public static void main(String[] args) {
boolean clockwise = true;
Node<Integer> dhead = new Node<Integer>(1, null, null);
dhead.prior = dhead;
dhead.next = dhead;
Scanner sc = new Scanner(System.in);
int num = 0;
while (num < 10) {
System.out.print("游戏人数(>=10):");
num = sc.nextInt();
}
for (int i = num; i > 1; i--) {
Node<Integer> node = new Node<Integer>(i, null, null);
dhead.insertFirst(node);
}
Node<Integer> pointer = new Node<Integer>();
pointer = dhead;
System.out.print("数到第几个数:");
num = sc.nextInt();
int i = 1;
while (i <= num) {
System.out.print(i+"-->");
pointer.showNode();
clockwise = direction(i, clockwise);
if (clockwise == true)
pointer = pointer.next;
else
pointer = pointer.prior;
i++;
}
}
}
结果:
Conclusion
游戏人数(>=10):20
数到第几个数:100
1-->The data of this node is:1
2-->The data of this node is:2
3-->The data of this node is:3
4-->The data of this node is:4
5-->The data of this node is:5
6-->The data of this node is:6
7-->The data of this node is:7
8-->The data of this node is:6
9-->The data of this node is:5
10-->The data of this node is:4
11-->The data of this node is:3
12-->The data of this node is:2
13-->The data of this node is:1
14-->The data of this node is:20
15-->The data of this node is:1
16-->The data of this node is:2
17-->The data of this node is:3
18-->The data of this node is:2
19-->The data of this node is:1
20-->The data of this node is:20
21-->The data of this node is:19
22-->The data of this node is:20
23-->The data of this node is:1
24-->The data of this node is:2
25-->The data of this node is:3
26-->The data of this node is:4
27-->The data of this node is:5
28-->The data of this node is:4
29-->The data of this node is:5
30-->The data of this node is:6
31-->The data of this node is:7
32-->The data of this node is:8
33-->The data of this node is:9
34-->The data of this node is:10
35-->The data of this node is:11
36-->The data of this node is:10
37-->The data of this node is:9
38-->The data of this node is:10
39-->The data of this node is:11
40-->The data of this node is:12
41-->The data of this node is:13
42-->The data of this node is:14
43-->The data of this node is:13
44-->The data of this node is:12
45-->The data of this node is:11
46-->The data of this node is:10
47-->The data of this node is:9
48-->The data of this node is:10
49-->The data of this node is:11
50-->The data of this node is:10
51-->The data of this node is:9
52-->The data of this node is:8
53-->The data of this node is:7
54-->The data of this node is:6
55-->The data of this node is:5
56-->The data of this node is:4
57-->The data of this node is:5
58-->The data of this node is:4
59-->The data of this node is:3
60-->The data of this node is:2
61-->The data of this node is:1
62-->The data of this node is:20
63-->The data of this node is:19
64-->The data of this node is:20
65-->The data of this node is:1
66-->The data of this node is:2
67-->The data of this node is:3
68-->The data of this node is:2
69-->The data of this node is:1
70-->The data of this node is:20
71-->The data of this node is:1
72-->The data of this node is:2
73-->The data of this node is:3
74-->The data of this node is:4
75-->The data of this node is:5
76-->The data of this node is:6
77-->The data of this node is:7
78-->The data of this node is:6
79-->The data of this node is:5
80-->The data of this node is:4
81-->The data of this node is:3
82-->The data of this node is:2
83-->The data of this node is:1
84-->The data of this node is:20
85-->The data of this node is:1
86-->The data of this node is:2
87-->The data of this node is:3
88-->The data of this node is:2
89-->The data of this node is:1
90-->The data of this node is:20
91-->The data of this node is:19
92-->The data of this node is:20
93-->The data of this node is:1
94-->The data of this node is:2
95-->The data of this node is:3
96-->The data of this node is:4
97-->The data of this node is:5
98-->The data of this node is:4
99-->The data of this node is:5
100-->The data of this node is:6