algorithm/백준
백준 1009 Java
umilove98
2021. 7. 27. 12:17
반응형
a의 b제곱을 구해서 일의 자리 수를 출력한다.
예제에서 보듯이 9의 635제곱 같이 큰 수가 나오면 메모리가 초과될 수 있으므로 for 문으로 계속 같은 수를 % 10을 이용해 일의 자리 수만 남기면서 곱한다. 결과값이 0이면 10을 출력하고 1~9이면 그대로 출력한다.
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
31
32
33
34
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bf.readLine());
for(int i = 0; i < t; i++) {
StringTokenizer st = new StringTokenizer(bf.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int result = 1;
for(int j = 0; j < b; j++) {
result = (result * a) % 10;
}
if(result == 0) {
System.out.println(10);
}else {
System.out.println(result);
}
}
}
}
|
cs |
반응형