首页 > 代码库 > HDOJ 1061 Rightmost Digit

HDOJ 1061 Rightmost Digit

Rightmost Digit

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


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
本题的大意是输入一个1到1000000000的整数N,要求输出N^N次方后的最后一个数字.....
解题思路:因为数据很大,只需要输出最后一位,所以对每个数据先求余得到最后一位数字,然后找出其最后一位的周期即可....
 1 /**************************************************
 2 HDOJ 1061 已经AC
 3 ***************************************************/
 4 #include <iostream>
 5 using namespace std;
 6 int main()
 7 {
 8     int N,T;
 9     int round=0;
10     int r[100]={0};
11     int i=0;
12     cin>>T;
13     while(T--)
14     {
15         cin>>N;
16         r[1]=N%10;//一定要对其求余
17         r[2]=(N%10)*(N%10)%10;//求余后再相乘,然后再求余,因为如果直接相乘时碰到1000000000时会发生错误
18         for(i=3;;i++)
19         {
20             r[i]=r[i-1]*(N%10)%10;
21             if(r[i]==r[1])
22             {round=i-1;break;}
23         }
24         if(N%round==0)
25             cout<<r[round]<<endl;
26         else cout<<r[N%round]<<endl;
27     }
28     //while(1);
29     return 0;
30 }