uva 10106 Product (大数相乘)
发布时间:2020-12-14 03:06:37 所属栏目:大数据 来源:网络整理
导读:?Product ? The Problem The problem is to multiply two integers X,Y. ( 0=X,Y10 250 ) The Input The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer. The Output For each input pair of lines the output
The ProblemThe problem is to multiply two integers X,Y. (0<=X,Y<10250) The InputThe input will consist of a set of pairs of lines. Each line in pair contains one multiplyer. The OutputFor each input pair of lines the output line should consist one integer the product. Sample Input12 12 2 222222222222222222222222 Sample Output144 444444444444444444444444 题解----数组模拟储存大数的每一位,再相乘,进位。注意 0*123和123*0的特殊情况 #include<iostream> #include<cstdio> #include<cstring> using namespace std; int a[300],b[300],c[700]; char str_1[300],str_2[300]; int main() { int i,p,q,j; while(cin>>str_1) { q=0; cin>>str_2; if(strcmp(str_1,"0")==0||strcmp(str_2,"0")==0) { printf("0n"); continue; } memset(a,sizeof(a)); memset(b,sizeof(b)); memset(c,sizeof(c)); for(i=strlen(str_1)-1,p=299;i>=0;i--,p--) a[p]=str_1[i]-'0'; for(i=strlen(str_2)-1,p--) b[p]=str_2[i]-'0'; for(i=299;i>=0;i--) { for(j=299,p=699-(299-i);j>=0;j--,p--)//关键是P的变化,每乘一位P要加1 { c[p]=c[p]+a[i]*b[j]; } } for(i=699;i>=0;i--) { c[i-1]=c[i-1]+c[i]/10; c[i]=c[i]%10; } for(i=0;i<700;i++) { if(c[i]==0&&q==0) continue; else { cout<<c[i]; q=1; } } puts(""); } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |