Jump to content

Recommended Posts

Posted

Nume_fis/cerinta probl

35-re-aranjari de elemente intr un sir

36-elementele distincte ale unui sir

38-verifica daca elementele unui vector sunt in ordine crescatoare

40-triunghi de numere

35.


//re-aranjari de elemente intr un sir

//sa se re-aranjeze elementele unui sir, a.i. cele pare sa ocupe primele pozitii in ordine crescatoare

//iar cele impare sa ocupe restul pozitiilor in ordine descrescatoare

#include<iostream.h>

#include<values.h>

int i, j, k, ok, n, min, pmin, temp, v[20], capat;

void main()

{

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

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

{

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

}

//la fiecare pas al algoritmului, privind vectorul ca fiind format din doua parti:

//o parte sortata(v[0], ..., v[k-1]) ce contine elementele pare in ordine crescatoare,

//si o parte nesortata (v[k], ..., v[n]) in care vor ramane elementele impare

k=0; //initial partea sortata este vida

capat=0; //in variabile capat vom memora inceputul partii nearanjate

while(k<=n-1)

{

//determinam minimul min al partii nearanjate

 min=MAXINT;

 ok=0; //variabila ok va indica daca partea nesortata mai contine elemente pare

 for(j=k; j<=n-1; j++)

	if(v[j]<min && v[j]%2==0)

	{

	 pmin=j;

	 min=v[j];

	 ok=1;

	 }

	if(ok) //daca in partea nesortata mai exista elemente pare

	{

	//interschimba minimul min al partii nearanjate cu v[k]

	//crescand astfel partea sortata cu inca un element

	 temp=v[k];

	 v[k]=v[pmin];

	 v[pmin]=temp;

	 k++;

	 }

	 else

	 {

		capat=k;

		k=n; //iese fortat din ciclu

		}

	 }

	cout<<endl;

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

	 cout<<v[i]<<" ";

	 //sorteaza partea nearanjata cu elementele impare

	 if(capat>0)

	 {

		for(i=capat; i<=n-2; i++)

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

			if(v[j]>v[i])

			{

			 temp=v[i];

			 v[i]=v[j];

			 v[j]=temp;

			 }

		}

		cout<<endl;

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

		 cout<<v[i]<<" ";

}

36.

//elementele distincte ale unui sir

#include<iostream.h>

void main()

{

int v[30], u[30], i, n, j, k, e;

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

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

{

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

 }

k=0;

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

{

 for(e=0, j=0; j<=n-1; j++)

 if(u[j]==v[i])

 e=1;

 if(!e)

 u[k++]=v[i];

 }

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

 cout<<u[i]<< " ";

}

38.

#include<iostream.h>

void main()

{

 int n, i, v[20], ok;

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

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

 {

	cout<<endl<<"v["<<i<<"]="; cin>>v[i];

	}

	ok=1;

	i=1;

	while(i<=n-1 && ok==1)

	{

	if(v[i]<v[i-1])

	ok=0;

	i++;

	}

 if(ok)

	cout<<endl<<"sunt in ordine crescatoare";

 else

	cout<<endl<<"nu sunt in ordine crescatoare";}

40.

#include<iostream.h>

void main()

{

 int n, j, i;

 cout<<"dati n: "; cin>>n;

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

 {

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

	 cout<<(2*j-1)<< " ";

	 cout<<endl;

	 }

	}

  • 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.