Given positive integers a and b as strings, evaluate a / b and return
the quotient and the remainder as strings in the form [quotient,
remainder] (vector {quotient, remainder} in C++).a and b can be very large (at the order of 10^150 to 10^200) As usual,
your result should not have leading 0s require is disabled in
JavaScript. Do it yourself ?
翻译:
给定两个字符串,进行除法运算,返回商和余数,注意:除数和被除数可能是极大的值。
思路:
第一步:用减法模拟除法
bool sub_once(std::string& a, const std::string&b)
{
std::string r;
int z = 0;
int i = 0;
int iaLen = a.size();
int ibLen = b.size();
for (;i < iaLen;i++)
{
char x = a[i];
char y = '0';
if (i <ibLen)
{
y = b[i];
}
int s = x - y - z;
if (s < 0)
{
s += 10;
z = 1;
}
else
{
z = 0;
}
r += std::to_string(s);
}
if (i < b.size())
{
return false;
}
if (z ==1)
{
return false;
}
a = r;
return true;
}
第二步:
按照平时计算除法的方式,从被除数的左边开始,寻找第一个大于除数的字符串,进行字符串分割,如果没找到则该被除数就是余数,否则,计算第一个大于除数的字符串的商,进行保存,之后继续寻找下一个大于除数的字符串。应当注意几种特殊情况,
1:在两个字符串的之间的位数,应当记得补0
2:在前面的字符串计算中,余数为零,此时应该持续添加0直到字符串不为0,然后继续之前的步骤。
3:在前面的计算中,余数不为空,则后面应当将余数添加到剩余的字符串的前面。
4:应当确保商的长度,与从第一个字符串的最后一位的下标开始计算的长度相等.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void remove_zero(std::string& str)
{
while (str.size() > 1)
{
if (str[0] =='0')
{
str.erase(str.begin());
}
else
{
return;
}
}
}
bool sub_once(std::string& a, const std::string&b)
{
std::string r;
int z = 0;
int i = 0;
int iaLen = a.size();
int ibLen = b.size();
for (;i < iaLen;i++)
{
char x = a[i];
char y = '0';
if (i <ibLen)
{
y = b[i];
}
int s = x - y - z;
if (s < 0)
{
s += 10;
z = 1;
}
else
{
z = 0;
}
r += std::to_string(s);
}
if (i < b.size())
{
return false;
}
if (z ==1)
{
return false;
}
a = r;
return true;
}
int getLeftDivLen(std::string &a, std::string &b)
{
int iLena = a.size();
int iLenb = b.size();
for (int i=0;i < iLena;i++)
{
std::string strTemp;
strTemp.assign(a.begin(), a.begin() + i+1);
std::reverse(strTemp.begin(), strTemp.end());
if (sub_once(strTemp,b))
{
return i;
}
}
return -1;
}
std::vector<std::string> divide_strings(std::string a, std::string b) {
remove_zero(a);
remove_zero(b);
std::vector<std::string> result(2);
int iLena = a.size();
int iLenb = b.size();
if (iLenb >iLena)
{
result[0] = "0";
result[1] = a;
return result;
}
std::reverse(b.begin(), b.end());
int iIndexa = getLeftDivLen(a,b);
std::string strCount;
while (iIndexa != -1)
{
std::string strTempStr = a.substr(0, iIndexa +1);
std::reverse(strTempStr.begin(), strTempStr.end());
int iTempCount = 0;
while (sub_once(strTempStr,b))
{
iTempCount++;
}
strCount += std::to_string(iTempCount);
std::string strRemain = strTempStr;
std::reverse(strRemain.begin(), strRemain.end());
remove_zero(strRemain);
int iRemainSize = 0;
a.erase(a.begin(), a.begin() + iIndexa + 1);
int iaRemainLen = a.size();
if (strRemain !="0")
{
a.insert(a.begin(), strRemain.begin(), strRemain.end());
iRemainSize = strRemain.size();
}
else
{
while (a.size())
{
if (a[0] =='0')
{
strCount += "0";
a.erase(a.begin());
}
else
{
break;
}
}
iaRemainLen = a.size();
}
iIndexa = getLeftDivLen(a, b);
if (iIndexa ==-1)
{
while (iaRemainLen >0)
{
strCount += "0";
iaRemainLen--;
}
}
else
{
int iSub = iIndexa - iRemainSize;
while (iSub > 0)
{
strCount += "0";
iSub--;
}
}
}
result[0] = strCount;
result[1] = a;
if (result[0].empty())
{
result[0] = "0";
}
if (result[1].empty())
{
result[1] = "0";
}
else
{
remove_zero(result[1]);
}
return result;
}