Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.0 kB
4
Indexable
Never
#include <iostream>
#include <cmath>
using namespace std;

int n,x,mini;

struct nod
{
    int info;
    nod* urm;
};

nod* p=NULL;

void adaugarefinal(nod*& p,int x)
{
    nod *q=new nod;
    q->info=x;
    q->urm=NULL;
    if(p==NULL)
    {
        p=q;
    }
    else
    {
        nod *t=p;
        while(t->urm)
            t=t->urm;
        t->urm=q;
    }
}

void afisare(nod* p)
{
    while(p)
    {
        cout<<p->info<<" ";
        p=p->urm;
    }
}

int exista_pp(nod *p)
{
    int i=0;
    while(p)
    {
        if(sqrt(p->info)==(int)sqrt(p->info))
            return 1;
        p=p->urm;
    }
    return 0;
}

void pp(nod*p)
{
    while(p)
    {
        if(sqrt(p->info)==(int)sqrt(p->info))
            cout<<p->info<<" ";
        p=p->urm;
    }
}

int nr_prime(nod *p)
{
    int k=0,ok;
    while(p)
    {
        ok=1;
        if(p->info<2)
            ok=0;
        else
            if(p->info==2 || p->info==3)
            ok=1;
        else
        {
            for(int i=2;i<=p->info/2;i++)
                if(p->info%i==0)
            {
                ok=0;
                break;
            }
        }
        if(ok)
            k++;

        p=p->urm;

    }
    return k;
}

int ordine(nod *p)
{
    while(p->urm)
    {
        if(p->info < p->urm->info)
            return 0;
        p=p->urm;
    }
    return 1;
}

int nr_mini(nod *p)
{
    int i;
    while(p)
    {
        if(p->info==mini)
            i++;
        p=p->urm;
    }
    return i;
}

void dublare(nod*& p)
{
    while(p)
    {
        int s=0;
        int ok=0;
        int copie=p->info;
        while(copie)
        {
            s+=copie%10;
            copie/=10;
        }
        if(s<15)
        {
            nod *q=new nod;
            q->info=p->info;
            q->urm=p->urm;
            p->urm=q;
            ok=1;

        }
        if(ok==0)
        p=p->urm;
        else
        p=p->urm->urm;
    }
}

int main()
{
    int ok=1;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        adaugarefinal(p,x);

        if(ok==1)
        {
            mini=x;
            ok=0;
        }
        if(x<mini)
            mini=x;
    }
    cout<<"Afisare lista: ";
    afisare(p);
    cout<<endl;
    int a=exista_pp(p);
    if(a)
    {
        cout<<"Numere pp: ";
        pp(p);
        cout<<endl;
    }
    else
        cout<<"Nu exista pp."<<endl;

    int b=nr_prime(p);
    if(b==0)
        cout<<"Nu exista nr prime."<<endl;
    else
        cout<<"Numere prime in total: "<<b<<endl;
    int c=ordine(p);
    if(c)
        cout<<"Lista este ordonata descrescator."<<endl;
    else
        cout<<"Lista nu este ordonata descrescator."<<endl;

    cout<<"Numarul minim: "<<mini<<" apare de "<<nr_mini<<" ori."<<endl;

    dublare(p);
    cout<<"Afisare dublu: ";
    afisare(p);



    return 0;
}