51nod 1004 n^n的末位数字【快速幂】

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
 收藏
 关注
给出一个整数N,输出N^N(N的N次方)的十进制表示的末位数字。
 
Input
一个数N(1 <= N <= 10^9)
Output
输出N^N的末位数字
Input示例
13
Output示例
3

【代码】:
#include <bits/stdc++.h>

using namespace std;
#define LL long long
LL n;

int main()
{
   while(~scanf("%lld",&n))
   {
       LL ans=1,tmp=n;//改变位数,所以要临时变量
       while(n)
       {
           if(n&1)
           {
               ans=ans*tmp%10;
           }
           tmp=tmp*tmp%10;//改变位数的地方用临时变量
           n>>=1;
       }
       printf("%lld
",ans);
   }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/Roni-i/p/7774173.html