-컴퓨터는 10대
1 2 3 4 5 6 7 8 9 10
-데이터 개수는 a^b
: Math.pow(a,b)
-데이터 처리는 10개씩 주기를 돈다.
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
...
=> % 10 의 값이 데이터 처리 컴퓨터로 해당됨을 알 수 있다.
-입력값 a
2^3 = 8
22^3 = 10648
3^3 = 9
33^33 = 35937
ex) 2의 3제곱이든 22의 3제곱이든 % 10은 8
: 입력값 a에 % 10을 해줌으로써 a는 1~10을 고려하면 된다.
-입력값 b
: 문제 예시 값을 보면
1^6 = Math.pow(1,6) = 1
3^7 = Math.pow(3,7) = 2187
6^2 = Math.pow(6,2) = 36
7^100 = Math.pow(7,100) = 3.234476509624758e+84 // % 10의 경우 1이 나와야 하는데 4가 나옴
9^635 = Math.pow(9,635) = Infinity // % 10 하면 NaN이 나옴
숫자가 너무 크면 % 10으로 해결이 안됨.
규칙을 찾아보자
for (let i = 1; i <= 10; i++) {
for (let j = 1; j <= 10; j++) {
console.log(Math.pow(i, j));
}
}
1~9의 제곱값들을 % 10 한 경우 아래와 같은 주기가 나온다.
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
10 = 0
7의 경우로 확인을 해보니 배열 순서를 고려해줘야한다.
Math.pow(7,4);
2401 (1)
4 % 4 = 0
Math.pow(7,3);
343 (3)
3 % 4 = 3
Math.pow(7,2);
49 (9)
2 % 4 = 2
Math.pow(7,14);
678223072849 (9)
14 % 4 = 2
Math.pow(7,15);
4747561509943 (3)
15 % 4 = 3
Math.pow(7,5);
16807 (7)
5 % 4 = 1
배열 순서를 고려해서 b % 10의 주기 조정
1 = 1
2 = [6 2 4 8]
3 = [1 3 9 7]
4 = [6 4 6 4]
5 = 5
6 = 6
7 = [1 7 9 3]
8 = [6 8 4 2]
9 = [1 9 1 9]
10 = 0
const selectLastComputer = function (a, b) {
let answer = 0;
const b2Cycle = [6, 2, 4, 8];
const b3Cycle = [1, 3, 9, 7];
const b4Cycle = [6, 4, 6, 4];
const b7Cycle = [1, 7, 9, 3];
const b8Cycle = [6, 8, 4, 2];
const b9Cycle = [1, 9, 1, 9];
switch (a % 10) {
case 0:
answer = 10;
break;
case 1:
answer = 1;
break;
case 2:
answer = b2Cycle[b % 4];
break;
case 3:
answer = b3Cycle[b % 4];
break;
case 4:
answer = b4Cycle[b % 4];
break;
case 5:
answer = 5;
break;
case 6:
answer = 6;
break;
case 7:
answer = b7Cycle[b % 4];
break;
case 8:
answer = b8Cycle[b % 4];
break;
case 9:
answer = b9Cycle[b % 4];
break;
}
console.log(answer);
};
const userInput = function () {
const fs = require("fs");
const input = fs.readFileSync("/dev/stdin").toString().split("\n");
for (let i = 1; i <= input[0]; i++) {
const [a, b] = input[i].split(" ").map(Number);
selectLastComputer(a, b);
}
};
userInput();