gpt4 book ai didi

c++ - 找出数的最后一位数的某次幂

转载 作者:行者123 更新时间:2023-11-28 05:55:33 27 4
gpt4 key购买 nike

我正在尝试使用最简单的方法找出一个数的某次幂的个位数。我知道模幂运算。我正在尝试实现类似于 How to find the units digit of a certain power in a simplest way 的东西,但我的代码显示了 SPOJ 中某些测试用例的错误答案。这是 SPOJ 中问题的链接:http://www.spoj.com/problems/LASTDIG/

#include<iostream>
using namespace std;

int main() {
int a2[] = {6, 2, 4, 8},
a3[] = {1, 3, 9, 7},
a4[] = {6, 4},
a7[] = {1, 7, 9, 3},
a8[] = {6, 8, 4, 2},
a9[] = {1, 9},
t;
long long int b1, b2;

cin >> t;

while (t--) {
cin >> b1 >> b2;
b1 = b1 % 10;
if (b2 == 0)
cout << 1 << endl;
else if (b1 == 0)
cout << 0 << endl;
else if (b2 == 1 || b1 == 5 || b1 == 6)
cout << b1 << endl;
else if (b1 == 2)
cout << a2[b2 % 4] << endl;
else if (b1 == 3)
cout << a3[b2 % 4] << endl;
else if (b1 == 4)
cout << a4[b2 % 2] << endl;
else if (b1 == 7)
cout << a7[b2 % 4] << endl;
else if (b1 == 8)
cout << a8[b2 % 4] << endl;
else if (b1 == 9)
cout << a9[b2 % 2] << endl;
}
return 0;
}

最佳答案

首先

a7[]={1,7,0,3}
^^^

零应该是9

a7[]={1,7,9,3}

还有a9

a9[]={9,1}
^^^

应该是反的

a9[]={1,9}

关于c++ - 找出数的最后一位数的某次幂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34236757/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com