Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Problema turnurilor din Hanoi

#include <iostream>
using namespace std;
void hanoi(int n, char A, char B, char C)
{
    if(n==1)
        cout<<A<<"->"<<B<<endl;
    else
    {
        hanoi(n-1,A,C,B);
        cout<<A<<"->"<<B<<endl;
        hanoi(n-1,C,B,A);
    }
}

int main()
{
    int n;
    cin>>n;
    hanoi(n,'A','B','C');
    return 0;
}

Elementul maxim dintr-un vector


#include <iostream>
using namespace std;
int n,v[101],i;
int maxim (int st, int dr)
{
    int m,maxst,maxdr;
    if(st==dr)
        return v[st];
    else
  {
        m=(st+dr)/2;
        maxst=maxim(st,m-1);
        maxdr=maxim(m+1,dr);
        if(maxst>maxdr)
            return maxst;
        else
            return maxdr;
  }
}
int main()
{
    cin>>n;
    for(i=1; i<=n; i++)
        cin>>v[i];
    cout<<maxim(1,n);
    return 0;
}

You might also like