https://www.acmicpc.net/problem/1009
데이터 개수가 증가하면서 값이 1~10 내에서만 변화한다.
a^b를 바로 연산하면 숫자가 매우 커지므로 반복문을 통해 쪼갠야 한다.
a에 a를 곱하고, 10으로 나눈 나머지를 구하는 과정을 b번 반복한다.
결과가 0인 경우 10번째 컴퓨터를 의미하므로 10으로 수정한다.
결과값을 출력한다.
C
더보기
#include <stdio.h>
int main() {
int t; scanf("%d", &t);
while(t--){
int a, b; scanf("%d %d", &a, &b);
int res = a; res %= 10;
while(--b){
res *= a;
res %= 10;
}
if(res == 0) res = 10;
printf("%d\n", res);
}
}
Python
더보기
t = int(input())
for _ in range(t):
a, b = map(int, input().split())
res = pow(a, b, 10)
if res == 0: res = 10
print(res)
Java
더보기
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for(int i = 0; i < t; i++){
int a = s.nextInt(), b = s.nextInt();
int res = 1;
for(int j = 0; j < b; j++){res *= a; res %= 10;}
System.out.println(res == 0 ? 10 : res);
}
}
}
JavaScript
더보기
ip = require('fs').readFileSync(0).toString().split('\n')
t = ip[0]
for(let i = 1; i <= t; i++){
var[a, b] = ip[i].split(' ').map(Number)
res = 1;
for(let j = 1; j <= b; j++){res *= a; res %= 10}
console.log(res == 0 ? 10 : res)
}
'구현, 수학' 카테고리의 다른 글
[백준] 1085. 직사각형에서 탈출 풀이 (0) | 2025.02.02 |
---|---|
[백준] 1011. Fly me to the Alpha Centauri 풀이 (0) | 2025.02.02 |
[백준] 1008. A/B 풀이 (0) | 2025.02.01 |
[백준] 1001. A-B 풀이 (0) | 2025.02.01 |
[백준] 1000. A+B 풀이 (0) | 2025.02.01 |