n^n的末位數字(快速冪)

給出一個整數N,輸出N^N(N的N次方)的十進制表示的末位數字。
Input
一個數N(1 <= N <= 10^9)
Output
輸出N^N的末位數字
Input示例
13
Output示例

3

AC代碼:

#include<bits/stdc++.h>
using namespace std;
int powmod(int x){
	if(x==1)return 1;
	else{
		int ans=1;
		int k=x%10;
		while(x>0){
			if(x%2==1)ans=ans*k%10;
			x/=2;
			k=k*k%10;
		}
		return ans;
	}
}
int main()
{
	int N;
	while(~scanf("%d",&N))
	printf("%d\n",powmod(N));
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章