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.
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
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->#include <iostream>
using namespace std;
int main()
{
int a, b, result = 1, temp, has = 0, pos, cap = 0;
int yushu[10];
while (cin >> a >> b)
{
temp = b;
has = 0;
pos = b;
cap = 0;
for(int i = 0; i < 10; i++)
{
yushu[i] = 0;
}
while(a > 9)
{
a %= 10;
}
while(temp--)
{
result *= a;
while(result > 9)
{
result %= 10;
}
for(int i = 0; i < 10; i++)
{
if(yushu[i] == result)
{
has = 1;
break;
}
}
if(has == 1)
break;
else
{
yushu[cap] = result;
cap++;
}
}
if(has == 1 && cap != 0)
{
pos = b % (cap) - 1;
if(pos == -1)
pos = cap - 1;
cout << yushu[pos] << endl;
}
else
if(has != 1)
cout << result << endl;
if(has == 1 && cap == 0)
cout << 0 << endl;
result = 1;
a = 0;
b = 0;
for(int i = 0; i < 10; i++)
{
yushu[i] = 0;
}
}
return 0;
}
using namespace std;
int main()
{
int a, b, result = 1, temp, has = 0, pos, cap = 0;
int yushu[10];
while (cin >> a >> b)
{
temp = b;
has = 0;
pos = b;
cap = 0;
for(int i = 0; i < 10; i++)
{
yushu[i] = 0;
}
while(a > 9)
{
a %= 10;
}
while(temp--)
{
result *= a;
while(result > 9)
{
result %= 10;
}
for(int i = 0; i < 10; i++)
{
if(yushu[i] == result)
{
has = 1;
break;
}
}
if(has == 1)
break;
else
{
yushu[cap] = result;
cap++;
}
}
if(has == 1 && cap != 0)
{
pos = b % (cap) - 1;
if(pos == -1)
pos = cap - 1;
cout << yushu[pos] << endl;
}
else
if(has != 1)
cout << result << endl;
if(has == 1 && cap == 0)
cout << 0 << endl;
result = 1;
a = 0;
b = 0;
for(int i = 0; i < 10; i++)
{
yushu[i] = 0;
}
}
return 0;
}
本文介绍了一种解决特定数学问题的算法:给定两个正整数a和b,如何高效地计算a^b的最后一位数字。通过使用余数的概念并限制计算范围来优化算法,避免了直接计算大数值的复杂性和资源消耗。
1569

被折叠的 条评论
为什么被折叠?



