码迷,mamicode.com
首页 > Web开发 > 详细

HDU 1061 Rightmost Digit题解

时间:2014-06-18 11:21:53      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   使用   2014   os   

求一个大数N^N的值的最右边的数字,即最低位数字。

简单二分法求解就可以了。

不过注意会溢出,只要把N % 10之后,就不会溢出了,不用使用long long。


#include <stdio.h>
int rightMost(int n, int N)
{
	if (n == 0) return 1;
	int t = rightMost(n / 2, N);
	t = t * t % 10;;
	if (n % 2) t *= N;
	return t % 10;
}

int main()
{
	int T, n;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d", &n);
		printf("%d\n", rightMost(n, n%10));
	}
	return 0;
}



HDU 1061 Rightmost Digit题解,布布扣,bubuko.com

HDU 1061 Rightmost Digit题解

标签:class   blog   code   使用   2014   os   

原文地址:http://blog.csdn.net/kenden23/article/details/31405149

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!