백준 1009번 문제 '분산처리' 리뷰 🙇🏻♀️
문제출처: https://www.acmicpc.net/problem/1009
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const lastDigitObj = {
"1": [1],
"2": [2, 4, 8, 6],
"3": [3, 9, 7, 1],
"4": [4, 6],
"5": [5],
"6": [6],
"7": [7, 9, 3, 1],
"8": [8, 4, 2, 6],
"9": [9, 1],
"0": [10]
}
let lastDigit;
let t = -1;
let count = 0;
rl.on('line', line => {
if (t < 0){
t = parseInt(line); //제일 처음 들어오는 input이 t가 된다.
console.log("t", t);
} else {
count++;
if (count > t) {
console.log("ended!");
rl.close();
} else {
let [int, expo] = line.split(' ').map(el => Number(el));//input.push(line);
int = parseInt(JSON.stringify(int).slice(-1));
let lastDigit = carculate(int, expo);
console.log("count", count);
console.log(lastDigit);
}
}
});
function carculate(int, expo) {
const remainder = expo % 10; //10보다 작은 수로 나옴.
if (lastDigitObj[int].length === 1){
lastDigit = lastDigitObj[int][0];
} else {
const length = lastDigitObj[int].length;
let idx;
if (remainder % length === 0){
idx = lastDigitObj[int].length - 1;
} else {
idx = (remainder % length) - 1;
}
lastDigit = lastDigitObj[int][idx];
}
return lastDigit;
}
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const lastDigitObj = {
"1": [1],
"2": [2, 4, 8, 6],
"3": [3, 9, 7, 1],
"4": [4, 6],
"5": [5],
"6": [6],
"7": [7, 9, 3, 1],
"8": [8, 4, 2, 6],
"9": [9, 1],
"0": [10]
}
let lastDigit;
let t = -1;
let count = 0;
rl.on('line', (line) => {
if (t < 0){
t = parseInt(line); //제일 처음 들어오는 input이 t가 된다.
} else {
count++;
let [int, expo] = line.split(' ').map((el) => Number(el));//input.push(line);
let lastDigit = carculate(int, expo);
console.log(lastDigit);
if (count === t) {
rl.close();
}
}
});
function carculate(int, expo) {
const remainder = int % 10; //10보다 작은 수로 나옴.
if (remainder === 0){
lastDigit = 10;
} else if (lastDigitObj[remainder].length === 1){
lastDigit = lastDigitObj[remainder][0];
} else {
const length = lastDigitObj[remainder].length; //length of each obj in obj
let idx; //need idx for lastDigit
if (expo % length === 0){
idx = lastDigitObj[remainder].length - 1;
} else {
idx = (expo % length) - 1;
}
lastDigit = lastDigitObj[remainder][idx];
}
return lastDigit;
}
readline.createInterface()
, rl.on()
, rl.close()
등)lastDigitObj
객체에 저장했고, 나머지를 기준으로 인덱스로 접근해 1의자리를 찾아내는 방법을 사용했다.10
의 거듭제곱은 1의 자리가 0
으로 끝나니 자연스레 0
을 저장해뒀지만, 사실...const remainder = expo % 10;
→ const remainder = int % 10;
remainder % length
도 expo % length
로 바뀜.if (count > t) { console.log("ended!"); rl.close(); }
→ if (count === t) { rl.close(); }
if (remainder === 0){ lastDigit = 10; }
으로 0인 경우 따로 예외처리.lastDigit = lastDigitObj[int][idx];
→ lastDigit = lastDigitObj[remainder][idx];
으로 바뀜.