小猫钓鱼——栈、队列的应用(C++)http://t.csdnimg.cn/12oUN感谢博友
/* 小猫钓鱼 */
const getResult = (inputArray) => {
let mm = inputArray[1].split(' ').map(Number);
let xx = inputArray[2].split(' ').map(Number);
let sum = 0;
let stack = [];
function isWin(tmp, arr, cat) {
console.log(`${cat}: ${tmp} | ${arr.join(' ')} \t\t\=> ${stack.join(' ')}`);
const index = 0;
const lastIndex = stack.lastIndexOf(tmp);
if (lastIndex && lastIndex !== index) {
// 本轮获胜
arr.splice(arr.length, 0, ...stack.splice(index, lastIndex + 1));
}
console.log(`${cat}: ${tmp} | ${arr.join(' ')} \t\t\=> ${stack.join(' ')}\n`);
}
while (mm.length && xx.length) {
const mmTmp = mm.shift(); // mm 出牌
stack.unshift(mmTmp);
isWin(mmTmp, mm, 'mm');
const xxTmp = xx.shift(); // xx 出牌
stack.unshift(xxTmp);
isWin(xxTmp, xx, 'xx');
sum++;
}
console.log(sum);
console.log(mm.length === 0 ? 'xx' : 'mm');
console.log(mm.length === 0 ? xx.join(' ') : mm.join(' '));
};
getResult(['6', '2 4 1 2 5 6', '3 1 3 5 6 4']);
// 25
// xx
// 6 5 2 3 4 1