程序书算法精题–JS版
- 两个乒乓球队进行比赛, 各出三人. 甲队为a, b, c三人, 乙队为x, y, z三人. 已抽签决定比赛名单. 有人向队员打听比赛的名单. a说他不和x比, c说他不和x, z比, 请编程序找出三队赛手的名单.
分析
- a != x
- c != x && c != z => c == y
- 由1和2, 推出a == z; b == x
字母转为ASCII码, 用charCodeAt()
ASCII码转字符:用fromCharCode();
源码
// 声明i, j, k分别为a, b, c的对手
let i, j, k;
// x, y, z转换为ASCII码
let X = 'x'.charCodeAt(0);
let Y = 'y'.charCodeAt(0);
let Z = 'z'.charCodeAt(0);
for (i = X; i <= Z; i++) {
for (j = X; j <= Z; j++) {
for (k = X; k <= Z; k++) {
if (i == X || j == i || k == X || k == Z || k == i ||k == j) {
continue;
}
console.log('a:' + String.fromCharCode(i));
console.log('b:' + String.fromCharCode(j));
console.log('c:' + String.fromCharCode(k));
}
}
}
console