HDOJ---1061 Rightmost Digit[简单数学题]

Rightmost Digit

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


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
 
 
 
 
 
 
 
 
code:
 1 #include<iostream>

 2 using namespace std;

 3 

 4 int table[10][10]={

 5     0,0,0,0,0,0,0,0,0,0,

 6     1,1,1,1,1,1,1,1,1,1,

 7     2,4,8,6,2,4,8,6,2,4,

 8     3,9,7,1,3,9,7,1,3,9,

 9     4,6,4,6,4,6,4,6,4,6,

10     5,5,5,5,5,5,5,5,5,5,

11     6,6,6,6,6,6,6,6,6,6,

12     7,9,3,1,7,9,3,1,7,9,

13     8,4,2,6,8,4,2,6,8,4,

14     9,1,9,1,9,1,9,1,9,1

15 };

16 

17 int cnt[10]={10,10,4,4,2,10,10,4,4,2};

18 

19 int main()

20 {

21     int t;

22     int n;

23     scanf("%d",&t);

24     while(t--)

25     {

26         scanf("%d",&n);

27         int temp=n;

28         temp%=10;

29         if(!temp)

30         {

31             printf("0\n");

32             continue;

33         }

34         printf("%d\n",table[temp][(n-1)%cnt[temp]]);

35     }

36     return 0;

37 }

 

你可能感兴趣的:(right)