hdu_1061_Rightmost Digit_201311071851

Rightmost Digit

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

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
 
 
 1 #include <stdio.h>
 2 int main()
 3 {
 4     int T;
 5     scanf("%d",&T);
 6     while(T--)
 7     {
 8         int n,i,m;
 9         int t,sum=1;
10         scanf("%d",&n);
11         t=n%10;
12         m=n%4;
13         if(m==0)
14         sum=t*t*t*t%10;
15         else
16         {
17             for(i=0;i<m;i++)
18             {
19                 sum *= t;
20             }
21         }
22         printf("%d\n",sum%10);
23     }
24     return 0;
25 }
26 //找规律题,找到循环周期 

 

 
/*
对于数字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次就可以了,大大减少了循环的次数
*/
posted @ 2013-11-07 21:47  龙腾四海365  阅读(164)  评论(0编辑  收藏  举报