#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int max(int a,int b){
return a>b?a:b;
}
void swap(char* a,char *b){
char t=*a;
*a=*b;
*b=t;
}
void inverse(char *a){
int i,len=strlen(a);
for(i=0;i<len/2;i++){
swap(&a[i],&a[len-i-1]);
}
}
char *multiply(char *a,char *b){
int lena=strlen(a);
int lenb=strlen(b);
int len=(lena+lenb);
int *cnum=(int *)malloc(sizeof(int)*(len+5));
char*c=(char*)malloc(sizeof(char)*(len+5));
memset(cnum,0,sizeof(int)*(len+5));
inverse(a);
inverse(b);
int i,j;
for(i=0;i<lena;i++)
for(j=0;j<lenb;j++){
cnum[j+i]+=(a[i]-'0')*(b[i]-'0');
}
for(i=0;i<len;i++){
cnum[i+1]+=cnum[i]/10;
cnum[i]%=10;
}
len++;
while(len>1&&!cnum[len-1]){
len--;
}
for(i=0;i<len;i++){
c[i]=cnum[i]+'0';
}
c[len]='\0';
inverse(c);
return c;
}
char *addstrings(char*a,char*b){
char*c;
int lena=strlen(a);
int lenb=strlen(b);
int maxlen=max(lena,lenb);
int i,now,cap,av,bv;
c=(char*)malloc(sizeof(char)*(maxlen+2));
cap=0;
for(i=0;i<maxlen;i++){
av=(i<lena)?(a[i]-'0'):0;
bv=(i<lenb)?(b[i]-'0'):0;
now=av+bv+cap;
cap=now/10;
c[i]=(now%10)+'0';
}
if(cap){
c[i]='1';
maxlen++;
}
c[maxlen]='\0';
inverse(c);
return c;
}
int main(){
char a[10001];
char b[10001];
int i,n;
scanf("%s%s",a,b);
char*c=multiply(a,b);
printf("%s",c);
return 0;
}
高精度乘法
最新推荐文章于 2024-11-10 00:17:32 发布