我正在尝试实现一个简单的程序,该程序采用基数和指数并输出求幂结果的最后一位,但在线法官说我的程序给出了错误的答案。有什么问题?
PS 程序的约束必须是 700 kb,这在这里不是问题(我可以删除空格和注释并使用单字母变量来避免该问题)
#include <iostream>
using namespace std;
int main()
{
int t; // t - number of test cases
cin >> t;
cin.get();
for (int i = 0; i < t; ++i)
{
int base, exp; // base - base, exp - exponent
cin >> base >> exp;
cin.get();
if (exp == 0)
cout << 1 << endl;
else if (base % 10 == 0)
cout << 0 << endl;
else if (base % 10 == 1)
cout << 1 << endl;
else if (base % 10 == 5)
cout << 5 << endl;
else if (base % 10 == 6)
cout << 6 << endl;
else if (base % 10 == 2 || base % 10 == 3 || base % 10 == 7 || base % 10 == 8)
{
int pattern = exp % 4; // pattern repeats every 4th exponent
int lastDigit = base; // lastDigit - result of program
if (pattern == 0)
pattern = 4;
for (int i = 1; i < pattern; ++i)
lastDigit = (lastDigit * base) % 10;
cout << lastDigit << endl;
}
else if (base % 10 == 4 || base % 10 == 9)
{
int pattern = exp % 2; // pattern repeats every 2nd exponent
int lastDigit = base; // lastDigit 0 result of program
if (pattern == 0)
pattern = 2;
for (int i = 1; i < pattern; ++i)
lastDigit = (lastDigit * base) % 10;
cout << lastDigit << endl;
}
}
}
这里是样品。
INPUT
3 10
6 2
7 3123123
0 1
1 0
0 0
OUTPUT
9
6
3
0
1
1
提前致谢。
编辑: