题目描述
Given a positive integer N, you should output the most right digit of N^N.
输入
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).
输出
For each test case, you should output the rightmost digit of N^N.
样例输入
2 3 4
样例输出
7
6
末位数一共就0123456789十种可能性,我们稍微找一下规律就可以发现: 0:0 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 所有的数字只有四种可能性,接下来末位数又要循环了,就能找出一个总规律: (a%10)^(a%4)%10
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n,a,b,c,d;
scanf("%d",&n);
while(n--)
{
scanf("%d",&a);
b=a%10; //求a的末位数
c=a%4; //求a循环的次数
if(c==0)
c=4;
d=(int)pow(b,c)%10;
printf("%d\n",d);
}
return 0;
}