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的个位数的只有N的个位数,且每个个位数的N次方有规律,以4为周期重复。
代码如下:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int t;
int n, rd, loop;
cin >> t;
while(t--)
{
cin >> n;
rd = n % 10;
loop = n % 4;
if(loop == 0)
loop = 4;
//cout << loop << endl;
rd = (int)pow(rd, loop);
rd = rd % 10;
cout << rd << endl;
}
return 0;
}