C++高精度加减乘除和求余数
2021-04-24 01:27
标签:sizeof 转化 ring while || 字符串表 tmp memset 技术 传入参数:分别为被除数,除数以及选择返回商还是余数( 1求商2求余 ) C++高精度加减乘除和求余数 标签:sizeof 转化 ring while || 字符串表 tmp memset 技术 原文地址:https://www.cnblogs.com/HDawn/p/13268331.html1.高精度加法
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 for (int i = 0; i int lmax = la > lb ? la : lb;
for (int i = 0; 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
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 for (i = 0; i for (i = 0; i x[i] -= y[i];
if (x[i] }
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() if (a.length() > b.length())return 0;
for (i = 0; i if (a[i] return 0;
}
int main() {
string n, m;
cin >> n >> m;
if (cmp(n, m)) {
cout swap(n, m);
}
cout }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 a[la - i - 1] = sa[i] - ‘0‘;
for (int i = 0; i b[lb - i - 1] = sb[i] - ‘0‘;
for (int i = 0; i for (int j = 0; 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.高精度除法
int L = 100010;
int sub1(int *a, int *b, int La, int Lb) {
if (La 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] return -1;//如果a小于b,则返回-1
}
for (int i = 0; i {
a[i] -= b[i];
if (a[i] 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 if (nn == 1) return "0";
if (nn == 2) return n1;
}//如果a 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 int temp;
//如果被除数比除数大继续减
while ((temp = sub1(a, b + j, La, Lb - j)) >= 0)
{
La = temp; r[t - j]++;
}
}
for (i = 0; i r[i + 1] += r[i] / 10, r[i] %= 10;//统一处理进位
while (!r[i])
i--;//将整形数组表示的商转化成字符串表示的
while (i >= 0)
s += r[i--] + ‘0‘;
//cout i = tp;
while (!a[i])
i--;//将整形数组表示的余数转化成字符串表示的
while (i >= 0)
v += a[i--] + ‘0‘;
if (v.empty())
v = "0";
//cout if (nn == 1) return s;
if (nn == 2) return v;
}
上一篇:多线程