
大数运算2026-08-29
高精度除法
算法高精度竞赛
高精度除法
高精度系列最难
高精度除以低精度
#include<iostream>
#include<string>
using namespace std;
string s;
int a[101], c[101];
long long b;//低精度
long long tmp=0;//存余数
void fanzhuan(string src, int des[])
{
for (int i = 0; i < src.size(); i++)
{
des[i+1] = src[i] - '0';
}
}
int main()
{
cin >> s;
cin >> b;
int la = s.size();
fanzhuan(s, a);
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 < la)
{
lc++;
}
for (int i = lc; i <= la; i++)
{
cout << c[i];
}
return 0;
}
利用C语言中的字符串实现,求的是余数
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
bool sub_(char* s1, char* s2)
{
int n1 = strlen(s1);
int n2 = strlen(s2);
if (n1 < n2)return false;
if (n1 == n2) {
char* p = s1 + n1 - 1;
char* q = s2 + n2 - 1;
while (p >= s1) {
if (*p > *q) break;
if (*p < *q)return false;
p--; q--;
}
}
char* p = s1;
char* q = s2;
int cx = 0;//借位标志
while (*q) {
int t = *p - *q + cx;
if (t < 0) {
cx = -1;
t += 10;
}
else
cx = 0;
*p = t + '0';
p++; q++;
}
while (*p) {
int t = *p - '0' + cx;
if (t < 0) {
cx = -1;
t += 10;
}
else
cx = 0;
*p = t + '0';
p++;
}
p = s1 + n1 - 1;
while (p > s1 && *p == '0')p--;
p[1] = '\0';
return true;
}
int mod(char* s1, char* s2)
{
char* p = s1 + strlen(s1) - strlen(s2);
if (p < s1) p = s1;
do {
while (sub_(p , s2));
p--;
} while (p >= s1);
return 0;
}
int main()
{
char s1[] = "6677834";
char s2[] = "123456789987654321";
_strrev(s1);
_strrev(s2);//反转
mod(s1, s2);
_strrev(s1);
cout << s1 << endl;
return 0;
}