Rekurzió - feladatok

Nyomonkövetés:

Lásd a https://www.pbinfo.ro/?pagina=itemi-evaluare-lista&disciplina=0&clasa=10&tag=81&subtag=82&eticheta=0 oldalon levő gyakorlatokat.

Megoldások:

Exercițiul 492
Kimenet: 864157
Nyomonkövetés:

    F(154678, 3)
    |   cout << 8
    |   F(15467, 2)
    |   |   F(1546, 3)
    |   |   |   cout << 6
    |   |   |   F(154, 2)
    |   |   |   |   cout << 4
    |   |   |   |   F(15, 1)
    |   |   |   |   |   F(1, 2)
    |   |   |   |   |   |   F(0, 3)
    |   |   |   |   |   |   |
    |   |   |   |   |   |   []
    |   |   |   |   |   |   cout << 1
    |   |   |   |   |   []
    |   |   |   |   |   cout << 5
    |   |   |   |   []
    |   |   |   []
    |   |   []
    |   |   cout << 7
    |   []
    []

Programozás:

  1. https://www.pbinfo.ro/probleme/4207/sumprodrec
  2. https://www.pbinfo.ro/probleme/821/cmmdcrec
  3. https://www.pbinfo.ro/probleme/822/nrcifrezerorec
  4. https://www.pbinfo.ro/probleme/1862/cntcifkrec
  5. https://www.pbinfo.ro/probleme/4208/existaimparerec
  6. https://www.pbinfo.ro/probleme/926/fsumdiv3rec
  7. https://www.pbinfo.ro/probleme/916/factorialrec1
  8. https://www.pbinfo.ro/probleme/4206/cifdiv3rec
  9. https://www.pbinfo.ro/probleme/4210/fcrescrec
  10. https://www.pbinfo.ro/probleme/4537/cifegalerec
  11. https://www.pbinfo.ro/probleme/4209/difparimpar
  12. https://www.pbinfo.ro/probleme/1842/crearenumarrec
  13. https://www.pbinfo.ro/probleme/920/cifmaxminrec
  14. https://www.pbinfo.ro/probleme/834/elimcifrec
  15. https://www.pbinfo.ro/probleme/4211/elimztrec
  16. https://www.pbinfo.ro/probleme/1863/numararerec

Megoldások

1. https://www.pbinfo.ro/probleme/4207/sumprodrec

1
2
3
4
5
6
7
long long SumProdRec(int n)
{
    if (n == 2)
        return 1*2;
    else
        return SumProdRec(n-1) + (n-1)*n;
}

2. https://www.pbinfo.ro/probleme/821/cmmdcrec - lásd itt.

4. https://www.pbinfo.ro/probleme/1862/cntcifkrec

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
void cnt_cif(int n, int k, int &c)
{
    if (n<10){
        if (n>=k) c=1;
        else c=0;
    }
    else {
        cnt_cif(n/10, k, c);
        if (n%10>=k) c++;
    }

}

5. https://www.pbinfo.ro/probleme/4208/existaimparerec

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
int ExistaImpareRec(int n)
{
    if (n < 10) {
        if (n % 2 == 1) return 1;
        else return 0;
    }
    else {
        int voltak = ExistaImpareRec(n/10);
        if (voltak || n%2 == 1)
            return 1;
        else
            return 0;
    }
}

6. https://www.pbinfo.ro/probleme/926/fsumdiv3rec

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
int sum3(int v[], int n)
{
    if (n == 0)
        return 0;

    int s = sum3(v, n-1);
    if (v[n-1] % 3 == 0)
        s += v[n-1];

    return s;
}

11. https://www.pbinfo.ro/probleme/4209/difparimpar

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
int DifParImpar(int n)
{
    if(n < 10){
        if(n % 2 == 0)
            return 1;
        else
            return -1;
    }
    int x=n%10;
    if(x % 2 == 0)
        return 1 + DifParImpar(n/10);
    else
        return -1 + DifParImpar(n/10);
}

12. https://www.pbinfo.ro/probleme/1842/crearenumarrec

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
void F(int n, int a[], int &k)
{
    if (n == 0)
        k = -1;
    else {
        F(n-1, a, k);
        if (a[n-1] % 2 ==0) {
            if (k == -1)
                k = a[n-1];
            else
                k = k * 10 + a[n-1];
        }
    }
}

13. https://www.pbinfo.ro/probleme/920/cifmaxminrec

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
void cifmaxmin(int n, int &max, int &min)
{
    if (n < 10) {
        max = n; 
        min = n;
    }
    else {
        cifmaxmin(n/10, max, min);
        if (n % 10 > max)
            max = n % 10;
        if (n % 10 < min)
            min = n % 10;
    }
}