Tömbök - adott tulajdonság ellenőrzése

1.

Adott tömbnek van-e páros eleme?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;

    int t[1000];
    for (int i = 0; i < n; i++)
        cin >> t[i];

    bool talalt = false;

    for (int i = 0; i < n; i++)
        if (t[i] % 2 == 0)
            talalt = true;

    if (talalt) {
        cout << "van" << endl;
    }
    else {
        cout << "nincs" << endl;
    }

    return 0;
}

2.

Igaz-e a tömbre, hogy minden eleme páros?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;

    int t[1000];
    for (int i = 0; i < n; i++)
        cin >> t[i];

    bool talalt_paratlant = false;

    for (int i = 0; i < n; i++)
        if (t[i] % 2 != 0)
            talalt_paratlant = true;

    if (!talalt_paratlant) {
        cout << "igaz" << endl;
    }
    else {
        cout << "nem igaz" << endl;
    }

    return 0;
}

Másképp:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;

    int t[1000];
    for (int i = 0; i < n; i++)
        cin >> t[i];

    int hany_paros = 0;

    for (int i = 0; i < n; i++)
        if (t[i] % 2 == 0)
            hany_paros++;

    if (hany_paros == n) {
        cout << "igaz" << endl;
    }
    else {
        cout << "nem igaz" << endl;
    }

    return 0;
}

3.

Igaz-e, hogy a tömb minden eleme azonos?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;

    int t[1000];
    for (int i = 0; i < n; i++)
        cin >> t[i];

    bool baj = false;

    for (int i = 0; i < n; i++)
        if (t[i] != t[0])
            baj = true;

    if (baj) {
        cout << "nem igaz" << endl;
    }
    else {
        cout << "igaz" << endl;
    }

    return 0;
}

4.

Igaz-e, hogy a tömb elemei növekvő sorrendben vannak?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;

    int t[1000];
    for (int i = 0; i < n; i++)
        cin >> t[i];

    bool baj = false;

    for (int i = 1; i < n; i++) // csak a második elemtől indulunk
        if (t[i] < t[i-1])
            baj = true;

    if (baj) {
        cout << "nem igaz" << endl;
    }
    else {
        cout << "igaz" << endl;
    }

    return 0;
}

5.

Igaz-e, hogy a tömb páros elemei növekvő sorrendben vannak?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;

    int t[1000];
    for (int i = 0; i < n; i++)
        cin >> t[i];

    bool baj = false;
    int elozo_paros = -1;

    for (int i = 0; i < n; i++)
        if (t[i] % 2 == 0) { // ha nem páros, akkor nem érdekel
            if (elozo_paros != -1 && t[i] < elozo_paros)
                baj = true;

            elozo_paros = t[i];
        }

    if (baj) {
        cout << "nem igaz" << endl;
    }
    else {
        cout << "igaz" << endl;
    }

    return 0;
}