📅  最后修改于: 2020-09-25 08:48:39             🧑  作者: Mango
div() 函数在
数学上
quot * y + rem = x
div_t div(int x, int y);
ldiv_t div(long x, long y);
lldiv_t div(long long x, long long y);
它有两个参数x
和y
,并返回整数商和x除以y的余数。
的商quot
是表达式x / y的结果。其余的rem
是表达式x%y的结果。
div() 函数返回div_t
, ldiv_t
或lldiv_t
类型的结构。每个结构都包含两个成员: quot
和rem
。它们的定义如下:
div_t:
struct div_t {
int quot;
int rem;
};
ldiv_t:
struct ldiv_t {
long quot;
long rem;
};
lldiv_t:
struct lldiv_t {
long long quot;
long long rem;
};
#include
#include
using namespace std;
int main()
{
div_t result1 = div(51, 6);
cout << "Quotient of 51/6 = " << result1.quot << endl;
cout << "Remainder of 51/6 = " << result1.rem << endl;
ldiv_t result2 = div(19237012L,251L);
cout << "Quotient of 19237012L/251L = " << result2.quot << endl;
cout << "Remainder of 19237012L/251L = " << result2.rem << endl;
return 0;
}
运行该程序时,输出为:
Quotient of 51/6 = 8
Remainder of 51/6 = 3
Quotient of 19237012L/251L = 76641
Remainder of 19237012L/251L = 121