我可不是为了被全人类喜欢才活着的,只要对于|

王陸

园龄:6年11个月粉丝:2052关注:178

Rightmost Digit(最后一位数字)

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次方的最后一位数字
复制代码
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     long long int n,i,j,ans,t,sum;;
 6     scanf("%lld",&t);
 7     while(t--)
 8     {
 9         scanf("%lld",&n);
10         sum=1;
11         ans=1;
12         n=n%20;//找出规律,20个数一次循环
13         if(n==0)
14         {
15             n=20;
16         }//将范围缩小到20以内
17         for(i=0;i<n;i++)
18         {
19             sum=ans*n;
20             ans=sum%10;//(只要最后的一位)
21         }
22         ans=ans%10;
23         printf("%lld\n",ans);
24     }
25     return 0;
26 }
复制代码

 

本文作者:王陸

本文链接:https://www.cnblogs.com/wkfvawl/p/9208592.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   王陸  阅读(666)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起