1. 程式人生 > >第五次測試 A的B次方

第五次測試 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並不代表我真正會了,只是取巧,下次不一定能做得出來,有關快速冪的解法會在這周補上。