Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #include <string>
  2. #include <vector>
  3. #include <iostream>
  4. #include <stdio.h>
  5. using namespace std;
  6. void product(string a, string b) {
  7. vector<int> result(a.size() + b.size(), 0);
  8. for( int i = a.size() - 1; i >= 0; i-- ){
  9. for( int j = b.size() - 1; j >= 0; j-- ){
  10. result[ i + j + 1 ] += ( b[ j ] - '0') * ( a[ i ] - '0' ); //single array to store intermediate values
  11. }
  12. }
  13. for( int i = a.size() + b.size(); i >= 0; i-- ){
  14. if( result[ i ] >= 10 ){
  15. result[ i - 1 ] +=result[ i ] / 10;result[ i ] %= 10;
  16. }
  17. }
  18. cout << a << " * " << b << " = ";
  19. for( int i = 0; i < a.size() + b.size(); i++ ){
  20. cout << result[ i ];
  21. }
  22. cout << endl;
  23. }
  24. int main( void ){
  25. string str1, str2 ;
  26. str1 = "99999999999999999999";str2 = "9985995948565498566";
  27. product( str1, str2 );
  28. }
  29. return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement