码迷,mamicode.com
首页 > Web开发 > 详细

HDU 1061 Rightmost Digit

时间:2016-07-20 22:58:07      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

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.

N的N次方,只需要求N的个位数的N的方的个位数,同时有一个规律,
1 1 1 1 1 1 1 1
2 4 8 6 2 4 8 6
3 9 7 1 3 9 7 1
4 6 4 6 4 6 4 6
5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6
7 9 3 1 7 9 3 1
8 4 2 6 8 4 2 6
1 9 1 9 1 9 1 9
所以应该很简单把
 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int main()
 5 {
 6     int t;
 7     int n;
 8     int i,j;
 9     while(cin>>t)
10     {
11         while(t--)
12         {
13             cin>>n;
14             i=n%10;
15             j=n%4;
16             if(j==0)
17                 cout<<i*i*i*i%10<<endl;
18             else
19             {
20                 int ans=1;
21                 while(j--)
22                 {
23                     ans*=i;
24                     ans=ans%10;
25                 }
26                 cout<<ans<<endl;
27             }
28         }
29     }
30     return 0;
31 }

 

HDU 1061 Rightmost Digit

标签:

原文地址:http://www.cnblogs.com/Aa1039510121/p/5689862.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!