AlenAntonelli

Producto de números grandes

May 30th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. using namespace std;
  5.  
  6. string suma (string a, string b)
  7. {
  8.     int may = max( a.size(), b.size() );
  9.    
  10.     reverse( a.begin(), a.end() );
  11.     reverse( b.begin(), b.end() );
  12.    
  13.     for(int i=a.size(); i<may; i++)
  14.         a.push_back('0');
  15.     for(int i=b.size(); i<may; i++)
  16.         b.push_back('0');
  17.        
  18.     bool ac = false;
  19.     string c;
  20.    
  21.     for(int i=0; i<a.size(); i++)
  22.     {
  23.         int A = a[i]-'0';
  24.         int B = b[i]-'0';
  25.        
  26.         int C = A+B+ac;
  27.         ac = C/10;
  28.         c.push_back( (C)%10 +'0' );
  29.     }
  30.     if(ac)
  31.         c.push_back('1');
  32.    
  33.     reverse(c.begin(), c.end());
  34.    
  35.     return c;
  36. }
  37.  
  38. string prod (string &a, string &b)
  39. {
  40.     string p, c;
  41.     int ac;
  42.    
  43.     reverse( a.begin(), a.end() );
  44.     reverse( b.begin(), b.end() );
  45.    
  46.     for(int i=0; i<a.size(); i++)
  47.     {
  48.         int A = a[i]-'0';
  49.         ac = false;
  50.         c.clear();
  51.        
  52.         for(int j=0; j<b.size(); j++)
  53.         {
  54.             int B = b[j]-'0';
  55.            
  56.             int C = (A*B)+ac;
  57.             ac = C/10;
  58.             c.push_back( (C)%10 +'0' );
  59.         }
  60.         if(ac)
  61.             c.push_back(ac+'0');
  62.            
  63.         reverse( c.begin(), c.end() );
  64.         for(int R=0; R<i; R++)
  65.             c.push_back('0');
  66.        
  67.         p = suma(p,c);
  68.     }
  69.    
  70.     return p;
  71. }
  72.  
  73. int main()
  74. {
  75.     string a, b;
  76.    
  77.     cin>>a;
  78.     cin>>b;
  79.     cout<<prod(a,b);
  80.  
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment