一、题目如下
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
题目大意就是要求 a的b次方的个位数。
二、解题思路:
这题的关键在于末尾计算,大数字的乘法个位只与个位数字有关,即不管数字怎么样,我们只需要个位即可,再由数学归纳得1,5,6这几个数的次方个位数恒定,而,2,4,8三个数有规律可转化,同理3,9也一样,所以我们只需要去找2,3,7这三个数的次方规律即可,这三个数的次方中每4次方个位数都是一个循环,因此,利用这个即可化简,解答如下:
#include<iostream>
#include<math.h>
using namespace std;
int digit(int a,int b)
{
int n;
if(a==4)
{
a=2;
b=b*2;
}
else if(a==8)
{
a=2;
b=b*3;
}
else if(a==9)
{
a=3;
b=b*2;
}
if(b%4!=0)
b=b%4;
else b=4;
n=pow(a,b);
if(n>10)
n=n%10;
return n;
}
int main()
{
int a,b,print;
ios::sync_with_stdio(false);
while(cin>>a>>b)
{
a=a%10;
switch(a)
{
case 1:
print=1; break;
case 5:
print=5; break;
case 6:
print=6; break;
default: print=digit(a,b); break;
}
cout<<print<<endl;
}
return 0;
}