gpt4 book ai didi

c++ - 如何一步获得商和余数?

转载 作者:IT老高 更新时间:2023-10-28 21:55:44 26 4
gpt4 key购买 nike

Possible Duplicate:
Divide and Get Remainder at the same time?

是否可以在一个步骤中同时获得整数除法的商和余数,即不执行两次整数除法?

最佳答案

div 会这样做。见 reference和例子:

/* div example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
div_t divresult;
divresult = div (38,5);
printf ("38 div 5 => %d, remainder %d.\n", divresult.quot, divresult.rem);
return 0;
}

输出:

38 div 5 => 7, remainder 3.

编辑:

C 规范说:

7.20 通用实用程序

The types declared are size_t and wchar_t (both described in 7.17),
div_t
which is a structure type that is the type of the value returned by the div function,
ldiv_t
which is a structure type that is the type of the value returned by the ldiv function, and
lldiv_t
which is a structure type that is the type of the value returned by the lldiv function.

...但没有说明div_t的定义是什么。

关于c++ - 如何一步获得商和余数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8318272/

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