码迷,mamicode.com
首页 > 其他好文 > 详细

CDZSC_2015寒假新人(2)——数学 C

时间:2015-01-24 20:04:38      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

C - C
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

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. 
         
 

 

 
技术分享
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 using namespace std;
 5 int main()
 6 {
 7 
 8     int n;
 9     int x;
10     scanf("%d",&n);
11     while(n--)
12     {
13         scanf("%d",&x);
14         int m=x%10;
15         int num=1;
16         if(m==0||m==1||m==5||m==6||m==9)
17         {
18             cout<<m<<endl;
19         }
20         else
21         {
22             while(x>0)
23             {
24                 if(x%2==1)
25                 {
26                     num*=m;
27                     num%=10;
28                 }
29                 x/=2;
30                 m*=m;
31                 m%=10;
32             }
33             cout<<num<<endl;
34         }
35     }
36 }
View Code

 

CDZSC_2015寒假新人(2)——数学 C

标签:

原文地址:http://www.cnblogs.com/guofeng1022/p/4246340.html

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