加法
include
using namespace std;
string s1, s2;
int a[101], b[101],c[101];
void strtoint(string str,int des[]) {
for (int i = 0; i < str.size(); i++) {
des[str.size() - i] = str[i] - '0';
}
}
int main() {
cin >> s1 >> s2;
strtoint(s1, a);
strtoint(s2, b);
int la, lb, lc;
la = s1.size();
lb = s2.size();
lc = max(la, lb) + 1;
for (int i = 1; i <= lc; i++) {
c[i] = a[i] + b[i] + c[i];
c[i + 1] = c[i] / 10;
c[i] = c[i] % 10;
}
while (c[lc] == 0 && lc > 1) lc--;
for (int i = lc; i >= 1; i--) cout << c[i];
}
减法
include
using namespace std;
string s1, s2;
int a[101], b[101], c[101];
void strtoint(string str, int des[]) {
for (int i = 0; i < str.size(); i++) {
des[str.size() - i] = str[i] - '0';
}
}
int main() {
cin >> s1 >> s2;
strtoint(s1, a);
strtoint(s2, b);
int la, lb, lc;
la = s1.size();
lb = s2.size();
lc = max(la, lb) + 1;
for (int i = 1; i <= lc; i++) {
if (a[i] - b[i] < 0) {
a[i] += 10;
a[i + 1]--;
}
c[i] = a[i] - b[i];
}
while (c[lc] == 0 && lc > 1) lc--;
for (int i = lc; i >= 1; i--) cout << c[i];
}
乘法
include
include
using namespace std;
string s1, s2;
int a[100000], b[100000], c[100000];
void strtoint(string str, int des[]) {
for (int i = 0; i < str.size(); i++) {
des[str.size() - i] = str[i] - '0';
}
}
int main() {
cin >> s1 >> s2;
strtoint(s1, a);
strtoint(s2, b);
int la = s1.size();
int lb = s2.size();
int lc = la + lb;
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 >= 1; i--) cout << c[i];
}
除法
include
using namespace std;
string s1;
long long b;
int tmp;
short a[100000],c[100000];
void strtoint(string str, short des[]) {
for (int i = 0; i < str.size(); i++) {
des[i+1] = str[i] - '0';
}
}
int main() {
cin >> s1>>b;
strtoint(s1, a);
int la;
la= s1.size();
for (int i = 1; i <= la; i++) {
c[i] = (tmp * 10 + a[i]) / b;
tmp = (tmp * 10 + a[i]) % b;
}
int lc = 1;
while (c[lc] == 0) lc++;
for (int i = lc; i <= la; i++) cout << c[i];
cout << "余数: " << tmp << endl;
}
标签:lc,高精度,int,s2,s1,算法,str,size From: https://www.cnblogs.com/windzhao6/p/18313838