1. 程式人生 > >HDU1097暴力打表找規律a^b

HDU1097暴力打表找規律a^b

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;
}