Repository navigation
|
老師好,各位同學好 function solve(lines){
let arr = []
let min = Infinity
for (let i=1; i<lines.length; i++){
arr1 = lines[i].split(' ')
arr = arr.concat(arr1)
}
for (let i=0; i<arr.length; i+=2){
for (let j=i+2; j<arr.length; j+=2){
a = Number(arr[i]) - Number(arr[j])
b = Number(arr[i+1]) - Number(arr[j+1])
result = Math.sqrt(a * a + b * b).toFixed(2)
if (result < min){
min = result
spot1_x = arr[i]
spot1_y = arr[i+1]
spot2_x = arr[j]
spot2_y = arr[j+1]
}
}
}
if (spot1_x > spot2_x){
console.log(spot2_x + ' ' + spot2_y)
console.log(spot1_x + ' ' + spot1_y)
}else if (spot1_x < spot2_x){
console.log(spot1_x + ' ' + spot1_y)
console.log(spot2_x + ' ' + spot2_y)
}else {
if (spot1_y > spot2_y){
console.log(spot2_x + ' ' + spot2_y)
console.log(spot1_x + ' ' + spot1_y)
}else {
console.log(spot1_x + ' ' + spot1_y)
console.log(spot2_x + ' ' + spot2_y)
}
}
} |
Answered by
PT-HUANG
Aug 9, 2023
Replies: 1 comment 3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
結果...加了Number 就AC了
spot1_x = Number(arr[i])
完全不曉得原因在哪裡😅😅😅