Valamilyen szabály szerint elő kell állítani
a kimeneten egy többelemű sorozatot.
1.
Állítsuk elő az első n darab kettőhatvány sorozatát.
Példák
n = 5 esetén:
1 2 4 8 16
n = 10 esetén:
1 2 4 8 16 32 64 128 256 512
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;
for (int i = 0; i < n; i++) {
int szam = 1;
for (int j = 1; j <= i; j++)
szam = szam * 2;
cout << szam << " ";
}
cout << endl;
// másképp:
int szam = 1;
for (int i = 0; i < n; i++) {
cout << szam << " ";
szam = szam * 2;
}
cout << endl;
return 0;
}
|
2.
Fibonacci-sorozat első n tagjának kiírása
(https://www.pbinfo.ro/probleme/255/fibonacci)
Értelmezés: Fibonacci-sorozat
f(0) = 0,
f(1) = 1,
f(n) = f(n-1) + f(n-2) bármely n >= 2 esetén
Első néhány eleme:
0 1 1 2 3 5 8 13 21 34 55 89 144 ...
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
| #include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
if (n == 1) {
cout << 0 << endl;
}
else {
cout << 0 << " " << 1 << " ";
int a = 0;
int b = 1;
for (int i = 3; i <= n; i++) {
int c = a + b;
cout << c << " ";
a = b;
b = c;
}
cout << endl;
}
return 0;
}
|
Vagy (ha mindig a-t írjuk ki):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| #include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int a = 0;
int b = 1;
for (int i = 1; i <= n; i++) {
int c = a + b;
cout << a << " ";
a = b;
b = c;
}
cout << endl;
return 0;
}
|
3.
https://www.pbinfo.ro/probleme/2793/pozitiiconsecutive
A megadott rekurzív összefüggés alapján az x és y egymás utáni elemekig terjedő részét kell kiírni
a sorozatnak csökkenő sorrendben.
Ötlet:
f(1) = 0
f(2) = 3
f(n) = 2*f(n-1) - f(n-2) + 2
0, 3, 8, 15, 24, 35, 48, 63, 80 ...
x y
63 48 35 24 ....
y x uj
y x uj
y x uj
f(n) = 2*f(n-1) - f(n-2) + 2
<=> f(n-2) = 2*f(n-1) + 2 - f(n)
uj = 2*x + 2 - y
Megoldás:
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;
int main()
{
int x, y;
cin >> x >> y;
cout << y << " " << x << " ";
while (x > 0) {
int uj = 2*x + 2 - y;
cout << uj << " ";
y = x;
x = uj;
}
cout << endl;
return 0;
}
|
Megoldás pbinfora (fájlokkal):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| #include <fstream>
using namespace std;
int main()
{
ifstream bemenet("pozitiiconsecutive.in");
ofstream kimenet("pozitiiconsecutive.out");
int x, y;
bemenet >> x >> y;
kimenet << y << " " << x << " ";
while (x > 0) {
int uj = 2*x + 2 - y;
kimenet << uj << " ";
y = x;
x = uj;
}
kimenet << endl;
return 0;
}
|
4.
https://www.pbinfo.ro/probleme/423/fibonacci1
Adott n esetén a fibonacci-sorozat n-nél kisebb vagy egyenlő tagjait kell kiírni növekvő sorrendben.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| #include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int a = 0;
int b = 1;
while (a <= n) {
int c = a + b;
cout << a << " ";
a = b;
b = c;
}
cout << endl;
return 0;
}
|