publicstaticvoidEgyptFrac(int A, int B) { int E, R; System.out.println(A + "/" + B + "="); do { E = B / A + 1;// 求A/B包含的最大埃及分数 System.out.println("1/" + E + "+"); A = A * E - B;// 本行及下一行求A/B - 1/E B = B * E; R = CommFactor.getCommFactor(B, A);// 求A,B最大公约数 if (R > 1) {// 化简A/B A = A / R; B = B / R; } } while (A > 1);// 当A/B不是埃及分数时继续循环 System.out.println("1/" + B); }
欧几里得法求公约数(即辗转相除法)
1 2 3 4 5 6 7 8 9
publicstaticintgetCommFactor(int m, int n) { intr= m % n; while (r != 0) { m = n; n = r; r = m % n; } return n; }
publicstaticvoidMultiMachine(int[] t, int[] d) {// t存储n个作业的处理时间,d存储m台机器的已占用时间 intn= t.length, m = d.length;// m台机器,n个任务 int[][] S = newint[m][n];// S[i]存储机器i处理作业的队列,rear[i]为队尾下标 int[] rear = newint[m]; int i, j, k;
for (i = 0; i < m; i++) {// 初始化S for (j = 0; j < n; j++) { S[i][j] = -1; } }
for (i = 0; i < m; i++) {// 安排前m个作业 S[i][0] = i; rear[i] = 0; d[i] = t[i]; } for (i = m; i < n; i++) {// 依次安排余下的作业 for (j = 0, k = 1; k < m; k++) {// 查找最先空闲的机器 if (d[k] < d[j]) j = k; } rear[j]++; S[j][rear[j]] = i;// 将作业i插入队列S[j] d[j] = d[j] + t[i]; } for (i = 0; i < m; i++) { System.out.println("机器" + i + ":"); for (j = 0; S[i][j] >= 0; j++) { System.out.println("---作业" + S[i][j]); } } }