给定一个很大的数字,例如num,另一个很大的数字,则m。任务是使用除法运算来打印商,使用模数来打印大数的余数。
输出应为Remainder = xxx; 商= yyy
假设我们有一个输入num =字符串num =“ 14598499948265358486”,而其他输入m = 487,则余数为430,商为29976385930729688。
Input: num = “214755974562154868” m = 17 Output: Remainder = 15 quotient = 12632704386009109 Input: num =“214” m = 5 Output: Remainder = 4 Quotient = 42
我们将用来解决给定问题的方法-
最初将mod设置为0。
从右边开始,我们必须使用mod来找到它:mod =(mod * 10 + digit)%m。
通过quo [i] = mod / m查找商,其中i是商的位置编号。
Start Step 1 -> Declare long long ll Step 2 -> In function void quotientremainder(string num, ll m) Declare a vector<int> vec Set ll mod = 0 Loop For i = 0 and i < num.size() and i++ Set digit = num[i] - '0' Set mod = mod * 10 + digit Set quo = mod / m Call vec.push_back(quo) Set mod = mod % m End loop Print remainder value which is in mod Set zeroflag = 0 Loop For i = 0 and i < vec.size() and i++ If vec[i] == 0 && zeroflag == 0 then, Continue End If zeroflag = 1 print the value of vec[i] End For Return Step 3 -> In function int main() Declare and assign num = "14598499948265358486" Declare and assign ll m = 487 Call function quotientremainder(num, m) Stop
#include <bits/stdc++.h> using namespace std; typedef long long ll; //计算模量的功能 void quotientremainder(string num, ll m) { //存储大数模 vector<int> vec; ll mod = 0; //进行逐步划分 for (int i = 0; i < num.size(); i++) { int digit = num[i] - '0'; //更新模 //当前数字。 mod = mod * 10 + digit; //更新商 int quo = mod / m; vec.push_back(quo); //更新mod以进行下一次迭代。 mod = mod % m; } cout << "\nRemainder : " << mod << "\n"; cout << "Quotient : "; //用于删除起始零的标志 bool zeroflag = 0; for (int i = 0; i < vec.size(); i++) { if (vec[i] == 0 && zeroflag == 0) continue; zeroflag = 1; cout << vec[i]; } return; } //主块 int main() { string num = "14598499948265358486"; ll m = 487; quotientremainder(num, m); return 0; }
输出结果
Remainder : 430 Quotient : 29976385930729688