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.
7 66 8 800
9 6
这是求a^b最后一位的,可以求出0~9循环多少次又回到原来的值,比如2的次方是2 4 8 16 32 周期是4
然后把这些数存起来就是数组
int c[10][5]={
{0},{1},{2,4,8,6},{3,9,7,1},{4,6},{5},
{6},{7,9,3,1},{8,4,2,6},{9,1}
};
#include<iostream>
#include<cmath>
using namespace std;
const
int c[10][5]={
{0},{1},{2,4,8,6},{3,9,7,1},{4,6},{5},
{6},{7,9,3,1},{8,4,2,6},{9,1}
};
int main()
{
int a,b;
int sum,i,k;
while(cin>>a>>b)
{
k=1;
if(a>=10)
a=a%10;
sum=a;
for(i=1;i<=k;i++)//最多循环5次
{
sum*=a;
if(sum>=10)
{
sum%=10;
}
if(sum!=a)
{k=k+1;}
}
if(b%(i-1)==0) cout<<c[a][b%(i-1)+(i-1)-1]<<endl;//注意输出,原来我没有
else cout<<c[a][b%(i-1)-1]<<endl;
}
return 0;
}
另一种:
#include <iostream>
using namespace std;
int main()
{
int a, b, n;
while (cin>>a>>b) {
a %= 10;
b %= 4;
switch(b) {
case 1:
n = a%10;
break;
case 2:
n = a*a%10;
break;
case 3:
n = a*a*a%10;
break;
case 0:
n = a*a*a*a%10;
break;
default:
break;
}
cout<<n<<endl;
}
return 0;
}