大数的乘法
发布时间:2020-12-14 03:54:58 所属栏目:大数据 来源:网络整理
导读:1 #includeiostream 2 #include string .h 3 #define MAX 100 4 void change( char *st, int len); 5 using namespace std; 6 7 8 int main() 9 { 10 char st1[MAX],st2[MAX]; 11 int st3[ 2 *MAX] = { 0 }; 12 scanf( " %s%s " ,st1,st2); 13 int len1,len2
1 #include<iostream> 2 #include<string.h> 3 #define MAX 100 4 void change(char *st,int len); 5 using namespace std; 6 7 8 int main() 9 { 10 char st1[MAX],st2[MAX]; 11 int st3[2*MAX] = {0}; 12 scanf("%s%s",st1,st2); 13 int len1,len2,len3; 14 int i,j; 15 len1 = strlen(st1); 16 len2 = strlen(st2); 17 change(st1,len1); //将数字翻转 18 change(st2,len2); 19 for(i = 0; i < len1; i++) //大数乘法核心代码 20 { 21 for(j = 0; j < len2; j++) 22 { 23 st3[i+j] += (st1[i]-'0')*(st2[j]-'0'); 24 st3[i+j+1] += st3[i+j] / 10; 25 st3[i+j] %= 10; 26 } 27 } 28 len3 = len1+len2+1; 29 while(!st3[len3]) 30 {len3--;} 31 for(i = len3; i >= 0; i--) //输出 32 cout<<st3[i]; 33 cout<<endl; 34 return 0; 35 } 36 37 void change(char *st,int len) 38 { 39 int i,j; 40 char temp; 41 for(i =0,j = len-1; i <= j; i++,j--) 42 { 43 temp = st[i]; 44 st[i] = st[j]; 45 st[j] = temp; 46 } 47 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |