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
| function combo(N) { let pairs = [] for (let a = 1; a <= N; a++) { for (let b = a + 1; b <= N; b++) { pairs.push([a, b, 0]) } } return pairs } function isConflict(A, B) { return A == B || A[0] == B[0] || A[0] == B[1] || A[1] == B[0] || A[1] == B[1] }
function match(pairs) { let vs = [], P = pairs.length for (let i = 0; i < P; i++) { let A = pairs[i] for (let j = i + 1; j < P; j++) { let B = pairs[j] if (isConflict(A, B)) continue vs.push([A, B]) } } return vs }
function main(N, M) { if (N < 4) return console.error(`人数不足(${N}<4)`) if (N > 20) return console.error(`人数太多啦!`) let plays = new Array(N).fill(0) function tires(v) { let A = v[0], B = v[1] return (A[2] + 1) * (plays[A[0] - 1] + plays[A[1] - 1]) + (plays[B[0] - 1] + plays[B[1] - 1]) * (B[2] + 1) } let pairs = combo(N) let allvs = match(pairs) let vs = [] console.log(`${N}人,${pairs.length}队,${M>0?('打'+M+'局'):''}对阵:`) for (let i = 0; allvs.length > 0 ; i++) { let v = allvs.shift() let A = v[0], B = v[1] A[2]++, plays[A[0] - 1]++, plays[A[1] - 1]++ B[2]++, plays[B[0] - 1]++, plays[B[1] - 1]++ console.log(`${i + 1}. (${A[0]},${A[1]}) x (${B[0]},${B[1]})`) vs.push(v) if (!M || i+1 >= M){ if (pairs.every(p => p[2]>0)){ if (plays.every(c => c==plays[0])) break } } allvs = allvs.sort((a, b) => tires(a) - tires(b)) } console.log(`每人上场${plays[0]}次.\n`) return vs }
main(3),main(4),main(5) main(6),main(6, 15) main(7),main(7, 21) main(8),main(8, 16),main(8, 18) main(9),main(9, 27) main(10),main(100)
|