LIOJ 1046:圈圈叉叉
- 解題想法:一共有八種組合,只要把所有組合列出來,相等就是 'O',不相等就是 'X',剩下的就是平手。
從來沒聽過二維陣列,所以可以寫成
Lines[0][1] -> Lines 第一行第一個元素
,長知識了。I've learned something new today, '2 dimensional array', never heard of it before! That's really convenient to use it in this problem.
const { format } = require('path');
var readline = require('readline');
var lines = []
var rl = readline.createInterface({
input: process.stdin
});
rl.on('line', function (line) {
lines.push(line)
});
rl.on('close', function() {
solve(lines)
})
function solve(lines) {
console.log(WhoWin(lines))
}
function WhoWin(lines){
for(i=0; i<3; i++){
if(lines[i][0] === lines[i][1] && lines[i][1] === lines[i][2]) {
return lines[i][0]
}
}
for(i=0; i<3; i++) {
if(lines[0][i] === lines[1][i] && lines[1][i] === lines[2][i]){
return lines[0][i]
}
}
if(lines[0][0] === lines[1][1] && lines[1][1] === lines[2][2]){
return lines[0][0]
}
if(lines[0][2] === lines[1][1] && lines[1][1] === lines[2][0]){
return lines[0][2]
}
return 'DRAW'
}
LIOJ 1034:凱薩加密
- 解題想法:
好像大腦當機了,因為輸出的結果變成多行,不是單行,然後我不知道要怎麼解決這個問題
function solve(lines) {
let N = Number(lines[0])
let str = lines[1]
for(let i = 0; i < str.length; i++){
let code = str[i].charCodeAt()
let result = code + N
let ans = ''
if(result <= 122) {
String.fromCharCode(result)
} else {
String.fromCharCode(result -= 26)
}
ans += String.fromCharCode(result)
console.log(ans)
}
}
- 正解在這裡
const { format } = require('path');
var readline = require('readline');
var lines = []
var rl = readline.createInterface({
input: process.stdin
});
rl.on('line', function (line) {
lines.push(line)
});
rl.on('close', function() {
solve(lines)
})
function solve(lines) {
let n = Number(lines[0])
let str = lines[1]
let result = ''
for(let i = 0; i < str.length; i++){
result += CaesarCipher(n, str[i])
}
console.log(result)
}
function CaesarCipher(n, s) {
let code = s.charCodeAt(0) - 97
let newCode = (code + n) % 26
return String.fromCharCode(newCode + 97)
}