杭电1097

开始使用暴力算法,结果肯定是time exceed ,后来想到了简便算法,第一个数a只用考虑个位,然后对0-9的每个数进行乘方比较,发现都是以4为周期的函数,结果就很容易得出来了。

代码如下

#include <iostream>
using namespace std;
int main(int argc, char** argv) {
   int a=0;
   int b=0;
   int sum=1;
   while(cin>>a&&cin>>b)
   { 
    a=a%10;
    b=b%4; 
    while(b>0)
    {
        sum=sum*a;
        b--;
    }
    if(b==0)
    {
        sum=sum*a*a*a*a;
    }
       cout<<sum%10<<endl;
       sum=1;
   }

}

猜你喜欢

转载自blog.csdn.net/qq_38271045/article/details/85682260