Given a positive integer N, you should output the most right digit of N^N.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
2 3 4
7
6
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.
代码:
#include<cstdio>
int main()
{long long int n;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
long long int sum=1%10,k=n%10;
while(n>0)
{
if(n%2==1)
sum=sum*k%10;
k=k*k%10;
n/=2;
}
printf("%d\n",sum);
}
return 0;
}
本文介绍了一个算法问题,即如何计算正整数N的N次方后的最右一位数字。通过输入一系列测试用例,算法可以快速计算出每个测试用例中N^N的最右一位数字。
1189

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



