HDU1097暴力打表找规律a^b

xiaoxiao2021-02-28  105

A hard puzzle

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 44659 Accepted Submission(s): 16333

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.

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

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

思路

1、还是打表找规律,找a^b值的个位数

打表找规律代码

打表a从1-15,b从1-10找规律

#include <stdio.h> #include <iostream> using namespace std; int main() { for(int i=1;i<=15;i++) { long int sum=1; for(int j=1;j<=10;j++) { sum*=i; if(sum>100) sum%=100; cout<<sum%10<<" "; } cout<<endl; } return 0; }

可以看出a从1-10顺序规律,b也可以看出每项规律。放入二维数组即可


代码

#include <iostream> #include <math.h> #include <stdio.h> using namespace std; int main() { long int a,b; int s[10][5]={{0},{1},{2,4,8,6},{3,9,7,1},{4,6},{5},{6},{7,9,3,1},{8,4,2,6},{9,1}}; while(cin>>a>>b) { a=a%10; if(a==0) cout<<s[0][0]<<endl; if(a==1) cout<<s[1][0]<<endl; if(a==2) { int m=(b%4)-1; if(m==-1) m=3; cout<<s[2][m]<<endl; } if(a==3) { int m=(b%4)-1; if(m==-1) m=3; cout<<s[3][m]<<endl; } if(a==4) { int m=(b%2)-1; if(m==-1) m=1; cout<<s[4][m]<<endl; } if(a==5) { cout<<s[5][0]<<endl; } if(a==6) cout<<s[6][0]<<endl; if(a==7) { int m=(b%4)-1; if(m==-1) m=3; cout<<s[7][m]<<endl; } if(a==8) { int m=(b%4)-1; if(m==-1) m=3; cout<<s[8][m]<<endl; } if(a==9) { int m=(b%2)-1; if(m==-1) m=1; cout<<s[9][m]<<endl; } } return 0; }
转载请注明原文地址: https://www.6miu.com/read-34939.html

最新回复(0)