A - A hard puzzle解题报告(张宇)

A - A hard puzzle
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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%这个周期,找到对应的答案。其实这个方法也不算怪吧,以前做此数学题就是这么做的呀,对吧?

[cpp]  view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     int a,b,ans;  
  6.     while(scanf("%d%d",&a,&b)!=EOF)  
  7.     {  
  8.         int x[4];  //事实证明,周期都在1~4  
  9.         int n;  
  10.         if(a>=10)  
  11.             a%=10; //既然只要求个位,干脆这样来简便运算  
  12.         switch(a)  
  13.         {  
  14.         case 0:ans=0;break;  
  15.         case 1:ans=1;break;  
  16.         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;    
  17.             //思路一直很简单,但这里要注意; if(n==0)  n=4;ans=x[n-1]其实也不难理解吧....  
  18.         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;  
  19.         case 4:x[0]=4;x[1]=6;n=b%2;if(n==0) n=2;ans=x[n-1];break;  
  20.         case 5:ans=5;break;  
  21.         case 6:ans=6;break;  
  22.         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;  
  23.         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;  
  24.         case 9:x[0]=9;x[1]=1;n=b%2;if(n==0) n=2;ans=x[n-1];break;  
  25.         }  
  26.         printf("%d\n",ans);  
  27.     }  
  28.     return 0;  
  29. }  


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值