View Code of Problem 3696

#include<iostream>
 
using namespace std;
 
int quickpower(int n){//快速幂
    int answer=1;
    int k=n;
    while(k!=0){
        if(k%2==1){
            answer*=n;
            answer=answer%10;//取多少位就模多少,结果只跟后几位相关
        }
        k/=2;
        n*=n;
        n%=10;
    }
    return answer;
}
 
int main(){
    int n;
    while(cin>>n){
        cout<<quickpower(n)<<endl;
    }
}

Double click to view unformatted code.


Back to problem 3696