zoukankan      html  css  js  c++  java
  • [ACM

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

    运行截图:


  • 相关阅读:
    C/C++程序内存的分配
    【解题报告】UVA101 The Blocks Problem
    洗牌算法
    【持续更新】算法竞赛常用模板
    北京邮电大学2021乐理公共选修课期末考试
    Oracle通用大数据量存储过程分页修正版
    (转)NHibernate 3.0在PetShop 3层架构中的应用
    Json.Net学习笔记
    JS获得浏览器高度和宽度参数
    jQuery数组处理汇总
  • 原文地址:https://www.cnblogs.com/sr1993/p/3697824.html
Copyright © 2011-2022 走看看