C++高精度加减乘除和求余数
1.高精度加法
string add(string a, string b)//只限两个非负整数相加
{
string ans;
int nb[100010] = {0};
int na[100010] = {0};
int la = a.size(), lb = b.size();
for (int i = 0; i < la; i++) na[la - 1 - i] = a[i] - '0';
for (int i = 0; i < lb; i++) nb[lb - 1 - i] = b[i] - '0';
int lmax = la > lb ? la : lb;
for (int i = 0; i < lmax; i++)
na[i] += nb[i], na[i + 1] += na[i] / 10, na[i] %= 10;
if (na[lmax]) lmax++;
for (int i = lmax - 1; i >= 0; i--) ans += na[i] + '0';
return ans;
}
2.高精度减法
#include<bits/stdc++.h>
using namespace std;
int size = 100010;
string minuses(string a, string b) {
int x[size], y[size], la = a.length(), lb = b.length(), lc, i;
memset(y, 0, sizeof(y));
lc = max(la, lb);
string ans;
for (i = 0; i < la; i++)x[la - i - 1] = a[i] - 48;
for (i = 0; i < lb; i++)y[lb - i - 1] = b[i] - 48;
for (i = 0; i < lc; i++) {
x[i] -= y[i];
if (x[i] < 0)x[i + 1]--, x[i] += 10;
}
while (!x[lc - 1] && lc > 1)lc--;
for (i = lc - 1; i + 1; i--)ans.push_back(x[i] + 48);
return ans;
}
bool cmp(string a, string b) {
int i;
if (a.length() < b.length())return 1;
if (a.length() > b.length())return 0;
for (i = 0; i < a.length(); i++)
if (a[i] < b[i])return 1;
return 0;
}
int main() {
string n, m;
cin >> n >> m;
if (cmp(n, m)) {
cout << "-";
swap(n, m);
}
cout << minuses(n, m) << endl;
}
3.高精度乘法
int N = 100010;
string _mul(char sa[], char sb[]) {
if (sa[0] == '0' || sb[0] == '0') return "0";
string ans = "";
int a[N], b[N], c[N];
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
int la = strlen(sa);
int lb = strlen(sb);
for (int i = 0; i < la; i++)
a[la - i - 1] = sa[i] - '0';
for (int i = 0; i < lb; i++)
b[lb - i - 1] = sb[i] - '0';
for (int i = 0; i < la; i++)
for (int j = 0; j < lb; j++) {
int f, w, jw;//i和j位相乘存储在答案的i+j位当中
f = a[i] * b[j];
jw = f / 10;
f = f % 10;
w = i + j;//jw表示进位 f表示进位后剩下的数
c[w] = c[w] + f;
jw += c[w] / 10;
c[w] = c[w] % 10;
c[w + 1] = c[w + 1] + jw;
}
int lc = la + lb;
while (c[lc - 1] == 0 && lc > 0) lc--;//前面有解释
for (int i = lc - 1; i >= 0; i--) {
char tmp = c[i] + '0';
ans = ans + tmp;
}
return ans;
}
4.高精度除法
传入参数:分别为被除数,除数以及选择返回商还是余数( 1求商2求余 )
int L = 100010;
int sub1(int *a, int *b, int La, int Lb) {
if (La < Lb)
return -1;//如果a小于b,则返回-1
if (La == Lb) {
for (int i = La - 1; i >= 0; i--)
if (a[i] > b[i])
break;
else if (a[i] < b[i])
return -1;//如果a小于b,则返回-1
}
for (int i = 0; i < La; i++) //高精度减法
{
a[i] -= b[i];
if (a[i] < 0)
a[i] += 10, a[i + 1]--;
}
for (int i = La - 1; i >= 0; i--)
if (a[i])
return i + 1;//返回差的位数
return 0;//返回差的位数
}
string div(string n1, string n2, int nn)
//n1,n2是字符串表示的被除数,除数,nn是选择返回商还是余数
{
string s, v;//s存商,v存余数
int a[L], b[L], r[L],
La = n1.size(), Lb = n2.size(), i, tp = La;
//a,b是整形数组表示被除数,除数,tp保存被除数的长度
fill(a, a + L, 0);
fill(b, b + L, 0);
fill(r, r + L, 0);//数组元素都置为0
for (i = La - 1; i >= 0; i--)
a[La - 1 - i] = n1[i] - '0';
for (i = Lb - 1; i >= 0; i--)
b[Lb - 1 - i] = n2[i] - '0';
if (La < Lb || (La == Lb && n1 < n2)) {
if (nn == 1) return "0";
if (nn == 2) return n1;
}//如果a<b,则商为0,余数为被除数
int t = La - Lb;//除被数和除数的位数之差
for (int i = La - 1; i >= 0; i--) //将除数扩大10^t倍
if (i >= t)
b[i] = b[i - t];
else
b[i] = 0;
Lb = La;
for (int j = 0; j <= t; j++) {
int temp;
//如果被除数比除数大继续减
while ((temp = sub1(a, b + j, La, Lb - j)) >= 0)
{
La = temp;r[t - j]++;
}
}
for (i = 0; i < L - 10; i++)
r[i + 1] += r[i] / 10, r[i] %= 10;//统一处理进位
while (!r[i])
i--;//将整形数组表示的商转化成字符串表示的
while (i >= 0)
s += r[i--] + '0';
//cout<<s<<endl;
i = tp;
while (!a[i])
i--;//将整形数组表示的余数转化成字符串表示的
while (i >= 0)
v += a[i--] + '0';
if (v.empty())
v = "0";
//cout<<v<<endl;
if (nn == 1) return s;
if (nn == 2) return v;
}
本文作者:HDAWN
版权声明:本文采用
CC BY-NC-SA 3.0 CN协议进行许可