Problem Description
Given a positive integer N, you should output the most right digit of N^N.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
Output
For each test case, you should output the rightmost digit of N^N.
Sample Input
2 3 4
Sample Output
7 6HintIn the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7. In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
输入3,输出3*3*3的个位;
输入4,输出4*4*4*4的个位;
可以找到规律,20个数一个循环。其循环为:1,4,7,6,5,6,3,6,9,0,1,6,3,6,5,6,7,4,9,0
代码如下:
#include <iostream>
using namespace std;
int s[20]={1,4,7,6,5,6,3,6,9,0,1,6,3,6,5,6,7,4,9,0};
int main(){
int n,x,ans;
cin >> n;
while(n--){
cin >> x;
ans = s[x%20-1];
cout << ans << endl;
}
return 0;
}
本文探讨了如何通过寻找特定规律来快速计算任意正整数N的N次方的个位数,利用循环特性简化计算过程。
636

被折叠的 条评论
为什么被折叠?



