Jump to content

Recommended Posts

Posted

163-maximul dintr-un sir

166-cautare binara intr-un sir

169-turnurile din hanoi

163.

#include<iostream.h>

#include<conio.h>

int x[20], n, i;

int div_imp(int p, int q)

{

 int mij, max1, max2;

 if(p==q)

	return x[p];

	else

	 {

		mij=(p+q)/2;

		max1=div_imp(p, mij);

		 max2=div_imp(mij+1, q);

		 if(max1>max2)

			return max1;

		 else

			return max2;

			}

		}

void main()

{

cout<<endl<<"n="; cin>>n;

for(i=0; i<=n-1; i++)

 {

 cout<<"x["<<i<<"]="; cin>>x[i];

 }

 cout<<endl<<"valoarea maxima este: "<< div_imp(0, n-1);

 }

166.
#include<iostream.h>

int x[20], a, i, n;

int div_imp(int p, int q)

{

 int mij;

 if(q<p)

	return 0;

	else

	{

	mij=(p+q)/2;

	if(x[mij]==a)

	 return 1;

	else

	 if(a<x[mij])

		return div_imp(p, mij-1);

	 else

		return div_imp(mij+1, q);

		}

 }

 void main()

 {

 cout<<endl<<"n="; cin>>n;

 cout<<endl<<"x[0]="; cin>>x[0];

 for(i=1; i<=n; i++)

do {

 cout<<"x["<<i<<"]="; cin>>x[i];

 }while(x[i]<=x[i-1]);

 cout<<endl<<"a="; cin>>a;

 if(div_imp(0, n-1))

	cout<<endl<<"valoarea cautata este in vector!!!!!";

 else

	cout<<endl<<"nu se afla in vector! porry!!";

 }
169.
#include<iostream.h>

#include<stdio.h>

#include<conio.h>

int n;

char x, y, z;

void hanoi(int n, char x, char y, char z)

{

if(n==1)

 cout<<endl<<x<<y;

else

 {

 hanoi(n-1, x, z, y);

 cout<<endl<<x<<y;

 hanoi(n-1, z, y, x);

 }

}

void main()

{

 cout<<endl<<"dati nr de discuri: "; cin>>n;

 hanoi(n, 'x', 'y', 'z');

 getch();

 }

  • Upvote 1

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.