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
| public class Loading {
public static void main(String[] args) { float[] w = {1, 3, 10, 5, 3, 5, 6, 8, 1, 2, 12}; int n = w.length; Boolean[] x = new Boolean[n]; float c = 30; Loading(x, w, c, n); for (int i = 0; i < n; i++) { System.out.printf("%3d: weight: %-6.2f load: %6b\n", i + 1, w[i], x[i]); } }
public static void Loading(Boolean[] x, float[] w, float c, int n) { int[] t = new int[n]; Sort(w, t, n);
for (int i = 0; i < n; i++) { x[i] = false; } for (int i = 0; i < n && w[t[i]] <= c; i++) { x[t[i]] = true; c -= w[t[i]]; } }
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; } } } } }
|