昨天忘了这个事了QAQ
高精加:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char a1[500+10],b1[500+10];
int a[500+10],b[500+10],c[500+10];
int main()
{
scanf("%s%s",a1,b1);
int str1=strlen(a1),str2=strlen(b1);
for(int i=1;i<=str1;i++) a[i]=a1[str1-i]-'0';
for(int i=1;i<=str2;i++) b[i]=b1[str2-i]-'0';
int q=1;
while(q<=str1||q<=str2)
{
c[q]+=a[q];
c[q]+=b[q];
c[q+1]=c[q]/10;
c[q]%=10;
q++;
}
while(c[q]==0) q--;
for(int i=q;i>=1;i--) cout<<c[i];
return 0;
}
高精减:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char a1[500+10],b1[500+10];
int a[500+10],b[500+10],c[500+10];
int main()
{
scanf("%s%s",a1,b1);
int str1=strlen(a1),str2=strlen(b1);
for(int i=1;i<=str1;i++) a[i]=a1[str1-i]-'0';
for(int i=1;i<=str2;i++) b[i]=b1[str2-i]-'0';
if(str2>str1)
{
for(int i=1;i<=str2;i++) swap(a[i],b[i]);
putchar('-');
}
else
{
for(int i=str1;i>=1;i--)
{
if(a[i]>b[i]) break;
else if(a[i]==b[i]) continue;
else
{
for(int j=i;j>=1;j--) swap(a[j],b[j]);
putchar('-');
break;
}
}
}
int q=1;
while(q<=str1||q<=str2)
{
c[q]+=10;
c[q]+=a[q];
c[q]-=b[q];
c[q+1]=c[q]/10-1;
c[q]%=10;
q++;
}
while(c[q]==0&&q>1) q--;
for(int i=q;i>=1;i--) cout<<c[i];
return 0;
}
高精乘:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int SZ=500+10;
int a[SZ],b[SZ],c[SZ<<1];
char a1[SZ],b1[SZ];
int main()
{
scanf("%s%s",a1,b1);
int str1=strlen(a1);
int str2=strlen(b1);
for(int i=1;i<=str1;i++) a[i]=a1[str1-i]-'0';
for(int i=1;i<=str2;i++) b[i]=b1[str2-i]-'0';
for(int i=1;i<=str1;i++)
{
for(int j=1;j<=str2;j++)
{
c[i+j-1]+=a[i]*b[j];
}
}
for(int i=1;i<=str1+str2+1;i++)
{
c[i+1]+=c[i]/10;
c[i]%=10;
}
int q=str1+str2+1;
while(c[q]==0&&q>1) q--;
for(int i=q;i>=1;i--) cout<<c[i];
return 0;
}