加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

两个大数相乘

发布时间:2020-12-14 03:39:46 所属栏目:大数据 来源:网络整理
导读:# includestdio.h # includestring.h # includemalloc.h ? void multiply(char* a,char* b,char* c) { ? ? int i,j,ca,cb,* s; ? ? ca=strlen(a); ? ? cb=strlen(b); ? ? s=(int*)malloc(sizeof(int)*(ca+cb)); ? ? for (i=0;ica+cb;i++) ? ? ? ? s[i]=0; ?
# include<stdio.h>
# include<string.h>
# include<malloc.h>
?
void multiply(char* a,char* b,char* c)
{
? ? int i,j,ca,cb,* s;
? ? ca=strlen(a);
? ? cb=strlen(b);
? ? s=(int*)malloc(sizeof(int)*(ca+cb));
? ? for (i=0;i<ca+cb;i++)
? ? ? ? s[i]=0;
? ? for (i=0;i<ca;i++)
? ? ? ? for (j=0;j<cb;j++)
? ? ? ? ? ? s[i+j+1]+=(a[i]-'0')*(b[j]-'0'); ? ///这段之前看注释A
? ? for (i=ca+cb-1;i>=0;i--) ? ? ? ? ? ? ?///这层for循环看注释B
? ? ? ? if (s[i]>=10)
? ? ? ? {
? ? ? ? ? ? s[i-1]+=s[i]/10;
? ? ? ? ? ? s[i]%=10;
? ? ? ? }
? ? i=0;
? ? while (s[i]==0)
? ? ? ? i++;
? ? ? ?for (j=0;i<ca+cb;i++,j++)
? ? ? ? ? ?c[j]=s[i]+'0';
? ? c[j]='';
? ? free(s);

} 注释A: 举个简单的例子,19*25(数多一样的道理) ? ? ? 1 ? 9(数组a) * ? ?2 ? 5 ?(数组 b) -————----------------—————— ? ? 5 ? 45 ? (5*9=45 ,把45放在Int 型的数组里面。对应的位置为s[3],同理s[2]=5+18=23;s[1]=2) 2 18 注释B: 如果某位>10,说明产生了进位。%10留下各位,/10 表示进位的个数 开始:1.s[3]=45 s[2]=23 s[1]=2; ? ? ? ? ? ? 2.s[3]=45,大于10,s[3]=5,s[2]=27;s[1]=2; ? ? 3.s[3]=5;s[2]=7;s1[4]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读