HDU 1061 Rightmost Digit题解

求一个大数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题解

上一篇:ES6模板字符串中使用循环并取值


下一篇:17.WLAN拓扑介绍_无线分布式系统