import java util Random public class Main private static String format

  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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.util.Random;
public class Main {
private static String format(int value) {
if (value < 10) {
return "[0" + value + "]";
}
return "[" + value + "]";
}
private static double random() {
return new Random().nextDouble();
}
private static int linearDistribution(int a, int b) {
return (int) Math.round(a + random() * (b - a));
}
private static int triangularDistribution(double a, double b, double c) {
double Pl = (c - a) / (b - a);
if (random() < Pl) {
return (int) Math.round(a + (c - a) * Math.sqrt(random()));
} else {
return (int) Math.round(c + (b - c) * (1 - Math.sqrt(random())));
}
}
private static int normalDistribution(double m, double s) {
double eta = 0.0;
for (int i = 0; i < 12; i++) {
eta += random();
}
eta = eta - 6;
return (int) Math.round(m + s * eta);
}
private static class Statistic {
private int mDenied;
private int[] Share;
private int mTripNumber;
private Trip mTrip;
private Statistic() {
Share = new int[2];
mTrip = new Trip();
}
private void run() {
for (int index = 0; index < 16; index++) {
mTripNumber = normalDistribution(13, 0.5) - 1;
Share[1] += mTripNumber;
mTripNumber--;
if (mTrip.start()) {
mDenied++;
}
System.out.print(format(mTrip.mCurrent) + " -> ");
for (int tripIndex = 0; tripIndex < mTripNumber; tripIndex++) {
if (mTrip.stop()) {
mDenied++;
} else {
if (mTrip.mCurrent == 0) {
Share[0]++;
}
}
System.out.print(format(mTrip.mCurrent));
}
System.out.print(" -> Кінець");
mTrip.end();
System.out.print('\n');
}
}
}
private static class Trip {
private int mN;
private int mCapacity;
private int mCurrent;
private int mSupply;
private Trip() {
mN = 12;
mCapacity = 16;
mCurrent = 0;
mSupply = normalDistribution(12, 0.75);
}
private void end() {
mCurrent = 0;
}
private boolean start() {
int load = linearDistribution(1, mN);
mSupply = normalDistribution(12, 0.8);
if (load > mCapacity) {
mCurrent = mCapacity;
return true;
} else {
mCurrent = load;
}
return false;
}
private boolean stop() {
mCurrent -= triangularDistribution(0, mCurrent / 2, 0);
mCurrent += getEntered();
if (mCurrent > mCapacity) {
mCurrent = mCapacity;
return true;
}
return false;
}
private int getEntered() {
if (mSupply <= 0) {
return 0;
}
int load = normalDistribution(1.2, 0.4);
if (load <= 0) {
return 0;
}
mSupply -= load;
return load;
}
}
public static void main(String[] args) {
Statistic statistic = new Statistic();
statistic.run();
System.out.print("\n\nКількість відмов: " + statistic.mDenied + "\n" +
"Доля часу, яку маршрутка їде пуста: " + (statistic.Share[0] / statistic.Share[1]));
}
}