如何在一步中得到商和余数
How can I get the quotient and the remainder in a single step?
可能重复:
同时进行除法运算和求余数运算?
是否可以在一步中同时获得整数除法的商和余数,即不执行两次整数除法?
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()
(和ldiv
,甚至可能是lldiv
(的标准函数可以做到这一点。
相关文章:
- 维护unordered_map但同时每一步都需要最低的映射值
- 如何在最后一步使用CryptDecrypt解决NTE_BAD_DATA
- 如何在 C++ 中返回到上一步
- 如何一步一步地正确解释此代码?(编程新手)
- 通过GL_FLOAT,GL_UNSIGNED_INT一步
- 将隐式促销添加到复杂类,最后一步
- 如何在Linux / Windows操作系统上使用文件*.cpp一步编译下面的代码
- 检查下一步是检查员
- 改进malloc()算法的下一步是什么
- 如何在c++骰子游戏中存储第一个值,并在下一步进行比较
- 如何一步一步地遍历目录树
- 有没有一个调试器可以让我在代码中后退一步
- 若在下一步中对象被破坏,为什么不自动移动呢
- 第4章 斯特劳斯特鲁普钻头一个具有挑战性的一步(至少对我来说!
- 一步一步寻找选择排序大θ符号的过程
- 两步到一步的初始化和错误处理
- 带有下一步/后退按钮的Qt对话框/小部件
- 无法理解此代码的最后一步
- 只有当我在 c++ 中按 Enter 键(线程)时,代码如何进入下一步
- 如何在一步中得到商和余数