编写一个函数求一个正整数的任意次方的最后三位数,即求x^y的最后三位数。
时间: 2024-04-29 22:24:46 浏览: 141
可以使用快速幂算法,将x进行y次幂运算。每一次乘法运算时,只需要将结果的最后三位保留即可。以下是示例代码:
```
int lastThreeDigits(int x, int y) {
int res = 1;
while (y > 0) {
if (y % 2 == 1) {
res = res * x % 1000;
}
x = x * x % 1000;
y /= 2;
}
return res;
}
```
例如,计算2^10的最后三位数,调用函数lastThreeDigits(2, 10),得到的结果为24。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![text/plain](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)