第五次测试 A的B次方

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
lcy给风5166,lwg,JGShining和Ignatius出了一个难题:给出了a和b,如何知道a^ b。每个人都反对这个BT问题,所以lcy使问题比开始容易。这个谜题描述的是:给定a和b。如何知道一个a^ b的最后一位数字。但是每个人都懒得解决这个问题,所以他们把问题交给你。
输入*

    • *有多个测试用例。每个测试用例由a和b两个数字(0 < a、b < = 2 ^ 30)
    • *输出
  • 对于每个测试用例,您应该输出一个^ b的最后一位数。
  • emmmmmm
  • 就算是大佬也会有犯错的时候,这道题本来是考查快速幂的,但是我有一个更傻逼更粗暴的方案。
#include<stdio.h>
int main ()
{
   int n,m;
   int s;
   while(~scanf("%d %d",&n,&m)){
   	int t=n%10;
   	if(t==0||t==1||t==5||t==6){
   		printf("%d\n",t);
   	}
   	
   	if(t==4||t==9){
   		int s=m%2;
   		if(s==1)
   		printf("%d\n",t);
   		if(s==0){
   			printf("%d\n",(t*t)%10);
   		}
   	}
   	if(t==2||t==3||t==7||t==8||t==8){			
   		int s=m%4;
   		if(s==0){
   			printf("%d\n",(t*t*t*t)%10);
   		}
   		if(s==1){				
   			printf("%d\n",t%10);
   		}
   		if(s==2){
   			printf("%d\n",(t*t)%10);
   		}
   		if(s==3){
   			printf("%d\n",(t*t*t)%10);
   		}
   	}
   }	
   return 0;
}

这样一来这就成了找规律的题了,对于知识浅薄的我来说,这无疑是福音,但是AC并不代表我真正会了,只是取巧,下次不一定能做得出来,有关快速幂的解法会在这周补上。

猜你喜欢

转载自blog.csdn.net/weixin_43902655/article/details/84768284