Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 43080 Accepted Submission(s): 16192 Problem 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.
Author
Ignatius.L
需要观察规律,然后跟着打表就完了,不是很难。
首先观察0-9的n次方的最后一位:2,3,7,8,9 是4次方一个循环;4,9是二次方一循环;0,1,5,6末尾不变。
这里我提个建议,4,9虽然是2次方一循环,但是建议还是按照4次一循环来处理,这样不容易因为打代码时粗心而出bug,就像我,急着出去,结果WA了两次。
代码如下:
#include <stdio.h>
int num[10][5]=
{
{0},
{0,1},
{0,2,4,8,6},
{0,3,9,7,1},
{0,4,6},
{0,5},
{0,6},
{0,7,9,3,1},
{0,8,4,2,6},
{0,9,1},
};
int main()
{
// for (int i=0;i<10;i++)
// {
// for (int j=0;j<5;j++)
// {
// printf ("%d ",num[i][j]);
// }
// printf ("\n");
// }
int u;
int n;
int m; //n的末尾
scanf ("%d",&u);
while (u--)
{
scanf ("%d",&n);
m=n%10;
if (m==0)
{
printf ("0\n");
}
else if (m==1)
{
printf ("1\n");
}
else if (m==2)
{
int t=n%4;
if (t==0)
t=4;
printf ("%d\n",num[2][t]);
}
else if (m==3)
{
int t=n%4;
if (t==0)
t=4;
printf ("%d\n",num[3][t]);
}
else if (m==4)
{
int t=n%2;
if (t==0)
t=2;
printf ("%d\n",num[4][t]);
}
else if (m==5)
{
printf ("5\n");
}
else if (m==6)
{
printf ("6\n");
}
else if (m==7)
{
int t=n%4;
if (t==0)
t=4;
printf ("%d\n",num[7][t]);
}
else if (m==8)
{
int t=n%4;
if (t==0)
t=4;
printf ("%d\n",num[8][t]);
}
else if (m==9)
{
int t=n%2;
if (t==0)
t=2;
printf ("%d\n",num[9][t]);
}
}
return 0;
}