A hard puzzle—1097

来源:互联网 发布:软件体系结构风格 编辑:程序博客网 时间:2024/06/04 18:59

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

7 668 800

Sample Output

96

分析
求a的b次方得到的结果的最后一位,其实就是求a的个位数的b次方的最后一位数,例如13的124次方(13^124):3,3×3=9,3×9=27,3×7=21,3×1=3,最后一位总是呈现出一个循环的状态,可以利用这一性质来求解问题,用到一个vector来存储每次的最后一位。

C++代码

/************************************************* Copyright: 武汉大学计算机学院B507 Author: RyanDate: 2015-11-25 Description:求a^b的最后一位 **************************************************/ #include<iostream>#include<vector>using namespace std;int main(){    int a,b;    while(cin>>a>>b) {        //0的0次方没有意义        if(!a && !b)            return 0;        a=a%10; //取a的个位数        //个位为0,1,5,6的数字得到的结果不变        if(a==0 || a==1 || a==5 || a==6){            cout<<a<<endl;        }         else {            vector<int> vec;            vec.push_back(a);            int temp=(a*a)%10;            while(temp!=a) {                vec.push_back(temp);                temp=(a*temp)%10;            }            int res = b%vec.size();            cout<<(res==0?vec.back():vec[res-1])<<endl;        }    }    return 0;   } 

总结
最后代码AC了,一次性通过。

0 0
原创粉丝点击