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).
Output
For each test case, you should output the rightmost digit of N^N.
Sample Input
2
3
4
Sample Output
7
6
套用模板快速求幂取余,n的n次方对10进行取余
#include<stdio.h>
int pow(int a,int n,int b)
{
int result=1;
a=a%b;
while(n>0)
{
if(n%2==1)
result=result*a%b;
n=n/2;
a=a*a%b;
}
return result;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int ans=pow(n,n,10);
printf("%d\n",ans);
}
}