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;
}
该博客介绍了如何求解正整数N的N次方的个位数。给出的示例中,对于输入的测试用例,通过观察发现N的个位数对结果的个位数起决定作用,并且这个过程遵循以4为周期的规律。提供的代码实现了根据N的个位数快速确定N^N的个位数的功能。
972

被折叠的 条评论
为什么被折叠?



