您的位置:首页 > 其它

hdu 1061 Rightmost Digit

2013-12-22 23:07 411 查看
[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
这个题给定的n范围很大,但我们只关心最后一位,于是很容易想到每次都对10取余,但这超时了。其实,对每一个数来说,N^N的个位数是呈周期性的。找出周期所有的问题就解决了。代码如下:

#include<stdio.h>
#include<math.h>
int a[11];
int main()
{
int n,k,count,t,i;
scanf("%d",&t);
while(t--)
{
count=1;
scanf("%d",&n);
a[1]=n%10;
k=n%10;
for(i=2;;i++)//这里不要限定范围(i<=n),因为有可能还没找到周期,循环就已经结束了。
{
k*=n%10;
k%=10;
if(k==a[1]) break;
else{
a[++count]=k;
}

}
a[0]=a[count];
printf("%d\n",a[(n)%(count)]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: