51Nod

51Nod - 1004 n^n的末位数字

给出一个整数N,输出N^N(N的N次方)的十进制表示的末位数字。
 
Input
一个数N(1 <= N <= 10^9)
Output
输出N^N的末位数字
Input示例
13
Output示例
3

题解: 

    末尾数字,所以在快速迭代幂的时候,只需要考虑末尾数字即可。 

#include <iostream> 
#include <cstdio> 
#include <cstdlib> 
#include <cstring> 
using namespace std;  

int main(){
	int n, cnt, ans; 
	while(scanf("%d", &n) != EOF){
		cnt = n; 
		ans = 1; 
		n = n % 10; 
		while(cnt){
			if(cnt%2 == 1){
				ans = (ans * n) % 10; 
			}
			n = n * n % 10; 
			cnt = cnt / 2; 
		}
		printf("%d
", ans%10 );
	}
	return 0; 
}

  

原文地址:https://www.cnblogs.com/zhang-yd/p/6818859.html