JosepRivaille

P16175: F002B. Vectors comprimits

May 10th, 2015
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. struct Parell {
  7.         int valor; // != 0
  8.         int pos; // >=0
  9. };
  10.  
  11. typedef vector<Parell> Vec_Com; //Ordenat per pos
  12.  
  13. void llegeix(Vec_Com& v) {
  14.         int v_tam = v.size();
  15.         char c;
  16.         for (int i = 0; i < v_tam; ++i) {
  17.                 cin >> v[i].valor >> c >> v[i].pos;
  18.         }
  19. }
  20.  
  21. Vec_Com suma(const Vec_Com& v1, const Vec_Com& v2) {
  22.         int tam_v1 = v1.size();
  23.         int tam_v2 = v2.size();
  24.         Vec_Com sum(tam_v1 + tam_v2);
  25.         int i = 0;
  26.         int j = 0;
  27.         int k = 0;
  28.         while (i < tam_v1 && j < tam_v2) {
  29.                 if (v1[i].pos < v2[j].pos) {
  30.                     sum[k] = v1[i];
  31.                     ++k;
  32.                     ++i;
  33.                 }
  34.                 else if (v1[i].pos > v2[j].pos) {
  35.                     sum[k] = v2[j];
  36.                     ++k;
  37.                     ++j;
  38.                 }
  39.                 else {
  40.                     if (v1[i].valor + v2[j].valor != 0) {
  41.                         sum[k].valor = v1[i].valor + v2[j].valor;
  42.                         sum[k].pos = v1[i].pos;
  43.                         ++k;
  44.                     }
  45.                     ++i;
  46.                     ++j;
  47.                 }
  48.         }
  49.         while (i < tam_v1) {
  50.             sum[k] = v1[i];
  51.             ++k;
  52.             ++i;
  53.         }
  54.         while (j < tam_v2) {
  55.             sum[k] = v2[j];
  56.             ++k;
  57.             ++j;
  58.         }
  59.         Vec_Com sum_comp(k);
  60.         for (int p = 0; p < k; ++p) sum_comp[p] = sum[p];
  61.         return sum_comp;
  62. }
  63.  
  64.  
  65. //Pre: Llegeix n parells de vectors comprimits
  66. //Post: Escriu el vector comprimit resultant de la suma de cada parella
  67. int main() {
  68.         int n, tam;
  69.         cin >> n;
  70.         while (n != 0) {
  71.                 cin >> tam;
  72.                 Vec_Com v1(tam);
  73.                 llegeix(v1);
  74.                 cin >> tam;
  75.                 Vec_Com v2(tam);
  76.                 llegeix(v2);
  77.                 Vec_Com resultat = suma(v1, v2);
  78.                 int tam_res = resultat.size();
  79.                 if (tam_res == 1 && resultat[0].pos == -1) cout << 0 << endl;
  80.                 else {
  81.                         int count = 0;
  82.                         for (int i = 0; i < tam_res; ++i) {
  83.                                 if (resultat[i].valor != 0) ++count;
  84.                         }
  85.                         cout << count;
  86.                         for (int i = 0; i < tam_res; ++i) {
  87.                                 cout <<" " <<resultat[i].valor << ";" << resultat[i].pos;
  88.                         }
  89.                         cout << endl;
  90.                 }
  91.                 --n;
  92.         }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment