/*
int a[] = {1,2,3}
int b[] = {9,8}
int res[] = {1,2,0,5,4}
*/
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
vector<int> multiplytwonum(vector<int> &a, vector<int> &b){
vector<int> s, l, res(a.size()+b.size(),0);
if(a.size()>b.size()){
s=b;
l=a;
}
else{
s=a;
l=b;
}
int carryon = 0;
for(int i=s.size()-1;i>=0;--i){
for(int j=l.size()-1;j>=0;--j){
int product = s[i]*l[j];
int v = res[i+j+1] + product + carryon;
res[i+j+1] = v %10;
carryon = v /10;
}
}
if(carryon){
res[0] = carryon;
}
return res;
}
int main(){
int a[] = {1,2,3};
int b[] = {9,8};
vector<int> aa(a,a+3);
vector<int> bb(b,b+2);
vector<int> res = multiplytwonum(aa, bb);
for (int i=0;i<res.size();++i){
cout<<res[i]<<" ";
}
getchar();
return 0;
}
No comments:
Post a Comment