您的位置:首页 > 其它

hdu_1061_Rightmost Digit_201311071851

2013-11-07 21:47 357 查看

Rightmost Digit

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26512 Accepted Submission(s): 10193

[align=left]Problem Description[/align]
Given a positive integer N, you should output the most right digit of N^N.

[align=left]Input[/align]
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).

[align=left]Output[/align]
For each test case, you should output the rightmost digit of N^N.

[align=left]Sample Input[/align]

2
3
4

[align=left]Sample Output[/align]

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.

[align=left]Author[/align]
Ignatius.L

#include <stdio.h>
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,i,m;
int t,sum=1;
scanf("%d",&n);
t=n%10;
m=n%4;
if(m==0)
sum=t*t*t*t%10;
else
{
for(i=0;i<m;i++)
{
sum *= t;
}
}
printf("%d\n",sum%10);
}
return 0;
}
//找规律题,找到循环周期


/*
对于数字0~9, 它们的N次方,我们只看最后一个数字:
0^1=0; 0^2=0; 循环周期T=1;
1^1=1; 1^2=1;
循环周期T=1;
2^1=2; 2^2=4; 2^3=8; 2^4=6 2^5=2; 循环周期T=4;
3^1=3; 3^2=9; 3^3=7;
3^4=1;3^5=3; 循环周期T=4;
4^1=4; 4^2=6; 4^3=4;
循环周期T=2;

后面的大家可以自己算,会得出一个规律:
他们的循环周期只有1,2,4这三种,所以对于源代码里的b,我们可以只算b%4次就可以了,大大减少了循环的次数
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: