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).
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 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步