将字符串中的 bigint 除以 2
Dividing bigint presented in string to 2
我正在编写一个函数,将 String 中呈现的 Big Int(128 位)数字除以 2。
例如:8113 是一个字符串 ="8113"
我知道在 du != 0 的情况下我的函数错误。当我修复输入的代码时,其他输入会出错。
请给我一些解决方案的建议。如果我的算法很糟糕,告诉我更好的,然后我可以用另一种方式改变我的功能。
int CTOI(char a)
{
return a - 48;
}
char ITOC(int a)
{
if (a == 0)
return '0';
return a + '0';
}
int creDec(int a, int b)
{
return (a * 10) + b;
}
string divide2(string str)
{
string temp = str, t2;
int du, kq;
du = CTOI(temp[0]);
if (du == 1) {
temp.erase(0, 1);
while (temp[0] != 0)
{
du = creDec(du, CTOI(temp[0]));
if (du == 1)
{
temp.erase(0, 1);
}
else
temp.erase(0, 1);
kq = du / 2;
t2 += ITOC(kq);
du = du - kq * 2;
}
}
else
{
while (temp[0] != 0)
{
if (du == 1)
{
temp.erase(0, 1);
du = creDec(du, CTOI(temp[0]));
}
kq = du / 2;
t2 += ITOC(kq);
du = du - kq * 2;
temp.erase(0, 1);
du = creDec(du, CTOI(temp[0]));
}
}
return t2;
}
你似乎给函数增加了很多不必要的复杂性。这是一个适用于我的测试用例的简化函数。
string divide2(string str)
{
string ret; // Object to be returned.
int rem = 0; // Keep track of remainders.
// Iterate over the characters of the input string.
auto iter = str.begin();
auto end = str.end();
for ( ; iter != end; ++iter )
{
int n1 = CTOI(*iter); // The number from the string.
int n2 = creDec(rem, n1); // The number after we account for the remainder.
int n3 = n2/2; // Result of the division.
rem = n2%2; // Remainder of the division.
if ( ret.empty() && n3 == 0 )
{
// Do nothing. There is no need to addd leading zeros to ret.
}
else
{
// Add the character corresponding to n3 to ret.
ret.push_back(ITOC(n3));
}
}
// If the return value is an empty string, return "0".
if ( ret.empty() )
{
return "0";
}
else
{
return ret;
}
}
最好还是使用 range-for
循环来遍历字符串的字符。
for ( char ch : str )
{
int n1 = CTOI(ch); // The number from the string.
...
我正在编写一个函数,将 String 中呈现的 Big Int(128 位)数字除以 2。 例如:8113 是一个字符串 ="8113" 我知道在 du != 0 的情况下我的函数错误。当我修复输入的代码时,其他输入会出错。 请给我一些解决方案的建议。如果我的算法很糟糕,告诉我更好的,然后我可以用另一种方式改变我的功能。
int CTOI(char a)
{
return a - 48;
}
char ITOC(int a)
{
if (a == 0)
return '0';
return a + '0';
}
int creDec(int a, int b)
{
return (a * 10) + b;
}
string divide2(string str)
{
string temp = str, t2;
int du, kq;
du = CTOI(temp[0]);
if (du == 1) {
temp.erase(0, 1);
while (temp[0] != 0)
{
du = creDec(du, CTOI(temp[0]));
if (du == 1)
{
temp.erase(0, 1);
}
else
temp.erase(0, 1);
kq = du / 2;
t2 += ITOC(kq);
du = du - kq * 2;
}
}
else
{
while (temp[0] != 0)
{
if (du == 1)
{
temp.erase(0, 1);
du = creDec(du, CTOI(temp[0]));
}
kq = du / 2;
t2 += ITOC(kq);
du = du - kq * 2;
temp.erase(0, 1);
du = creDec(du, CTOI(temp[0]));
}
}
return t2;
}
你似乎给函数增加了很多不必要的复杂性。这是一个适用于我的测试用例的简化函数。
string divide2(string str)
{
string ret; // Object to be returned.
int rem = 0; // Keep track of remainders.
// Iterate over the characters of the input string.
auto iter = str.begin();
auto end = str.end();
for ( ; iter != end; ++iter )
{
int n1 = CTOI(*iter); // The number from the string.
int n2 = creDec(rem, n1); // The number after we account for the remainder.
int n3 = n2/2; // Result of the division.
rem = n2%2; // Remainder of the division.
if ( ret.empty() && n3 == 0 )
{
// Do nothing. There is no need to addd leading zeros to ret.
}
else
{
// Add the character corresponding to n3 to ret.
ret.push_back(ITOC(n3));
}
}
// If the return value is an empty string, return "0".
if ( ret.empty() )
{
return "0";
}
else
{
return ret;
}
}
最好还是使用 range-for
循环来遍历字符串的字符。
for ( char ch : str )
{
int n1 = CTOI(ch); // The number from the string.
...