题目描述:
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
题目分析:我们只需要求a^b的最后一位,显然由于a和b太大,只能找规律。
题目转化:1.最后一位即个位,那我们不妨将a mod 10,因为只有a的最后一位在起作用;
2.若我们将a再乘b下,肯定还是超时;必然还要继续找规律;显然剩下的a只有10种情况,那我们为什麽不一一列举了?事实证明:它们具有周期性,简单吧。(没事写写水题也不错。)
代码:
View Code
1 #include"stdio.h" 2 int main() 3 { 4 int a,b; 5 while(scanf("%d%d",&a,&b)!=EOF) 6 { 7 int s=1; 8 a%=10; 9 if(a==0||a==1||a==5||a==6) s=a; 10 else if(a==2) 11 { 12 b%=4; 13 if(b==0) s=6; 14 else if(b==1) s=2; 15 else if(b==2) s=4; 16 else s=8; 17 } 18 else if(a==3) 19 { 20 b%=4; 21 if(b==0) ; 22 else if(b==1) s=3; 23 else if(b==2) s=9; 24 else if(b==3) s=7; 25 } 26 else if(a==4) 27 { 28 b%=2; 29 if(b==0) s=6; 30 else s=4; 31 } 32 else if(a==7) 33 { 34 b%=4; 35 if(b==0) s=1; 36 else if(b==1) s=7; 37 else if(b==2) s=9; 38 else s=3; 39 } 40 else if(a==8) 41 { 42 b%=4; 43 if(b==0) s=6; 44 else if(b==1) s=8; 45 else if(b==2) s=4; 46 else if(b==3) s=2; 47 } 48 else 49 { 50 b%=2; 51 if(b==0) ; 52 else s=9; 53 } 54 printf("%d\n",s); 55 } 56 return 0; 57 }
2013-01-09 20:39:08