大数运算有很多种形式,今天我们将不同的情况进行归纳总结:(Ps:已经写好string和int之间转换的函数,以及去掉不必要的0之类的辅助小函数 )
[0]:首先实现大数求余的情况1 : A%B(A很大B可以用整数表示),算法也很好理解,用归纳的思想就可以证明.
int Mod(string a, int b)
{
int t = 0 ;
for (int i = 0 ; i != a.length(); ++i)
t = (t * 10 + a[i] - '0' ) % b;
return t;
}
[1]:然后实现两个很大的正数相减,如果需要实现负数的话需要再实现正数的加法,然后进行转换.
string Subtract(string a, string b)
{
a = remove_pre_zero(a);
b = remove_pre_zero(b);
if (a.length() < b.length())
return "-" + Subtract(b, a);
int n = b.length();
int b_pos = n - 6 ;
int a_pos = b_pos + a.length() - n;
int temp, borrow = 0 ;
string ans;
bool test = false ;
for (; !test; b_pos -= 6 )
{
auto mod = 6 ;
if (b_pos <= 0 )
{
mod = b_pos + 6 ;
b_pos = 0 ,test = true ;
}
a_pos = b_pos + a.length() - n;
temp =string_to_int(a.substr(a_pos,mod))
- string_to_int(b.substr(b_pos , mod)) - borrow;
borrow = 0 ;
if (temp < 0 )
{
if (a_pos>0 )
temp += string_to_int("1" +string (mod,'0' ));
borrow = 1 ;
}
auto s = int_to_string(temp);
if (temp>=0 )
ans = string (mod - s.length(), '0' ) + s + ans;
else
ans = s + ans;
}
if (a_pos > 0 )
{
if (borrow == 1 )
ans = Subtract(a.substr(0 , a_pos), string (a_pos - 1 , '0' ) + '1' ) + ans;
else
ans = a.substr(0 , a_pos) + ans;
}
return remove_pre_zero(ans);
}
[2]:实现两个大数求求余 A%B(A,B都很巨大,都只能用string来表示),这个函数就是模拟大数除法来计算的,需要用到减法的函数
string Mod(string a, string b)
{
if (a.length () < b.length ())
return a;
int n = b.length ();
int k;
string temp = a.substr(0 , n);
string old_ans,mod ;
for (int i = n;i!=a.length ()+1 ; ++i)
{
for ( k = 0 ;temp[0 ] !='-'; ++k)
{
old_ans = temp;
temp = Subtract(temp,b);
}
temp = old_ans+a.substr(i, 1 );
--k;
if (i == a.length ())
mod = old_ans;
}
return mod ;
}
[3]大数乘法和加法:在前面的博文已经实现过,这次直接照搬