题目
/**
* 输入n, 第35秒 n=35
* 输出坐标位置 (5,0)
*/
const getCoordinates = (n = 0) => {
if (n === 0) return "(0, 0)";
if (n === 1) return "(0, 1)";
if (n === 2) return "(1, 1)";
if (n === 3) return "(1, 0)";
let x = 0;
let y = 0;
const rooting_floor = Math.floor(Math.sqrt(n)); // 开平方 向下取整
const rooting = rooting_floor % 2 === 1 ? rooting_floor - 1 : rooting_floor;
const remainder = n - Math.pow(rooting, 2); // 平方之后多出的数
if (remainder >= 0 && remainder <= rooting) {
x = rooting;
y = remainder;
} else if (remainder > rooting && remainder <= 2 * rooting) {
x = rooting - (remainder - rooting);
y = rooting;
} else if (remainder === 2 * rooting + 1) {
x = rooting;
y = rooting + 1;
} else if (remainder > (2 * rooting + 1) && remainder <= (3 * rooting + 2)) {
x = (rooting + 1) - (remainder - (2 * rooting + 1))
y = rooting + 1;
} else {
x = rooting + 1;
y = (rooting + 1) - (remainder - (3 * rooting + 2));
}
return `(${x}, ${y})`
}
网友评论