首页 > 其他 > 详细

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

时间:2017-05-06 22:31:55      阅读:271      评论:0      收藏:0      [点我收藏+]

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\n", ans%10 );
	}
	return 0; 
}

  

 

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

原文:http://www.cnblogs.com/zhang-yd/p/6818859.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!