高精度加法
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int a[255], b[255], c[255];
int main()
{
string s1, s2;
int la, lb, len;
cin >> s1 >> s2;
la = s1.size();
lb = s2.size();
for (int i = la - 1; i >= 0; i--) a[la - i - 1] = s1[i] - '0';
for (int i = lb - 1; i >= 0; i--) b[lb - i - 1] = s2[i] - '0';
if(la > lb) len = la;
else len = lb;
for (int i = 0; i < len; i++) c[i] = a[i] + b[i];
for (int i = 0; i < len; i++)
{
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
if (c[len] > 0) len++;
for (int i = len - 1; i >= 0; i--) cout << c[i];
return 0;
}
高精度减法
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int a[10086], b[10086], c[10086];
int main()
{
string s1, s2, s;
char fh;
int la, lb, k;
cin >> s1 >> s2;
if (s1 == s2)
{
cout << "0" << endl;
return 0;
}
la = s1.size();
lb = s2.size();
if (la < lb || ((la == lb) && (s1 < s2)))
{
fh = '-';
s = s1;
s1 = s2;
s2 = s;
}
la = s1.size();
lb = s2.size();
for (int i = la - 1; i >= 0; i--) a[la - i - 1] = s1[i] - '0';
for (int i = lb - 1; i >= 0; i--) b[lb - i - 1] = s2[i] - '0';
k = la - 1;
for (int t = 0; t < la; t++)
{
if (a[t] < b[t])
{
a[t + 1] -= 1;
a[t] += 10;
}
a[t] -= b[t];
}
while (a[k] == 0) k--;
if (fh == '-') cout << fh;
for (int i = k; i >= 0; i--) cout << a[i];
return 0;
}
高精度乘法
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int a[2001], b[2001], c[4001];
int main()
{
string s1, s2;
int la, lb, len;
cin >> s1 >> s2;
la = s1.size();
lb = s2.size();
len = la + lb;
for (int i = la - 1; i >= 0; i--) a[la - i] = s1[i] - '0';
for (int i = lb - 1; i >= 0; i--) b[lb - i] = s2[i] - '0';
for (int i = 1; i <= la; i++)
{
for (int j = 1; j <= lb; j++)
{
c[i + j - 1] += a[i] * b[j];
}
}
for (int i = 1; i <= len; i++)
{
if (c[i] >= 10)
{
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
}
int f = 0;
for (int i = len; i >= 1; i--)
{
if (c[i])
{
len = i;
break;
}
if (i == 1) f = 1;
}
if (f == 1) cout << 0;
else for (int i = len; i >= 1; i--) cout << c[i];
return 0;
}
高精度除法
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 100001;
int a[N], b[N], res[N * 2];
char x[N], y[N];
int la, lb;
void init()
{
la = strlen(x);
lb = strlen(y);
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(res,0,sizeof(res));
for (int i = la - 1; i >= 0; i--) a[la - i - 1] = x[i] - '0';
for (int i = lb - 1; i >= 0; i--) b[lb - i - 1] = y[i] - '0';
}
int substraction(int *p1,int *p2,int len1,int len2)
{
if (len1 < len2) return -1;
bool flag = 0;
if (len1 == len2)
{
for (int i = len1 - 1; i >= 0; i--)
{
if (p1[i] > p2[i]) flag = 1;
else if (p1[i] < p2[i])
{
if (!flag) return -1;
}
}
}
for (int i = 0; i < len1; i++)
{
p1[i] -= p2[i];
if (p1[i] < 0)
{
p1[i] += 10;
p1[i + 1]--;
}
}
for (int i = len1 - 1; i >= 0; i--)
{
if (p1[i]) return i + 1;
}
return 0;
}
void output()
{
for (int i = 0; i < N; i++)
{
if (res[i] >= 10)
{
res[i + 1] += res[i] / 10;
res[i] %= 10;
}
}
bool flag = 0;
for (int i = N - 1; i >= 0; i--)
{
if (flag) cout << res[i];
else if (res[i])
{
cout << res[i];
flag = 1;
}
}
if (!flag) cout << 0;
cout << endl;
}
void divv()
{
init();
if (la < lb)
{
cout << 0 << endl;
return;
}
la = substraction(a, b, la, lb);
if (la < 0)
{
cout << 0 << endl;
return;
}
else if (la == 0)
{
cout << 1 << endl;
return;
}
res[0]++;
int k = la - lb;
if (k < 0)
{
output();
return;
}
else if (k > 0)
{
for (int i = la - 1; i >= 0; i--)
{
if (i >= k) b[i] = b[i - k];
else b[i] = 0;
}
}
lb = la;
for (int i = 0; i <= k; i++)
{
int temp;
while ((temp = substraction(a, b + i, la, lb - i)) >= 0)
{
la = temp;
res[k - i]++;
}
}
output();
}
int main()
{
scanf("%s %s", x, y);
divv();
return 0;
}