frentzy

Interclasare a 2 vectori, citire fisier

Oct 26th, 2020
1,235
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Input
  2. //5 8
  3. //2 4 7 37 42
  4. //88 88 67 45 42 32 4 1
  5. //Output
  6. //2 4 4 32 42 42 88 88
  7. //practic preia din cei 2 vectori(unu care e crescator si celalalt escrescator), elimina toate numerele care se gasesc de 2 sau mai multe ori si creeaza un nou sir crescator fara dubluri
  8. #include <conio.h>
  9. #include <stdio.h>
  10. #include <iostream>
  11. #include <algorithm>
  12. #include <fstream>
  13. #include <vector>
  14. using namespace std;
  15.  
  16. ifstream fin("interclasare3.in");
  17. ofstream fout("interclasare3.out");
  18.  
  19. void citireVector(int *p,int contorp) {
  20.     for (int i = 0; i < contorp; i++) {
  21.         fin >> p[i];
  22.     }
  23. }
  24.  
  25. int* contopireVectori(int* p1, int* p2, int contorp1, int contorp2,int &contorp3) {
  26.     //int c[2000],k=0;
  27.     int *p3 = new int[contorp1+contorp2];
  28.     //Introducere a tuturor elementelelor pare din primul vector in noul vector
  29.     for (int i = 0; i < contorp1; i++) {
  30.         if (p1[i] % 2 == 0) {
  31.             p3[contorp3++] = p1[i];
  32.         }
  33.     }
  34.    
  35.     //Introducere a tuturor elementelor pare din al 2-lea vector in noul vector
  36.     for (int i = 0; i < contorp2; i++) {
  37.         if (p2[i] % 2 == 0) {
  38.             p3[contorp3++] = p2[i];
  39.         }
  40.     }
  41.  
  42.     return p3;
  43. }
  44. void interclasare(int* p, int &contor) {
  45.     vector<int> myVector(p, p + contor);
  46.     sort(myVector.begin(), myVector.end());
  47.     for (int i = 0; i < myVector.size(); i++) {
  48.         p[i] = myVector[i];
  49.     }
  50.        
  51. }
  52.  
  53. void main() {
  54.     int a[1000], b[1000], *c = new int[2000], n, m, *k = new int;
  55.     *k = 0;
  56.     //Citire contori
  57.     fin >> n;
  58.     fin >> m;
  59.  
  60.     citireVector(a, n);
  61.     citireVector(b, m);
  62.     c = contopireVectori(a, b, n, m, *k);
  63.     interclasare(c, *k);
  64.  
  65.     for (int i = 0; i < *k; i++) {
  66.         if (i % 20 == 0)
  67.             cout << endl;
  68.         fout << c[i] << " ";
  69.     }
  70.  
  71. }
  72.  
RAW Paste Data