1 国际象棋
本题纯纯归纳总结,对于这种题目,我们应该先针对每一种情况列举出多种用例,这样才能提炼出判断逻辑,而不是抓住一个用例开始随便套规律。这题我卡在判断两步
const readline = require('readline').createInterface({
input: process.stdin,
ouput: process.stdout
})
let input = []
readline.on('line', (line) => {
input.push(line)
})
readline.on('close', () => {
let t = input[0]
for (let i = 1; i <= t; i ++) {
let [x1, y1, x2, y2] = input[i].split(' ').map(Number)
// 一步象
if (Math.abs(x1 - x2) === Math.abs(y1 - y2)) {
console.log(1)
continue
}
// 一步马
if (Math.abs(x1 - x2) === 1 && Math.abs(y1 - y2) === 2 ||
Math.abs(x1 - x2) === 2 && Math.abs(y1 - y2) === 1
) {
console.log(1)
continue
}
// 两步象
if ((Math.abs(x1 - x2) + Math.abs(y1 - y2)) % 2 === 0) {
console.log(2)
continue
}
// 一象一马
if (Math.abs(x1 - x2 + 3) === Math.abs(y1 - y2) ||
Math.abs(x1 - x2 - 3) === Math.abs(y1 - y2) ||
Math.abs(x1 - x2 + 1) === Math.abs(y1 - y2) ||
Math.abs(x1 - x2 - 1) === Math.abs(y1 - y2)
) {
console.log(2)
continue
}
console.log(3)
}
})
2 挑战boss
简单模拟,感觉难度不如国际象棋(
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
let input = []
readline.on('line', (line) => {
input.push(line)
})
readline.on('close', () => {
let [n, a, b] = input[0].split(' ').map(Number)
let str = input[1]
let k = 0
let sum = 0
for (let i = 0; i < n; i ++) {
sum += (a + k * b)
if (str[i] === 'o') {
k ++
} else {
k = 0
}
}
console.log(sum)
})
3 构造二阶行列式
20 * 20 * 20 * 20,暴力搜索不会超时,第一眼看上去还以为很难qwq
刚开始接受x的代码这样写的:
let x = input[0]
这样得到的x是string类型,导致一直没有输出,记得进行类型转化!
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
let input = []
readline.on('line', (line) => {
input.push(line)
})
readline.on('close', () => {
let x = Number(input[0])
let flag = 0
outerLoop:
for (let a = 1; a <= 20; a ++) {
for (let b = 1; b <= 20; b ++) {
for (let c = 1; c <= 20; c ++) {
for (let d = 1; d <= 20; d ++) {
if (a * d - b * c === x) {
console.log(`${a} ${b}`)
console.log(`${c} ${d}`)
flag = 1
break outerLoop
}
}
}
}
}
if (flag === 0) {
console.log(-1)
}
})

Comments NOTHING