如何一步求出商和余数?

发布于 2024-12-18 19:05:42 字数 217 浏览 1 评论 0原文

可能的重复:
同时除法并获取余数?

是否可能一步获得整数除法的商和余数,即不执行两次整数除法?

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

Is it possible to get both the quotient and the remainder of integer division in a single step, i.e., without performing integer division twice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

智商已欠费 2024-12-25 19:05:42

div 将执行此操作。请参阅 参考 和示例:

/* 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 的定义是什么。

div will do this. See reference and example:

/* 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;
}

Output:

38 div 5 => 7, remainder 3.

EDIT:

The C Specification says:

7.20 General utilities

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.

... but it doesn't say what the definition of div_t is.

少女七分熟 2024-12-25 19:05:42

是的,有一个名为 div() 的标准函数(和ldiv,甚至可能是lldiv)来实现这一点。

Yes, there is a standard function called div() (and ldiv, and maybe even lldiv) that does this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文