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的个位数是呈周期性的。找出周期所有的问题就解决了。代码如下:
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; }
相关文章推荐
- hdu1061 Rightmost Digit
- hdu-oj 1061 Rightmost Digit
- HDU 1061 Rightmost Digit
- hdu 1061 - Rightmost Digit(求N^N末位)
- HDU - 1061 Rightmost Digit
- HDU 1061 Rightmost Digit
- hdu 1061 Rightmost Digit
- 【HDU】1061 - Rightmost Digit(快速幂)
- hdu 1061 Rightmost Digit
- Rightmost Digit HDU - 1061 (快速幂裸题)
- hdu 1061 Rightmost Digit
- HDU 1061 Rightmost Digit
- HDU 1061 Rightmost Digit (四则运算求余,快速幂)
- HDU 1061 Rightmost Digit
- hdu 1061 Rightmost Digit
- HDU 1061 Rightmost Digit
- hdu 1061 Rightmost Digit
- HDU 1061 Rightmost Digit(A^A mod 10)
- HDU 1061 ----Rightmost Digit
- HDU1061-Rightmost Digit-规律题,快速幂