Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Inserati inaintea fiecarui nod cu informatie numar prim cel mai mic palindrom mai mare sau egal cu el.
- */
- #include <fstream>
- #include <iostream>
- #include <cmath>
- using namespace std;
- ifstream f("date.in");
- struct nod
- {
- int info;
- nod *urm;
- };
- nod *prim;
- void creare()
- {
- nod *nou,*p;
- int x;
- while (f>>x)
- if (prim==NULL)
- {
- prim=new nod;
- prim->info=x;
- p=prim;
- }
- else
- {
- nou=new nod;
- nou->info=x;
- p->urm=nou;
- p=nou;
- }
- p->urm=NULL;
- }
- void afisare()
- {
- nod *p=prim;
- while (p)
- {
- cout<<p->info<<" ";
- p=p->urm;
- }
- }
- int nrprim(int x)
- {
- int i,ok=0;
- if (x==0 || x==1)
- return 0;
- for (i=2;i<=sqrt(x);i++)
- if (x%i==0)
- ok=1;
- if (ok==1)
- return 1;//nu e prim
- return 0; // e prim
- }
- int palindrom(int x)
- {
- int c=x,nr=0;
- while (x)
- {
- nr=nr*10+x%10;
- x=x/10;
- }
- if (nr==c)
- return 1;//e palindrom
- return 0;//nu e palindrom
- }
- int urm_pal(int x)
- {
- while (palindrom(x)!=0)
- x++;
- return x;
- }
- void inserare()
- {
- nod *p=prim,*nou;
- if(nrprim(p->info))
- {
- nou=new nod;
- nou->info=urm_pal(p->info);
- prim=nou;
- }
- while (p->urm!=NULL)
- {
- if (nrprim(p->urm->info))
- {
- nou=new nod;
- nou->info=urm_pal(p->urm->info);
- nou->urm=p->urm;
- p->urm=nou;
- p=p->urm->urm;
- }
- else
- p=p->urm;
- }
- }
- int main()
- {
- creare();
- inserare();
- afisare();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement