高精度加法
#include<bits/stdc++.h>
using namespace std;
char s1[505],s2[505];
int a1[505], a2[505], a3[505];
int t1, t2, t3;
inline void init() {
scanf("%s", s1);scanf("%s", s2);
t1 = strlen(s1);
t2 = strlen(s2);
t3 = max(t1, t2) + 1;//答案最多的位数
for (int i=0;i<t1;++i) a1[t1 - i] = s1[i] - '0';
for (int i=0;i<t2;++i) a2[t2 - i] = s2[i] - '0';//倒序存放
}
void work() {
for (int i=1;i<=t3;++i) {
a3[i] += a1[i] + a2[i];
a3[i + 1] = a3[i] / 10;
a3[i] = a3[i] % 10;
}//进行竖式加法运算然后注意进位操作
while(a3[t3] == 0 && t3 > 1) t3--;//去除前导零
for (int i=t3;i>0;--i) printf("%d", a3[i]);//倒序输出答案即可
}
int main() {
init();
work();
return 0;
}
不考虑带负号的情况。
高精度减法
#include<bits/stdc++.h>
using namespace std;
char s1[10090], s2[10090], s3[10090];
int a[10090], b[10090], c[10090], la, lb, lc;
bool flag = 0;
inline bool compare(char a[], char b[]) {
int la = strlen(a);
int lb = strlen(b);
if (la != lb) return la > lb;
else {
for (int i=0;i<la;++i)
if (a[i] != b[i]) return a[i] > b[i];
}
return true;
}
inline void init() {
scanf("%s", s1);scanf("%s", s2);
if (!compare(s1, s2)) {
flag = true;
strcpy(s3, s1);
strcpy(s1, s2);
strcpy(s2, s3);
}//若小减大则进行标记后交换相减
la = strlen(s1);
lb = strlen(s2);
for (int i=0;i<la;++i) a[la - i] = s1[i] - '0';
for (int i=0;i<lb;++i) b[lb - i] = s2[i] - '0';
lc = max(la, lb);
}
inline void work() {
for (int i=1;i<=lc;++i) {
if (a[i] < b[i]) {
a[i + 1]--;
a[i] += 10;
}
c[i] = a[i] - b[i];
}//核心代码
while(c[lc] == 0 && lc > 1) lc--;
if (flag) cout << '-';
for (int i=lc;i>=1;--i) cout << c[i];
}
int main() {
init();
work();
return 0;
}
同样不考虑带符号的情况
高精度乘法
#include<bits/stdc++.h>
using namespace std;
#define maxn 100089
char s1[maxn], s2[maxn], s3[maxn];
int a[maxn], b[maxn], c[maxn], la, lb, lc;
inline void init() {
scanf("%s", s1);scanf("%s", s2);
la = strlen(s1); lb = strlen(s2);
for (int i=0;i<la;++i) a[la - i] = s1[i] - '0';
for (int i=0;i<lb;++i) b[lb - i] = s2[i] - '0';
lc = la + lb;//最多不超过la+lb位数字
}
void work() {
for (int i=1;i<=la;++i) {
for (int j=1;j<=lb;++j) {
c[i + j - 1] += a[i] * b[j];
c[i + j] += c[i + j - 1] / 10;
c[i + j - 1] %= 10;
}
}//核心代码
while(c[lc] == 0 && lc > 1) lc--;
for (int i=lc;i>0;--i) cout << c[i];
}
int main() {
init();
work();
return 0;
}
关于高精度乘法加法的组合应用可以做一下洛谷的P1009 阶乘之和。作为一个简单的无负数的高精度练习题还是可以练练手的。
代码:
#include<bits/stdc++.h>
using namespace std;
int a[505], s[505], xy[505], n;
int main() {
cin >> n;
a[0] = 1;
a[1] = 1;
s[0] = 1;
s[1] = 1;
for (int y=2;y<=n;++y) {
xy[0] = a[0];
for (int i=1;i<=a[0];++i) {
xy[i] += a[i] * y;
xy[i + 1] = xy[i] / 10;
xy[i] %= 10;
}
while(xy[xy[0] + 1] > 0) {
xy[xy[0] + 2] += xy[xy[0] + 1] / 10;
xy[xy[0] + 1] %= 10;
xy[0]++;
}
a[0] = xy[0];
for (int i=1;i<=a[0];++i) a[i] = xy[i];
// for (int i=xy[0];i>=1;--i) cout << xy[i];
// cout << endl;
memset(xy, 0, sizeof(xy));
xy[0] = a[0];
for (int i=1;i<=a[0];++i) {
xy[i] += a[i] + s[i];
xy[i + 1] = xy[i] / 10;
xy[i] %= 10;
}
while(xy[xy[0] + 1] > 0) {
xy[xy[0] + 2] += xy[xy[0] + 1] / 10;
xy[xy[0] + 1] %= 10;
xy[0]++;
}
for (int i=1;i<=xy[0];++i) s[i] = xy[i];
s[0] = xy[0];
memset(xy, 0, sizeof(xy));
}
for (int i=s[0];i>=1;--i) cout << s[i];
return 0;
}