📜  C++ STL中的lldiv()函数

📅  最后修改于: 2021-05-30 14:14:33             🧑  作者: Mango

lldiv()是C++ STL中的内置函数,它为我们提供了两个数字除法的商和余数。

语法

lldiv(n, d)

参数:该函数接受两个强制性参数,如下所述:

  • n:指定股息。数据类型可以是long long或long long int。
  • d:指定除数。数据类型可以是long long或long long int。

返回值:该函数返回lldiv_t类型的结构,该结构由两个成员组成: quot和rem ,其中quot是商, rem是余数。该结构定义如下:

struct lldiv_t {
    long long quot;
    long long rem;
};

下面的程序说明了上述函数:

程序1

// C++ program to illustrate the
// lldiv() function
#include 
#include 
using namespace std;
  
int main()
{
    long long n = 1000LL;
    long long d = 50LL;
  
    lldiv_t result = lldiv(n, d);
  
    cout << "Quotient of " << n << "/" << d
         << " = " << result.quot << endl;
  
    cout << "Remainder of " << n << "/" << d
         << " = " << result.rem << endl;
  
    return 0;
}
输出:
Quotient of 1000/50 = 20
Remainder of 1000/50 = 0

程序2

// C++ program to illustrate
// the lldiv() function
#include 
#include 
using namespace std;
  
int main()
{
    long long int n = 251987LL;
    long long int d = 68LL;
  
    lldiv_t result = lldiv(n, d);
  
    cout << "Quotient of " << n << "/" << d
         << " = " << result.quot << endl;
    cout << "Remainder of " << n << "/" << d
         << " = " << result.rem << endl;
  
    return 0;
}
输出:
Quotient of 251987/68 = 3705
Remainder of 251987/68 = 47
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”