-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise7_21.java
113 lines (105 loc) · 3.12 KB
/
Exercise7_21.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import java.util.*;
public class Exercise7_21 {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of balls to drop:");
int ballNum = input.nextInt();
System.out.println("Enter the number of slots in the bean machine:");
int slotNum = input.nextInt();
System.out.println(" ");
int[] slots = new int[slotNum];
int[] balls = new int[ballNum];
for (int i = 0; i < ballNum; i++)
{
int p;
balls[i] = showPath(7);
p = balls[i];
slots[p]++;
}
System.out.println(" ");
// int maxRow = 0;
// for (int j = 0; j < slotNum; j++)
// {
// if (maxRow < slots[j])
// maxRow = slots[j];
// }
// int[][] outputArray = new int[slotNum][maxRow];
// for (int a = 0; a < slotNum; a++)
// {
// if (slots[a] == 0)
// {
// for (int b = 0; b < maxRow; b++)
// {
// outputArray[a][b] = 0;
// }
// }
// else
// {
// for (int rows = maxRow; rows >= 0; rows--, slots[a]--)
// {
// if (slots[a] >= 0)
// outputArray[a][rows] = 1;
// else
// outputArray[a][rows] = 0;
// }
// }
// }
ShowMachine(ballNum, slotNum, slots);
}
public static int showPath(int n)
{
int[] path = new int[n];
int rightNum = 0;
for (int i = 0; i < n; i++)
{
path[i] = (int)((Math.random() * 10) % 2);
if (path[i] == 0)
{
System.out.print("R");
rightNum++;
}
else
{
System.out.print("L");
}
}
System.out.println(" ");
return rightNum;
}
// public static void showBall(int[][] outputArray, int slotNum)
// {
//
// for(int i = slotNum - 1; i >= 0; --i)
// {
// for(int j = 0; j < slotNum; ++j)
// {
// if(outputArray[i][j] == 0)
// System.out.print(" ");
// else
// System.out.print(0);
// }
// System.out.println();
// }
//
// }
public static void ShowMachine(int ballNum, int slotNum, int[] slots)
{
StringBuilder str;
int flag;
for (int i = ballNum; i > 0; i--) { //模拟二维数组,当count[j]超过i,证明此行此列存在值
flag = 0;
str = new StringBuilder();
for (int j = 0; j < slotNum; j++) {
if (slots[j] >= i) {
str.append("O");
flag = 1;
}
else
str.append(" ");
}
if (flag == 1) //使用str以及flag为了去除多余的空行
System.out.println(str);
}
}
}