[ACM - 数论]A hard puzzle

A hard puzzle

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 25   Accepted Submission(s) : 7

Font: Times New Roman | Verdana | Georgia

Font Size:  

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.

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

Author

eddy



解题思路:

一个数不管多大,我们关心的只是它的个位数,它再几次方也只关心个位的数字,个位数字0到9次方找周期,辅助图如下:


根据规矩,可以很容易写出代码。


代码:

#include <iostream>
using namespace std;
int main()
{
    int a,b;
    while(cin>>a>>b)
    {

            int t;
            t=a%10;//t为个位数
            switch(t)
            {
                case 0:{cout<<0<<endl;break;}
                case 1:{cout<<1<<endl;break;}
                case 2:
                    {
                    if(b%4==1)
                        cout<<2<<endl;
                    else if(b%4==2)
                        cout<<4<<endl;
                    else if(b%4==3)
                        cout<<8<<endl;
                    else if(b%4==0)
                        cout<<6<<endl;
                    break;

                    }
                case 3:
                    {
                        if(b%4==0)
                            cout<<1<<endl;
                        else if(b%4==1)
                            cout<<3<<endl;
                        else if(b%4==2)
                            cout<<9<<endl;
                        else if(b%4==3)
                            cout<<7<<endl;
                        break;
                    }
                case 4:
                    {
                        if(b%2==1)
                            cout<<4<<endl;
                        if(b%2==0)
                            cout<<6<<endl;
                        break;
                    }
                case 5:{cout<<5<<endl;break;}
                case 6:{cout<<6<<endl;break;}
                case 7:
                    {
                        if(b%4==0)
                            cout<<1<<endl;
                        else if(b%4==1)
                            cout<<7<<endl;
                        else if(b%4==2)
                            cout<<9<<endl;
                        else if(b%4==3)
                            cout<<3<<endl;
                        break;
                    }
                case 8:
                    {
                        if(b%4==0)
                            cout<<6<<endl;
                        else if(b%4==1)
                            cout<<8<<endl;
                        else if(b%4==2)
                            cout<<4<<endl;
                        else if(b%4==3)
                            cout<<2<<endl;
                        break;
                    }
                case 9:
                    {
                        if(b%2==0)
                            cout<<1<<endl;
                        else if(b%2==1)
                            cout<<9<<endl;
                        break;
                    }
            }
    }
    return 0;
}

运行截图:


posted @ 2013-11-09 19:16  同学少年  阅读(339)  评论(0编辑  收藏  举报