Advertisement
Nita_Cristian

Numere mari

Jan 30th, 2020
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin("baza.in");
  7. ofstream fout("baza.out");
  8.  
  9. vector<int> x, y, z;
  10. char numere[1001];
  11.  
  12. vector<int> adunare_numere_mari(vector<int> x, vector<int> y)
  13. {
  14.     vector<int> rez;
  15.     int l = x.size(), t = 0, r = 0;
  16.  
  17.     for(int i = 0; i < l; i++)
  18.     {
  19.         r = x[i] + y[i] + t;
  20.         rez.push_back(r % 10);
  21.         t = r / 10;
  22.     }
  23.     if(t)
  24.     {
  25.         rez.push_back(t);
  26.     }
  27.     return rez;
  28. }
  29.  
  30. vector<int> inmultire_scalar(vector<int> x, int s )
  31. {
  32.     int t = 0;
  33.     vector<int> rez;
  34.  
  35.     for(int i = 0; i < x.size(); i++)
  36.     {
  37.         int r = x[i] * s + t;
  38.         rez.push_back(r % 10);
  39.         t = r / 10;
  40.     }
  41.     while(t)
  42.     {
  43.         rez.push_back(t % 10);
  44.         t /= 10;
  45.     }
  46.     return rez;
  47. }
  48.  
  49. vector<int> inmultire_numere_mari(vector<int> x, vector<int> y)
  50. {
  51.     int t = 0, marime = x.size() + y.size() - 1;
  52.     vector<int> rez;
  53.     rez.resize(marime);
  54.  
  55.     for(int i = 0; i < x.size(); i++)
  56.     {
  57.         for(int j = 0; j < y.size(); j++)
  58.         {
  59.             rez[i+j] += x[i] * y[j];
  60.         }
  61.     }
  62.     for(int i = 0; i < marime; i++)
  63.     {
  64.         int r = t + rez[i];
  65.         rez[i] = r % 10;
  66.         t = r / 10;
  67.     }
  68.     while(t)
  69.     {
  70.         rez.push_back(t % 10);
  71.         t /= 10;
  72.     }
  73.     return rez;
  74. }
  75.  
  76. vector<int> scadere_numere_mari(vector<int> x, vector<int> y)
  77. {
  78.     int t = 0;
  79.     vector<int> rez;
  80.  
  81.     for(int i = 0; i < x.size(); i++)
  82.     {
  83.         int r = x[i] - y[i] - t;
  84.         if(r < 0)
  85.             r += 10, t = 1;
  86.         else
  87.             t = 0;
  88.         rez.push_back(r);
  89.     }
  90.     while(t)
  91.     {
  92.         rez.push_back(t % 10);
  93.         t /= 10;
  94.     }
  95.     while(rez.back() == 0 && rez.size() > 1)
  96.         rez.pop_back();
  97.     return rez;
  98. }
  99.  
  100. vector<int> impartire_scalar(vector<int> x, int s)
  101. {
  102.     int t = 0;
  103.     vector<int> rez;
  104.  
  105.     for(int i = x.size()-1; i >= 0; i--)
  106.     {
  107.         int r = t * 10 + x[i];
  108.         rez.push_back(r / s);
  109.         t = r % s;
  110.     }
  111.     return rez;
  112.     /// pentru imparire nu mai inversez vectorul
  113. }
  114.  
  115. void afisare(vector<int> a)
  116. {
  117.     for(const auto it : a)
  118.         if(it > 9)
  119.             fout << char(it - 10 + 65);
  120.         else
  121.             fout << it;
  122.     fout << '\n';
  123. }
  124.  
  125. int main()
  126. {
  127.     fin >> numere;
  128.     for(int i = 0; i < strlen(numere); i++)
  129.         if(isdigit(numere[i]))
  130.             x.push_back(numere[i]-'0');
  131.         else
  132.             x.push_back(numere[i] - 65 + 10);
  133.  
  134.     fin >> numere;
  135.     for(int i = 0; i < strlen(numere); i++)
  136.         if(isdigit(numere[i]))
  137.             y.push_back(numere[i] - '0');
  138.         else
  139.             y.push_back(numere[i] - 65 + 10);
  140.  
  141.     reverse(x.begin(), x.end());
  142.     reverse(y.begin(), y.end());
  143.  
  144.     while(x.size() < y.size())
  145.         x.push_back(0);
  146.  
  147.     while(y.size() < x.size())
  148.         y.push_back(0);
  149.  
  150.     afisare(x);
  151.     afisare(y);
  152.  
  153.     //z = adunare_numere_mari(x, y);
  154.     //z = inmultire_scalar(x, 2);
  155.     //z = inmultire_numere_mari(x, y);
  156.     //z = scadere_numere_mari(x, y);
  157.     z = impartire_scalar(x, 2);
  158.  
  159.     //reverse(z.begin(), z.end());
  160.     afisare(z);
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement