Rightmost Digit
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.
尾数为0,1,5,6的不管是多少次方尾数依然不变,而尾数为4和9的每2次循环,
2,3,7,8为每4次循环。循环结果如下:
0,1,5,6:位数永远是0,1,5,6
2:6,2,4,8循环
3:1,3,9,7循环
4:6,4循环
7:1,7,9,3循环
8:6,8,4,2循环
9:1,9循环
找到这个规律,这道题就不难了。不然一点思路没有。
代码如下:
#include<cstdio>
int main(){
int t,n,f[10][10]={{1,0},{1,1},{4,2,4,8,6},{4,3,9,7,1},{2,4,6},{1,5},{1,6},{4,7,9,3,1},{4,8,4,2,6},{2,9,1}};
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int m=n%f[n%10][0];
if(!m) m=f[n%10][0];
printf("%d\n",f[n%10][m]);
}
return 0;
}