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;
 }

Divizibilitate

1. Verificare numar prim
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
    int n,i,OK;
    cout<<"n=";cin>>n;
    OK=1;
    for (i=2;i<=sqrt(n)&&OK==1;i++)
        if(n%i==0) OK=0;
    if (OK==1) cout<<"DA";
    else cout<<"NU";
    return 0;
}


2. Numar perfect
#include <iostream>

using namespace std;

int main ()
{
    int n,i,S;
    cout<<"n=";cin>>n;
    S=0;
    for (i=1;i<=n/2;i++)
        if (n%i==0) S=S+i;
    if (S==n) cout<<"DA";
    else cout<<"NU";
    return 0;
}

Cmmdc

1. Scaderi repetate
include <iostream>

using namespace std;

int main ()
{
    int x,y;
    cout<<"x=";cin>>x;
    cout<<"y=";cin>>y;
    while (x!=y)
    {
        if (x>y) x=x-y;
        else y=y-x;
    }
    cout<<"cmmdc="<<x;
    return 0;
}


2. Euclid
include <iostream>

using namespace std;

int main ()
{
    int x,y,r;
    cout<<"x=";cin>>x;
    cout<<"y=";cin>>y;
    r=x%y;
    while (r!=0)
    {
        x=y;
        y=r;
        r=x%y;
    }
    cout<<"cmmdc="<<y;
    return 0;
}

Cifrele unui numar

1. Afisare
#include <iostream>

using namespace std;

int main ()
{
    int x,c;
    cout<<"x=";cin>>x;
    while (x!=0)
    {
        c=x%10;
        x=x/10;
        cout<<c<<"\n";
    }
    return 0;
}

2.Palindrom
#include <iostream>

using namespace std;

int main()
{
    int x,y,c,ogl;
    cout<<"x=";cin>>x;
    ogl=0;
    y=x;
    while (x!=0)
    {
        c=x%10;
        x=x/10;
        ogl=ogl*10+c;
    }
    if(ogl==y)cout<<"DA";
    else cout<<"NU";
    return 0;

3.Eliminarea unei cifre
#include <iostream>

using namespace std;

int main()
{
    int a,x,p,c;
    cout<<"a=";cin>>a;
    x=0;
    p=1;
    while(a!=0)
    {
       c=a%10;
       a=a/10;
       if(c%2==0)
       {
          x=x+c*p;
          p=p*10;
       }
    }
    cout<<"x="<<x;
    return 0;
}

4.Verificare munte
#include <iostream>

using namespace std;

int main()
{
    int n,c,x,OK;
    cout<<"n=";cin>>n;
    x=n;
    OK=1;
    while(n>9&&n%10<n/10%10)
       n=n/10;
    if(n==x||n<10) OK=0;
    while(n>9&&n%10>n/10%10)
       n=n/10;
    if(n>9) OK=0;
    if(OK==1) cout<<"DA";
    else cout<<"NU";
    return 0;
}