vault backup: 2022-12-19 12:46:22
This commit is contained in:
152
PI/Ćwiczenia/1.Rekurencja.md
Normal file
152
PI/Ćwiczenia/1.Rekurencja.md
Normal file
@@ -0,0 +1,152 @@
|
||||
---
|
||||
date:
|
||||
- 20221024115005
|
||||
- 20221107120342
|
||||
- 20221121122351
|
||||
---
|
||||
# Jak policzyć słowa w książce?
|
||||
```cpp
|
||||
const int rozmiar_książki = 300;
|
||||
int liczba_słów_na_stronie[rozmiar_książki];
|
||||
liczba_słów_na_stronie[2]=20; // <-- 20 słów na stronie 3
|
||||
int suma = liczba_słów_na_stronie[0];
|
||||
for (int nr = 1;nr<rozmiar;nr++){
|
||||
suma+=liczba_słów_na_stronie[nr];
|
||||
}
|
||||
```
|
||||
![[Drawing 2022-10-24 12.04.19.excalidraw]]
|
||||
|
||||
# Funkcja wyliczająca minimum i maksimum
|
||||
|
||||
```CpP
|
||||
void minmax1(double a[],int &min, int &max){
|
||||
max = 0;
|
||||
for (int i = 1; i<n;i++)
|
||||
if (a[i]>a[max]){
|
||||
max = i;
|
||||
}
|
||||
min = 0;
|
||||
for (int i = 1; i<n;i++)
|
||||
if (a[i]<a[max]){
|
||||
min = i;
|
||||
}
|
||||
} //2(n-1)
|
||||
void minmax2(double a[],int &min, int &max){
|
||||
max = min = 0;
|
||||
for (int i = 1; i<n;i++)
|
||||
if (a[i]>a[max]){
|
||||
max = i;
|
||||
};
|
||||
if (a[i]<a[max]){
|
||||
min = i;
|
||||
};
|
||||
} //2(n-1)
|
||||
void minmax3(double a[],int &min, int &max){
|
||||
max = min = 0;
|
||||
for (int i = 1; i<n;i++)
|
||||
if (a[i]>a[max]){
|
||||
max = i;
|
||||
} else if (a[i]<a[max]){
|
||||
min = i;
|
||||
};
|
||||
} // opt n-1 where 1/n!; pes 2(n-1) where 1/n; avg 2(n-1)-ln(n)-C
|
||||
|
||||
```
|
||||
|
||||
```c++
|
||||
# l, p - leftmost rightmist
|
||||
void minmax3(double a[], int l, int p, int &min, int &max){
|
||||
if (l==p){
|
||||
## tablica jednoelementowa
|
||||
min=max=l;
|
||||
} else if (l+1==p) {
|
||||
if (a[l]>a[p]){max=l;min=p;}else{min=l;max=p;}
|
||||
max = l;
|
||||
} else {
|
||||
int m1,m2,M1,M2;
|
||||
minmax3(a,l,(l+p)/2,m1,M1);
|
||||
minmax3(a,l,(l+p)/2+1,m2,M2);
|
||||
if(a[m2]<a[m1])
|
||||
min=m2;
|
||||
else min=m1;
|
||||
if(a[M2]>a[m1])
|
||||
max=M2;
|
||||
else max=M1;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Reprezentacja w C++
|
||||
```cpp
|
||||
int f(int m,int m){
|
||||
if(m==1 || n ==1){
|
||||
return 1;
|
||||
} else if (m>n){
|
||||
return f(m-n,n)+f(m,n-1)
|
||||
}else{
|
||||
return 1+f(m,m-1)
|
||||
}
|
||||
}
|
||||
```
|
||||
# Na ile sposobów można przedstawić liczbę 5?
|
||||
5
|
||||
4+1
|
||||
3+2
|
||||
3+1+1
|
||||
2+2+1
|
||||
2+1+1+1
|
||||
1+1+1+1+1
|
||||
|
||||
### Stos:
|
||||
![[20221107120342 2022-11-07 12.08.17.excalidraw]]
|
||||
|
||||
# Trójkąt Sierpińskiego
|
||||
## Grafika:
|
||||
![[20221107120342 2022-11-07 12.15.53.excalidraw]]
|
||||
|
||||
## Kod:
|
||||
```cpp
|
||||
void tr(double bok, double min){
|
||||
if(bok>min){
|
||||
for(int i=0;i<3,i++){
|
||||
tr(bok/2, min);
|
||||
rysuj(bok);
|
||||
obrot(120);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
# Drzewo:
|
||||
## Rysunek
|
||||
![[20221107120342 2022-11-07 12.22.55.excalidraw]]
|
||||
## Kod:
|
||||
```cpp
|
||||
void galaz(double a){
|
||||
rysuj(a);
|
||||
obrot(45);
|
||||
galaz(2a/3.0);
|
||||
obrot(-90);
|
||||
galaz(2a/3.0);
|
||||
obrot(-135);
|
||||
rysuj(a);
|
||||
obrot(180);
|
||||
}
|
||||
```
|
||||
# Problem skoczka szachowego:
|
||||
## Rysunek:
|
||||
![[20221107120342 2022-11-07 12.48.52.excalidraw]]
|
||||
## Kod:
|
||||
```cpp
|
||||
const n = 8;
|
||||
int Sz[n][n];
|
||||
int dx[8]={1,2,2,1,-1,-2,-2,-1};
|
||||
int dy[8]={2,1,-1,-2,-2,-1,1,2};
|
||||
bool Probuj(int x, int y, int nr){
|
||||
Sz[x][y]=nr;
|
||||
if(nr==n*n) return true;
|
||||
for (int i = 0;i<n;i++)
|
||||
}
|
||||
```
|
||||
```
|
||||
Reference in New Issue
Block a user