Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.4 kB
8
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 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 pp(nod* p)//verificare daca avem pp
{
    while (p)
    {
        if (sqrt(p->info) == (int)sqrt(p->info))
        {
            return 1;
        }
        p = p->urm;
    }
    return 0;
}

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

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

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

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

int main()
{
    int ok = 0;
    cin >> n;

    //citire lista && determinare minim
    for (int i = 1; i <= n; i++)
    {
        cin >> x;
        adaugarefinal(p, x);

        if (ok == 0)
        {
            mini = x;
            ok = 1;
        }
        if (mini > x)
            mini = x;
    }

    //1.afisare lista
    cout << "Afisare lista: ";
    afisare(p);
    cout << endl;

    //2.afisare pp
    int a = pp(p);
    if (a)//conditie daca avem pp
    {
        cout << "Afisare pp: ";
        afisare_pp(p);
        cout << endl;
    }
    else
        cout << "Nu exista pp." << endl;

    //3.afisare nr prime
    int b = nr_prime(p);
    if (b == 0)//conditie daca avem nr prime
        cout << "Nu exista numere prime." << endl;
    else
        cout << "Numere prime in total: " << b << endl;

    //4.ordinea listei
    int c = ordine(p);
    if (c)
        cout << "Lista este ordonata descrescator." << endl;
    else
        cout << "Lista NU este ordonata descrescator." << endl;

    //5.nr aparitii val mini
    cout << "Numarul minim: " << mini << " apare in lista de " << numar_mini(p) << " ori." << endl;

    //6.dublare elemente(sum<15)
    dublare(p);
    cout << "Afisare duble : ";
    afisare(p);
    
    return 0;
}