hdu-1097 A hard puzzle

                                                                                                     A hard puzzle

Problem Description
lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin.
this puzzle describes that: gave a and b,how to know the a^b's the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise.
Input
There are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)

Output
For each test case, you should output the a^b's last digit number.
Sample Input

7 66

8 800

 
Sample Output
9
6
题意:此题求a^b的个位数字。(0<a,b<=2^30)。数字很大,但要求的只是个位数字。怎么做呢?因为此题这么特殊的情况,0~9的幂的个位所呈的周期又很显然,周期最大也才4,还有很多周期就为1,如(0,1,5等),所以我采用了特殊方法。不管a是多少,只取他的个位数字来运算,算出0~9的幂的个位数字的周期,用b%这个周期,找到对应的答案。其实这个方法也不算怪吧,以前做此数学题就是这么做的呀,对吧?此题方法很多,用这个方法的人不知有几个呢?
#include<iostream>
using namespace std;
int main()
{
	int a,b,ans;
	while(scanf("%d%d",&a,&b)!=EOF)
	{
		int x[4];  //事实证明,周期都在1~4
		int n;
		if(a>=10)
			a%=10; //既然只要求个位,干脆这样来简便运算
		switch(a)
		{
		case 0:ans=0;break;
		case 1:ans=1;break;
		case 2:x[0]=2;x[1]=4;x[2]=8;x[3]=6;n=b%4;if(n==0)	n=4;ans=x[n-1];break;  
			//思路一直很简单,但这里要注意; if(n==0)	n=4;ans=x[n-1]其实也不难理解吧....
		case 3:x[0]=3;x[1]=9;x[2]=7;x[3]=1;n=b%4;if(n==0)	n=4;ans=x[n-1];break;
		case 4:x[0]=4;x[1]=6;n=b%2;if(n==0)	n=2;ans=x[n-1];break;
		case 5:ans=5;break;
		case 6:ans=6;break;
		case 7:x[0]=7;x[1]=9;x[2]=3;x[3]=1;n=b%4;if(n==0)	n=4;ans=x[n-1];break;
		case 8:x[0]=8;x[1]=4;x[2]=2;x[3]=6;n=b%4;if(n==0)	n=4;ans=x[n-1];break;
		case 9:x[0]=9;x[1]=1;n=b%2;if(n==0)	n=2;ans=x[n-1];break;
		}
		printf("%d\n",ans);
	}
	return 0;
}

 

你可能感兴趣的:(input,each,BT,output,Numbers)