#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
char s1[305], s2[305];
int a[305], b[305], c[305],tmp[305];
void init(int* a)
{
char s[305];
scanf("%s", s);
size_t len = strlen(s);
a[0] = len;
for (int i = 1; i <= len; i++)
{
a[i] = s[len - i] - '0';
}
}
//OK
int compare(int* a, int* b)
{
if (a[0] != b[0])
return a[0] - b[0];
for (int i = a[0]; i >0; --i)
{
if (a[i] != b[i])
return a[i] - b[i];
}
return 0;
}
//ok
void numcpy(int* p ,int * q, int n)
{
int len = p[0];
q[0] = p[0] + n-1;
for (int i = 1; i <= len; ++i)
{
q[i + n-1] = p[i];
}
}
//OK
void print(int* a)
{
if (a[0] == 0)
{
cout << 0;
return;
}
for (int i = a[0]; i>=1; --i)
{
printf("%d", a[i]);
}
}
//OK
void Sub(int* a, int* b)
{
int lb = b[0];
for (int i = 1; i <= lb; ++i)
{
if (a[i] < b[i])
{
--a[i + 1];
a[i] = a[i]+ 10;
}
a[i] = a[i] - b[i];
}
while (a[a[0]] == 0 && a[0] > 0)
--a[0];
}
int main()
{
init(a);
init(b);
c[0] = a[0] - b[0] + 1;
//print(a);
//cout << endl;
//print(b);
for (int i =c[0]; i>=1; --i)
{
memset(tmp, 0, sizeof(tmp));
numcpy(b, tmp, i);
while (compare(a, tmp)>=0)
{
c[i]++;
Sub(a, tmp);
}
}
while (c[c[0]] == 0 && c[0] > 0)
c[0]--;
print(c);
cout << endl;
print(a);
return 0;
}
高精度除法
最新推荐文章于 2024-09-04 21:39:45 发布