Base Time Limit: 1 second Space Limit: 131072 KB Score: 0
Difficulty: Basic
Given two large integers A and B, calculate the result of A*B.
Input
Row 1: Large number A Line 2: Large number B (A,B length <= 1000, A,B >= 0)
Output
output A*B
Input example
123456 234567
Output example
28958703552
large number multiplication template, of course this is not the optimal algorithm.
1 #include <bits/stdc++.h> 2 using namespace std; 3 string s,ss; 4 5 string bigmulti(string s,string ss){ 6 int slen = s.length(); 7 int sslen= ss.length(); 8 int st[20100]; 9 string tt=""; 10 reverse(s.begin(),s.end()); 11 reverse(ss.begin(),ss.end()); 12 for(int i=0;i<slen;i++){ 13 for(int j=0;j<sslen;j++){ 14 st[i+j] += (s[i]-'0')*(ss[j]-'0'); 15 } 16 } 17 for(int i=0;i<slen+sslen;i++){ 18 st[i+1]+=st[i]/10; 19 st[i]=st[i]%10; 20 } 21 int index; 22 for(int i=slen+sslen;i>=0;i--){ 23 if(st[i]!=0){ 24 index = i; 25 break; 26 } 27 } 28 for(int i=0;i<=index;i++){ 29 tt+=st[i]+'0'; 30 } 31 reverse(tt.begin(),tt.end()); 32 return tt; 33 } 34 int main(){ 35 while(cin>>s>>ss){ 36 bool sflag=true, ssflag=true; 37 if(s[0]=='-'){ 38 s.erase(0,1); 39 sflag = false; 40 } 41 if(ss[0]=='-'){ 42 ss.erase(0,1); 43 ssflag = false; 44 } 45 cout<<bigmulti(s,ss)<<endl; 46 } 47 return 0; 48 }