a^b问题(南阳oj473)

A^B Problem

时间限制:1000 ms  |  内存限制:65535 KB

难度:2

描述

Give you two numbers a and b,how to know the a^b's the last digit number.It looks so easy,but everybody is too lazy to slove this problem,so they remit to you who is wise.

输入

There are mutiple test cases. Each test cases consists of two numbers a and b(0<=a,b<2^30)

输出

For each test case, you should output the a^b's last digit number.

样例输入

7 66
8 800

样例输出

9
6

提示

There is no such case in which a = 0 && b = 0。

来源

hdu

上传者

ACM_丁国强

这道题目 输入a,b,求a的b次方,b通过对1-9的次方规律可以通过(b-1)%4+1来减小;

而a,因为题目要的是末尾位数,只需要a的个位即可;

多组数据,当a=0,b=0时可以继续运行而不是终止。

#include <iostream>
#include<stdio.h>
using namespace std;
int main()
{
    long long int a,b;
    int i,n,c;
    while(scanf("%lld %lld",&a,&b)!=EOF)
    {
        if(b==0)
        {
            printf("%d\n",1);
            continue;
        }
            b=(b-1)%4+1;
            n=1;
            c=a%10;
            for(i=1;i<=b;i++)
            {
                n=n*c;
            }
            printf("%d\n",n%10);

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/acm147258369/article/details/83821411