Problem Description
source:https://uva.onlinejudge.org/external/101/10106.html
The problem is to multiply two integers X, Y . (0 ≤ X, Y < 10250)
Input
The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.
Output
For each input pair of lines the output line should consist one integer the product.
Sample Input
12
12
2
222222222222222222222222
Sample Output
144
444444444444444444444444
Solution:
source:https://uva.onlinejudge.org/external/101/10106.html
The problem is to multiply two integers X, Y . (0 ≤ X, Y < 10250)
Input
The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.
Output
For each input pair of lines the output line should consist one integer the product.
Sample Input
12
12
2
222222222222222222222222
Sample Output
144
444444444444444444444444
Solution:
#include <algorithm> #include <cstdio> #include <cmath> #include <cstring> #include <deque> #include <fstream> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #include<stdio.h> #include<stdlib.h> using namespace std; int main() { long int i,j,k,lena,lenb,result[1000],carry,l,temp; char a[270],b[270]; while(scanf("%s%s",&a,&b)==2) { lena=strlen(a); lenb=strlen(b); memset(result,0,sizeof(result)); k=0; for(i=lenb-1;i>=0;i--) { k+=1; l=k-1; carry=0; for(j=lena-1;j>=0;j--) { l+=1; temp=(b[i]-48)*(a[j]-48)+carry+result[l]; result[l]=temp%10; carry=temp/10; } if(carry>0) { l+=1; result[l]=carry; } } if(strcmp(a,"0")==0 ||strcmp(b,"0")==0 ) printf("0"); else { for(i=l;i>=1;i--) printf("%ld",result[i]); } printf("\n"); } return 0; }
No comments:
Post a Comment
Write your comment - Share Knowledge and Experience