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. N的N次方,只需要求N的个位数的N的方的个位数,同时有一个规律,1 1 1 1 1 1 1 12 4 8 6 2 4 8 63 9 7 1 3 9 7 14 6 4 6 4 6 4 65 5 5 5 5 5 5 56 6 6 6 6 6 6 67 9 3 1 7 9 3 18 4 2 6 8 4 2 61 9 1 9 1 9 1 9所以应该很简单把
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 int main() 5 { 6 int t; 7 int n; 8 int i,j; 9 while(cin>>t) 10 { 11 while(t--) 12 { 13 cin>>n; 14 i=n%10; 15 j=n%4; 16 if(j==0) 17 cout<<i*i*i*i%10<<endl; 18 else 19 { 20 int ans=1; 21 while(j--) 22 { 23 ans*=i; 24 ans=ans%10; 25 } 26 cout<<ans<<endl; 27 } 28 } 29 } 30 return 0; 31 }
时间: 2024-10-15 18:57:02