C++ ldiv() 函数
ldiv() 函数是 cstdlib 头文件的库函数。它用于整数除法,它接受两个参数(分子和分母)并返回一个包含quot
(商) 和rem
(余)。
ldiv() 函数的语法:
C++11:
ldiv_t ldiv (long int numer, long int denom);
参数:
numer
– 表示分子的值。denom
– 表示分母的值。
返回值:
这个函数的返回类型是ldiv_t
, 返回一个包含quot
(商)和rem
(余)。
例:
Input: long int n = 123456789; long int d = 12345678; ldiv_t result; Function call: result = ldiv(n, d); Output: result.quot = 10 result.rem = 9
C++代码演示ldiv()函数的例子
// C++ code to demonstrate the example of
// ldiv() function
#include <iostream>
#include <cstdlib>
using namespace std;
// main() section
int main()
{
long int n = 123456789;
long int d = 12345678;
ldiv_t result;
result = ldiv(n, d);
cout << "Numerator :" << n << endl;
cout << "Denominator:" << d << endl;
cout << "Quotient :" << result.quot << endl;
cout << "Remainder :" << result.rem << endl;
cout << endl;
n = 999988887777;
d = 1112223334;
result = ldiv(n, d);
cout << "Numerator :" << n << endl;
cout << "Denominator:" << d << endl;
cout << "Quotient :" << result.quot << endl;
cout << "Remainder :" << result.rem << endl;
cout << endl;
n = 100100234;
d = 9878762536;
result = ldiv(n, d);
cout << "Numerator :" << n << endl;
cout << "Denominator:" << d << endl;
cout << "Quotient :" << result.quot << endl;
cout << "Remainder :" << result.rem << endl;
cout << endl;
return 0;
}
输出
Numerator :123456789 Denominator:12345678 Quotient :10 Remainder :9 Numerator :999988887777 Denominator:1112223334 Quotient :899 Remainder :100110511 Numerator :100100234 Denominator:9878762536 Quotient :0 Remainder :100100234
参考:C++ ldiv() 函数
相关用法
- C++ ldexp()用法及代码示例
- C++ list assign()用法及代码示例
- C++ llround()用法及代码示例
- C++ log2()用法及代码示例
- C++ lrint() and llrint()用法及代码示例
- C++ list::remove()、list::remove_if()用法及代码示例
- C++ lrint()用法及代码示例
- C++ lldiv()用法及代码示例
- C++ list back()用法及代码示例
- C++ list merge()用法及代码示例
- C++ list remove()用法及代码示例
- C++ log1p()用法及代码示例
- C++ list push_back()用法及代码示例
- C++ list::operator=用法及代码示例
- C++ list erase()用法及代码示例
- C++ list::empty()、list::size()用法及代码示例
- C++ list resize()用法及代码示例
- C++ list pop_front()用法及代码示例
- C++ list empty()用法及代码示例
- C++ list crbegin()、crend()用法及代码示例
注:本文由纯净天空筛选整理自 ldiv() Function with Example in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。