C - C
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status
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
Hint
In 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.
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 using namespace std; 5 int main() 6 { 7 8 int n; 9 int x; 10 scanf("%d",&n); 11 while(n--) 12 { 13 scanf("%d",&x); 14 int m=x%10; 15 int num=1; 16 if(m==0||m==1||m==5||m==6||m==9) 17 { 18 cout<<m<<endl; 19 } 20 else 21 { 22 while(x>0) 23 { 24 if(x%2==1) 25 { 26 num*=m; 27 num%=10; 28 } 29 x/=2; 30 m*=m; 31 m%=10; 32 } 33 cout<<num<<endl; 34 } 35 } 36 }
时间: 2024-11-10 19:45:32