Advertisement
hopingsteam

Problema #241 - interclasare - pbinfo.ro

Feb 16th, 2020
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. /* Tutoriale-Pe.NET - Algoritmul de interclasare
  2.  * Link: https://tutoriale-pe.net/interclasarea-a-doi-vectori-in-c/
  3.  * Rezolvare Problema #241 pbinfo.ro: https://www.pbinfo.ro/probleme/241/interclasare
  4.  */
  5.  
  6. #include    <iostream>
  7. #include    <fstream>
  8.  
  9. using namespace std;
  10.  
  11. ifstream fin("interclasare.in");
  12. ofstream fout("interclasare.out");
  13.  
  14. int main()
  15. {
  16.     int A[100005], B[100005], C[200005];
  17.     int n, m;
  18.  
  19.     fin >> n;
  20.     for(int i = 0; i < n; i++)
  21.         fin >> A[i];
  22.  
  23.     fin >> m;
  24.     for(int i = 0; i < m; i++)
  25.         fin >> B[i];
  26.  
  27.     int i = 0, j = 0, k = 0;
  28.  
  29.     while(i < n && j < m)
  30.     {
  31.         if(A[i] < B[j])
  32.         {
  33.             C[k] = A[i];
  34.             k++;
  35.             i++;
  36.         }
  37.         else
  38.         {
  39.             C[k] = B[j];
  40.             k++;
  41.             j++;
  42.         }
  43.     }
  44.     for(; i < n; i++)
  45.     {
  46.         C[k] = A[i];
  47.         k++;
  48.     }
  49.     for(; j < m; j++)
  50.     {
  51.         C[k] = B[j];
  52.         k++;
  53.     }
  54.  
  55.     int nr = 0;
  56.     for(int p = 0; p < k; p++)
  57.     {
  58.         fout << C[p] << " ";
  59.         nr++;
  60.         if(nr == 10) //Spatiu dupa 10 elemente
  61.         {
  62.             fout << "\n";
  63.             nr = 0;
  64.         }
  65.     }
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement