大数相乘
发布时间:2020-12-14 04:10:16 所属栏目:大数据 来源:网络整理
导读:不知道原始作者了,但是写的不错,代码重新调整了下格式,加了点注释。 # includestdio.h# includestring.hvoid multiply(char* a,char* b,char* c){int i,j,aLength,bLength,* tmpResult;aLength=strlen(a);bLength=strlen(b);tmpResult=new int[sizeof(int
不知道原始作者了,但是写的不错,代码重新调整了下格式,加了点注释。 # include<stdio.h> # include<string.h> void multiply(char* a,char* b,char* c) { int i,j,aLength,bLength,* tmpResult; aLength=strlen(a); bLength=strlen(b); tmpResult=new int[sizeof(int)*(aLength+bLength)]; for ( i = 0; i < aLength + bLength; i++) tmpResult[i]=0; for ( i = 0; i < aLength; i++ ) for ( j = 0; j < bLength; j++ ) tmpResult[ i+j+1 ] += ( a[i] - '0' ) * ( b[j] - '0' ); //将大于10的数字进行进位 for (i=aLength+bLength-1;i>=0;i--) if (tmpResult[i]>=10) { tmpResult[i-1] += tmpResult[i]/10; tmpResult[i] %=10; } //找到第一个不为0的数字 i=0; while (tmpResult[i]==0) i++; for (j=0;i<aLength+bLength;i++,j++) c[j]=tmpResult[i]+'0'; c[j]=' '; delete tmpResult; } int main() { char a1[]= "999"; char a2[] = "999"; char a3[20]; multiply( a1,a2,a3 ); cout<<a3<<endl; return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |