I.3.
Referencia nélkül:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| #include <iostream>
using namespace std;
void f(int n)
{
if(n > 0)
{
cout << n;
n = n-1;
f(n);
cout << n;
}
}
int main()
{
int x = 3;
f(x);
return 0;
}
|
Nyomonkövetés:
KIMENET: 321012
f(n=3) {
| n: 3 2
|
| cout << n
| f(n=2) {
| | n: 2 1
| |
| | cout << n
| | f(n=1) {
| | | n: 1 0
| | |
| | | cout << n
| | | f(n=0) {
| | | | n: 0
| | | |
| | | | return
| | | }
| | |
| | | cout << n
| | }
| | cout << n
| }
| cout << n
}
Referenciával:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| #include <iostream>
using namespace std;
void f(int &n)
{
if(n > 0)
{
cout << n;
n = n-1;
f(n);
cout << n;
}
}
int main()
{
int x = 3;
f(x);
return 0;
}
|
Nyomonkövetés:
KIMENET: 321000
main {
| x: 3 2 1 0
|
| f(n=3) {
| | n: ref. x-re (main)
| |
| | cout << n
| | n = n-1
| | f(n=2) {
| | | n: ref. x-re (main)
| | |
| | | cout << n
| | | n = n-1
| | | f(n=1) {
| | | | n: ref. x-re
| | | |
| | | | cout << n
| | | | n = n - 1
| | | | f(n=0) {
| | | | | n: ref. x-re
| | | | |
| | | | | return
| | | | }
| | | |
| | | | cout << n
| | | }
| | | cout << n
| | }
| | cout << n
| }
}
II.3.
1
2
3
4
| struct sala {
int nrLocuri;
int durata;
} s[10];
|
III.1.
1
2
3
4
5
6
7
8
9
| int depozit(int x, int y, int z)
{
int i = 1;
while (i % x != 0 || i % y != 0 || i % z != 0)
i++;
return i;
}
|
Vagy:
1
2
3
4
5
6
7
8
9
| int depozit(int x, int y, int z)
{
int i = 1;
while ((i*x) % y != 0 || (i*x) % z != 0)
i++;
return i*x;
}
|
III.2
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
| #include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s[101];
int a, b, c;
cin >> a >> b >> c;
cin.get();
cin.getline(s, 101);
char uj[101];
int n = 0; // hány betű van benne
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] != ' ') {
int eltolas;
if (n % 3 == 0) eltolas = a;
else if (n % 3 == 1) eltolas = b;
else eltolas = c;
if (s[i] + eltolas <= 'Z')
uj[n] = s[i] + eltolas;
else
uj[n] = s[i] + eltolas - 26; // 26 == 'Z'-'A'+1
n++;
}
}
uj[n] = '\0';
cout << uj << endl;
return 0;
}
|
Vagy:
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
| #include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s[101];
int eltolasok[3];
cin >> eltolasok[0] >> eltolasok[1] >> eltolasok[2];
cin.get();
cin.getline(s, 101);
char uj[101];
int n = 0; // hány betű van benne
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] != ' ') {
int eltolas = eltolasok[n % 3];
if (s[i] + eltolas <= 'Z')
uj[n] = s[i] + eltolas;
else
uj[n] = s[i] + eltolas - 26; // 26 == 'Z'-'A'+1
n++;
}
}
uj[n] = '\0';
cout << uj << endl;
return 0;
}
|
III.3.
b.
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
| #include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("bac.txt");
int x, y;
fin >> x >> y;
int elozo = -1;
int db = 0;
int szam;
while (fin >> szam) {
if (szam >= x
&& szam <= y
&& szam % 2 == 0
&& szam != elozo)
{
db++;
}
elozo = szam;
}
cout << db << endl;
return 0;
}
|
a.
Az algoritmus beolvassa x-et és y-t, majd a sorozat elemeit
megjegyezve mindig az előzőt is. Megszámoljuk az összes olyan
számot, ami páros, x és y közötti, illetve különbözik az
az előző elemtől.
Idő szempontjából hatékony, mert egyszer halad végig a sorozaton,
minden elemre konstans számú lépést végez.
Memória szempontjából pedig azért, mert nem tárolja az egész sorozatot,
csak konstans számú értéket.