Cele mai mari/mici 2,3,4 elemente din vector

1. Cele mai mari 2 elemente
#include <iostream>
#include <climits>
using namespace std;
int main()
 {
 int n,i,max1,max2;
 cout<<"n=”;cin>>n;
 int v[n+1];
 max1=max2=INT_MIN;
 for (i=1;i<=n;++i)
 {
 cout<<"v["<<i<<"]=”; cin>>v[i];
 }
 for(i=1;i<=n;++i)
 {
 if(v[i]>max1) {max2=max1;max1=v[i];}
 else if (v[i]>max2) max2=v[i];
 }
 cout<<"max1=”<<max1<<"\n"<<"max2=”<<max2;
 return 0;




2. Cele mai mari 3 elemente
#include <iostream>
#include <climits>
using namespace std;
int main()
 {
 int n,i,max1,max2,max3;
 cout<<"n=”;cin>>n;
 int v[n+1];
 max1=max2=max3=INT_MIN;
 for (i=1;i<=n;++i)
 {
 cout<<"v["<<i<<"]=”; cin>>v[i];
 }
 for (i=1;i<=n;++i)
 {
 if (v[i]>max1) {max3=max2;max2=max1;max1=v[i];}
 else if (v[i]>max2) {max3=max2;max2=v[i];}
 else if (v[i]>max3)  max3=v[i];
 }
 cout<<"max1=”<<max1<<"\n”<<"max2=”<<max2<<"\n”<<"max3=”<<max3;
 return 0;
 }



3. Cele mai mari 4 elemente 
#include <iostream>
#include <climits>
using namespace std;
int main()
 {
 int n,i,max1,max2,max3,max4;
 cout<<"n=”;cin>>n;
 int v[n+1];
 max1=max2=max3=max4=INT_MIN;
 for (i=1;i<=n;++i)
 {
 cout<<"v["<<i<<"]=”; cin>>v[i];
 }
 for (i=1;i<=n;++i)
 {
 if (v[i]>max1) {max4=max3;max3=max2;max2=max1;max1=v[i];}
 else if (v[i]>max2) {max4=max3;max3=max2;max2=v[i];}
 else if (v[i]>max3) {max4=max3;max3=v[i];}
 else if (v[i]>max4) max4=v[i] ;
 }
 cout<<"max1=”<<max1<<"\n”<<"max2=”<<max2<<"\n”<<"max3=”<<max3<<"\n"<<"max4=”<<max4;
 return 0;
 }



4. Cele mai mici 2 elemente
#include <iostream>
#include <climits>
using namespace std;
int main()
 {
 int n,i,min1,min2;
 cout<<"n=”;cin>>n;
 int v[n+1];
 min1=min2=INT_MAX;
 for (i=1;i<=n;++i)
 {
 cout<<"v["<<i<<"]=”; cin>>v[i];
 }
 for(i=1;i<=n;++i)
 {
 if(v[i]<min1) {min2=min1;min1=v[i];}
 else if (v[i]<min2) min2=v[i];
 }
 cout<<"min1=”<<min1<<"\n"<<"min2=”<<min2;
 return 0;
}



5. Cele mai mici 3 elemente
#include <iostream>
#include <climits>
using namespace std;
int main()
 {
 int n,i,min1,min2,min3;
 cout<<"n=”;cin>>n;
 int v[n+1];
 min1=min2=min3=INT_MAX;
 for (i=1;i<=n;++i)
 {
 cout<<"v["<<i<<"]=”;cin>>v[i];
 }
 for (i=1;i<=n;++i)
 {
 if (v[i]<min1) {min3=min2;min2=min1;min1=v[i];}
 else if (v[i]<min2) {min3=min2;min2=v[i];}
 else if (v[i]<min3)  min3=v[i];
 }
 cout<<"min1=”<<min1<<"\n”<<"min2=”<<min2<<"\n”<<"min3=”<<min3;
 return 0;
 }




6. Cele mai mici 4 elemente
#include <iostream>
#include <climits>
using namespace std;
int main()
 {
 int n,i,min1,min2,min3,min4;
 cout<<"n=”;cin>>n;
 int v[n+1];
 min1=min2=min3=min4=INT_MAX;
 for (i=1;i<=n;++i)
 {
 cout<<"v["<<i<<"]=”; cin>>v[i];
 }
 for (i=1;i<=n;++i)
 {
 if (v[i]<min1) {min4=min3;min3=min2;min2=min1;min1=v[i];}
 else if (v[i]<min2) {min4=min3;min3=min2;min2=v[i];}
 else if (v[i]<min3) {min4=min3;min3=v[i];}
 else if (v[i]<min4) min4=v[i] ;
 }
 cout<<"min1=”<<min1<<"\n”<<"min2=”<<min2<<"\n”<<"min3=”<<min3<<"\n"<<"min4=”<<min4;
 return 0;
 }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s