gpt4 book ai didi

c++ - 不使用 '/' 运算符的除法

转载 作者:行者123 更新时间:2023-11-30 21:20:03 27 4
gpt4 key购买 nike

我不能使用“/”和循环,我必须除以一些数字。操作数是 32 位的,我无法使用递归。

我想过使用 shr,但它只给我 2^n 除法,而且也不会保存提醒。

有什么想法吗?

最佳答案

#include <stdio.h>
#include <stdlib.h>

int main()
{
div_t output;

output = div(27, 4);
printf("Quotient part of (27/ 4) = %d\n", output.quot);
printf("Remainder part of (27/4) = %d\n", output.rem);

output = div(27, 3);
printf("Quotient part of (27/ 3) = %d\n", output.quot);
printf("Remainder part of (27/3) = %d\n", output.rem);

return(0);
}

输出:

Quotient part of (27/ 4) = 6
Remainder part of (27/4) = 3
Quotient part of (27/ 3) = 9
Remainder part of (27/3) = 0

关于c++ - 不使用 '/' 运算符的除法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43854590/

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