LIOJ 1029:加減乘除
Output a '+', '-', '*', '/' b
,the OJ input would be 'a + b' or 'a * b'
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 input = lines[0].split(' ')
let a = Number(input[0])
let symbol = input[1]
let b = Number(input[2])
if (symbol === '+') {
console.log(a + b)
} else if (symbol === '-') {
console.log(a - b)
} else if (symbol === '*') {
console.log(a * b)
} else {
console.log(a / b)
}
}
LIOJ 1030:判斷迴文
Find if a reversed string equals to the original string
- 在解這題的時候,我 i 的初始值設錯了,應該是要 input.length - 1,我忘了減一,所以一直都是 False; I forgot to setup the string length with 'input.length - 1', so the answer's always 'False'
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 input = lines[0]
if (reverse(input) === input) {
console.log('True')
} else {
console.log('False')
}
}
function reverse(input) {
let result = ''
for(i = input.length - 1; i>=0; i--) {
result += input[i]
}
return result
}
LIOJ 1031:完全平方和
Find the summation of perfect square within a certain number, ex 30: 1 + 4 + 9 + 16 + 25 = 55 The answer should be 55
- 卡在要如何判斷一個數是否為整數,後來想想,只要把從 1 開始把每個數平方,從 1 到小於題目指定所有的數,全部加起來就是答案了。
- Maybe I don't need to judge if a number is an integer, just find the square numbers smaller than a certain number and then make a summation.
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 ans = 0
for(i = 1; i <= n; i++) {
if(i*i <= n) {
ans += i * i
}
}
console.log(ans)
}
LIOJ 1032:平面距離計算
有三個問題:
- 怎麼取到資料?我卡在這個地方,要注意的是 lines[0] 其實就是讓我知道計算的次數,另外 x1, x2, y1, y2 那四個數不可以寫死,所以要找到每一組的開頭,題目的這句話是重點「每一筆測試資料會有 4 個整數」
- 寫第一個函式計算整個過程
- 怎麼取到小數點第二位
result.toFixed(2)
解題過程:我真的是太粗心了,要不就忘記轉成數字,要不就 x1, x2 順序寫錯
- 要看清楚題目
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 times = Number(lines[0])
for(let i = 1; i <= times; i++) {
//i 就是筆數
//我覺得這題最難的是理解這一段,從 lines[5] 往回推 lines[1]
let start = (i - 1) * 4 + 1
let x1 = Number(lines[start])
let y1 = Number(lines[start + 1])
let x2 = Number(lines[start + 2])
let y2 = Number(lines[start + 3])
console.log(distance(x1, y1, x2, y2))
}
}
function distance(x1, y1, x2, y2) {
let result = Math.sqrt(
Math.abs(x1 - x2) * Math.abs(x1 - x2) +
Math.abs(y1 - y2) * Math.abs(y1 - y2)
)
return result.toFixed(2)
}