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.
这道题粗略看简介大体思路,就是求pow(n,n)的解求出个位的数字,我相信这是大多人拿到这个题第一个想法,可能是我太菜...拿到手我就是这么想的
提交之后实则是WA
下面附网上代码
#include<iostream>
#include<stdio.h>
using namespace std;
int main(void)
{
int T,n,sd,digit;
scanf("%d",&T);
while(T--)
{
if(scanf("%d",&n) != EOF)
{
sd = n%10;
if(sd==0 || sd==1 || sd==5 || sd==6 || sd==9)
digit = sd;
else if(sd == 2 || sd == 8)
{
if(n%4 == 0)
sd = 6;
else
sd = 4;
}
else if(sd == 3)
{
if(n%4 == 1)
sd = 3;
else
sd = 7;
}
else if(sd == 4)
{
sd = 6;
}
else{
if(n%4 == 1)
sd = 7;
else
sd = 3;
}
printf("%d\n",sd);
}
}
return 0;
}
/*
末位数 相乘后的末位数
1 1
2 4 8 6 2
3 9 7 1 3
4 6 4
5 5
6 6
7 9 3 1 7
8 4 2 6 8
9 1 9(由于x9是奇数,所以1不会出现)
0 0
由上面的分析可见,每个数相乘后最多有四个结果
所以对一个数n,只需做其对4取余后余数次相乘即可
但是会出现 n%4 == 0 的情况
由于x5^x5的末位数是5,x9^x9的末位数是9
所以将n%4转换为(n-1)%4
*/
#include <stdio.h>
int main()
{
int i, ncase, m, n, t;
scanf("%d", &ncase);
while(ncase--)
{
scanf("%ld", &n);
t = n % 10;
m = 1;
for(i = 0; i <= (n-1)%4; i ++) //此处可换为 i < n%4 + 4;可解决n%4 ==0的情况
{
m *= t;;
}
printf("%d\n", m%10);
}
return 0;
}