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
| public class Waiting {
public static void main(String[] args) { float[] w = {1, 3, 10, 5, 3, 5, 6, 8, 1, 2, 12}; int n = w.length; float[] x = new float[n]; int[] order = new int[n]; float T = Waiting(x, w, n, order); for (int i = 0; i < n; i++) { System.out.printf("%3d:服务时间: %5.2f 服务顺序: %3d 等待时间: %5.2f\n", (i + 1), w[i], (order[i] + 1), x[i]); } System.out.printf("平均等待时间为: %.2f分钟", T); }
public static float Waiting(float[] x, float[] w, int n, int[] order) { int[] t = new int[n]; Sort(w, t, n); int temp = 0; float T = 0; for (int i = 0; i < n; i++) { x[t[i]] = temp; temp += w[t[i]]; order[t[i]] = i; } for (int i = 0; i < n; i++) { T += x[i]; } return T / n; }
public static void Sort(float[] w, int[] t, int n) { float[] array = w.clone(); float temp; int index; for (int i = 0; i < n; i++) { t[i] = i; }
for (int i = 0; i < n; i++) { for (int j = 0; j < n - i - 1; j++) { if (array[j] > array[j + 1]) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp;
index = t[j]; t[j] = t[j + 1]; t[j + 1] = index; } } } } }
|