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]));
}
}