Megoldások - 2022. simulare

II.3.

 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
33
34
35
36
37
38
39
40
41
42
#include <iostream>
using namespace std;

int main()
{
    int j, ok;

    char a[3][15] = {
        {'L','L','L','N','L','F','F','F','N','F','N','F','N','F','F'},
        {'N','L','L','L','N','L','L','F','F','F','F','N','F','N','F'},
        {'F','N','L','F','L','N','F','L','F','F','N','L','F','F','N'}
    };

    //----------------------------------

    ok = 0;
    for (j = 1; j < 14; j++) {
        // a[0][j], a[2][j], a[1][j-1], a[1][j+1]
        if (a[0][j] == a[2][j] && a[1][j-1] == a[1][j+1]
            && a[0][j] == a[1][j-1])
        {
            ok = 1;
        }
    }

    //----------------------------------

    ok = 0;
    for (j = 0; j < 13; j++) {
        // a[0][j+1], a[2][j+1], a[1][j], a[1][j+2]
        if (a[0][j+1] == a[2][j+1] && a[0][j+1] == a[1][j]
            && a[0][j+1] == a[1][j+2])
        {
            ok = 1;
        }
    }

    //----------------------------------

    cout << ok;
    return 0;
}

III.1.

1
2
3
4
5
6
7
8
void rest (int x, int y, int n, int &k)
{
    k=0;

    for (int i=1; i<n+1; i++)
        if (i%x==2 && i%y==2)
            k=i;
}

III.2.

Egyik lehetőség (kettőspontra nézve darabolunk):

 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
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char guest [251]= {};
    char x[251]= {};
    cin.getline(guest,251);
    cin.getline(x,251);
    char *p = strtok(guest, ";");
     bool igaz=0;
    while (p != NULL)
    {
        int eltolas;
        char keresztnev[251]= {};
        if (p[0] == ' ') {
            strcpy(keresztnev, p+1);
            eltolas = 2;
        }
        else {
            strcpy(keresztnev, p);
            eltolas = 1;
        }

        for (int i=0; i<strlen(keresztnev);i++){
            if (keresztnev[i]==' ') keresztnev[i]='\0';
        }

        if (strcmp(x, keresztnev)== 0)
        {   igaz=1;
            for (int i=strlen(keresztnev)+eltolas; i<strlen(p);i++){
                cout<<p[i];
            }
            cout<<" ";
        }

        p = strtok(NULL, ";");
    }

    if (igaz==0)
        cout << "NU";

    return 0;
}

III.3.b

Könnyebb megoldás, de nem hatékony:

 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
#include <iostream>
using namespace std;

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

    int putere_3 = 0;
    int putere_5 = 0;

    for (int i = 1; i <= n; i++) {
        int szam = i;
        while (szam % 3 == 0) {
            putere_3++;
            szam /= 3;
        }
        while (szam % 5 == 0) {
            putere_5++;
            szam /= 5;
        }
    }

    int sol = putere_5;

    if (sol > putere_3 / 2)
        sol = putere_3 / 2;

    cout << sol << endl;
    return 0;
}

Hatékony megoldás:

 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 exp_3 = 0;
    int exp_5 = 0;

    int p3 = 3;
    while (n >= p3) {
        exp_3 += n/p3;
        p3 = p3 * 3;
    }

    int p5 = 5;
    while (n >= p5) {
        exp_5 += n/p5;
        p5 = p5 * 5;
    }

    int sol = exp_3 / 2;

    if (sol > exp_5)
        sol = exp_5;

    cout << sol << endl;

    return 0;
}