/* Pb.11 Intersecție 149
VAR 2 - 2021-05-08 OK
Alex și George scriu fiecare pe câte o foaie câte un șir de numere naturale,
reprezentând numerele lor preferate, iar acum vor să vadă care numere apar pe ambele foi.
Astfel, să se scrie o funcție ce primește ca și parametri 3 vectori: A , B și C
și construiește în vectorul C intersecția celor două șiruri A și B.
Semnătură funcție
* Numele funcției va fi intersectie3
* Funcția va avea 3 parametri de tip întreg: A[ ] , B[ ] și C[ ]
Precizări
* Dacă intersecția celor două șiruri e mulțimea vidă, vectorul C[ ] se va lăsa gol
* Vectorii vor fi indexați de la 1, iar pe poziția 0 va fi memorat
numărul de elemente pe care le conține vectorul respectiv
* Se garantează că vectorii A și B sunt sortați crescător.
De asemenea, vectorul C trebuie să fie sortat crescător.
* Antetul funcției este următorul:
void intersectie3(int A[ ], int B[ ], int C[ ])
Restricții
* 0 ≤ A[0], B[0] ≤ 10.000
* Elementele celor doi vectori sunt numere naturale mai mici decât 1.000.000.000
Exemplu
A[ ] = {5, 1, 2, 2, 5, 8} ; B[ ] = {6, 2, 2, 2, 6, 7, 8}
În urma apelului funcției intersectie3( A , B , C), vectorul C[ ] devine:
C[ ] = {3, 2, 2, 8}
5 1 2 2 5 8
6 2 2 2 6 7 8
--->
3 2 2 8
DATELE MELE TEST
3 1 1 2
3 1 1 2
Afiseaza 3 1 1 2
2 10 20
3 3 5 45
Afiseaza 0
4 50 100 110 111
3 50 60 110
Afiseza 2 50 110
4 1 1 1 2
4 1 1 2 3
Afiseaza 3 1 1 2
ALTE DATE TEST
1)
A = 0
B = 0 C = 0
2)
3 1 3 5
2 2 4 C = 0 SIR VID
2b)
2 2 4
3 1 3 5 C = 0
3)
3 1 3 5
3 5 6 7 C = 1, 5
4)
3 1 3 5
3 6 7 8 C = 0
5)
3 7 8 9
3 1 3 5 C = 0
6)
3 1 3 5
3 1 3 5 3 1 3 5
7)
0
3 1 3 5 0
8)
3 1 3 5
0 0
9)
2 5 7
3 1 3 7 1 7
10)
4 1 1 1 2
4 1 1 2 3 3 1 1 2
11)
3 6 6 6
2 1 1 0
12)
1 1
3 1 1 1 1 1
13)
3 1 1 1
1 1 1 1
14)
4 1 1 1 1
4 1 1 1 1 4 1 1 1 1
*/
#include <iostream>
using namespace std;
void intersectie3 (int x[], int y[], int z[]) {
int i = 1, j = 1, k = 0;
while (i <= x[0] && j <= y[0]) {
//while (i <= x[0]) { // ABORDAREA CU 2 WHILE E GRESITA !!!
//while (j <= y[0]) {
if (x[i] < y[j]) {
i++;
} else if (x[i] > y[j]) {
j++;
} else { // x[i] == y[i]
k++;
//z[k] = x[i];
z[k] = y[j];
i++, j++;
}
//}
}
z[0] = k;
for (int l = 0; l <= z[0]; l++) {
cout << z[l] << " ";
}
}
int C[20001];
int main(){
//int A[10001], B[10001]; //, C[20001]; // NU LE CITESTE ASA !!!
//int A[] = {5, 1, 2, 2, 5, 8}, B[] = {6, 2, 2, 2, 6, 7, 8}; // ASA DA !!!
//intersectie3 (A, B, C);
int nA, nB, A[10001], B[10001];
cin >> nA;
A[0] = nA;
for (int i = 1; i <= A[0]; i++) {
cin >> A[i];
}
cin >> nB;
B[0] = nB;
for (int i = 1; i <= B[0]; i++) {
cin >> B[i];
}
intersectie3 (A, B, C);
cout << "\n";
for (int l = 0; l <= C[0]; l++) {
cout << C[l] << " ";
}
return 0;
}
LyoJCQkJUGIuMTEgSW50ZXJzZWPIm2llICAgICAgMTQ5CgkJCQkKVkFSIDIgLSAyMDIxLTA1LTA4IE9LCgpBbGV4IMiZaSBHZW9yZ2Ugc2NyaXUgZmllY2FyZSBwZSBjw6J0ZSBvIGZvYWllIGPDonRlIHVuIMiZaXIgZGUgbnVtZXJlIG5hdHVyYWxlLCAKcmVwcmV6ZW50w6JuZCBudW1lcmVsZSBsb3IgcHJlZmVyYXRlLCBpYXIgYWN1bSB2b3Igc8SDIHZhZMSDIGNhcmUgbnVtZXJlIGFwYXIgcGUgYW1iZWxlIGZvaS4KCkFzdGZlbCwgc8SDIHNlIHNjcmllIG8gZnVu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gMSAxIDIKNCAxIDEgMiAzCQkJMyAxIDEgMgoKMTEpCjMgNiA2IDYKMiAxIDEJCQkJMAoKMTIpCjEgMQozIDEgMSAxCQkJCTEgMQoKMTMpCjMgMSAxIDEKMSAxCQkJCTEgMQoKMTQpCjQgMSAxIDEgMQo0IDEgMSAxIDEJCQk0IDEgMSAxIDEKCgoqLwoKI2luY2x1ZGUgPGlvc3RyZWFtPgogCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKIAp2b2lkIGludGVyc2VjdGllMyAoaW50IHhbXSwgaW50IHlbXSwgaW50IHpbXSkgewogICAgaW50IGkgPSAxLCBqID0gMSwgayA9IDA7CiAgICB3aGlsZSAoaSA8PSB4WzBdICYmIGogPD0geVswXSkgewogICAgLy93aGlsZSAoaSA8PSB4WzBdKSB7IC8vIEFCT1JEQVJFQSBDVSAyIFdISUxFIEUgR1JFU0lUQSAhISEKICAgICAgICAvL3doaWxlIChqIDw9IHlbMF0pIHsKICAgICAgICAgICAgaWYgKHhbaV0gPCB5W2pdKSB7CiAgICAgICAgICAgICAgICBpKys7CiAgICAgICAgICAgIH0gZWxzZSBpZiAoeFtpXSA+IHlbal0pIHsKICAgICAgICAgICAgICAgIGorKzsKICAgICAgICAgICAgfSBlbHNlIHsgLy8geFtpXSA9PSB5W2ldCiAgICAgICAgICAgICAgICBrKys7CiAgICAgICAgICAgICAgICAvL3pba10gPSB4W2ldOwogICAgICAgICAgICAgICAgeltrXSA9IHlbal07CiAgICAgICAgICAgICAgICBpKyssIGorKzsKICAgICAgICAgICAgfQogICAgICAgIC8vfQogICAgfQogICAgelswXSA9IGs7CiAgICBmb3IgKGludCBsID0gMDsgbCA8PSB6WzBdOyBsKyspIHsKICAgICAgICBjb3V0IDw8IHpbbF0gPDwgIiAiOwogICAgfQp9CiAKIAppbnQgQ1syMDAwMV07CiAKaW50IG1haW4oKXsKICAgIC8vaW50IEFbMTAwMDFdLCBCWzEwMDAxXTsgLy8sIENbMjAwMDFdOyAvLyBOVSBMRSBDSVRFU1RFIEFTQSAhISEKICAgIC8vaW50IEFbXSA9IHs1LCAxLCAyLCAyLCA1LCA4fSwgQltdID0gezYsIDIsIDIsIDIsIDYsIDcsIDh9OyAvLyBBU0EgREEgISEhCiAgICAvL2ludGVyc2VjdGllMyAoQSwgQiwgQyk7CiAKICAgIGludCBuQSwgbkIsIEFbMTAwMDFdLCBCWzEwMDAxXTsKICAgIGNpbiA+PiBuQTsKICAgIEFbMF0gPSBuQTsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IEFbMF07IGkrKykgewogICAgICAgIGNpbiA+PiBBW2ldOwogICAgfQogCiAgICBjaW4gPj4gbkI7CiAgICBCWzBdID0gbkI7CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBCWzBdOyBpKyspIHsKICAgICAgICBjaW4gPj4gQltpXTsKICAgIH0KIAogICAgaW50ZXJzZWN0aWUzIChBLCBCLCBDKTsKIAogICAgY291dCA8PCAiXG4iOwogICAgZm9yIChpbnQgbCA9IDA7IGwgPD0gQ1swXTsgbCsrKSB7CiAgICAgICAgY291dCA8PCBDW2xdIDw8ICIgIjsKICAgIH0KICAgIHJldHVybiAwOwp9Cg==