Advertisement
icatalin

problema clasa 13.5.2015 nu merge

May 13th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. /*
  2. Inserati inaintea fiecarui nod cu informatie numar prim cel mai mic palindrom mai mare sau egal cu el.
  3. */
  4. #include <fstream>
  5. #include <iostream>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. ifstream f("date.in");
  11.  
  12. struct nod
  13. {
  14.     int info;
  15.     nod *urm;
  16. };
  17.  
  18.  
  19. nod *prim;
  20.  
  21. void creare()
  22. {
  23.     nod *nou,*p;
  24.     int x;
  25.  
  26.     while (f>>x)
  27.         if (prim==NULL)
  28.     {
  29.         prim=new nod;
  30.         prim->info=x;
  31.         p=prim;
  32.     }
  33.     else
  34.     {
  35.         nou=new nod;
  36.         nou->info=x;
  37.         p->urm=nou;
  38.         p=nou;
  39.     }
  40.  
  41.     p->urm=NULL;
  42. }
  43.  
  44. void afisare()
  45. {
  46.     nod *p=prim;
  47.     while (p)
  48.     {
  49.         cout<<p->info<<" ";
  50.         p=p->urm;
  51.     }
  52. }
  53.  
  54. int nrprim(int x)
  55. {
  56.     int i,ok=0;
  57.  
  58.     if (x==0 || x==1)
  59.         return 0;
  60.  
  61.     for (i=2;i<=sqrt(x);i++)
  62.         if (x%i==0)
  63.         ok=1;
  64.  
  65.     if (ok==1)
  66.         return 1;//nu e prim
  67.  
  68.     return 0; // e prim
  69.  
  70. }
  71.  
  72. int palindrom(int x)
  73. {
  74.     int c=x,nr=0;
  75.     while (x)
  76.     {
  77.         nr=nr*10+x%10;
  78.         x=x/10;
  79.     }
  80.  
  81.     if (nr==c)
  82.         return 1;//e palindrom
  83.     return 0;//nu e palindrom
  84. }
  85.  
  86. int urm_pal(int x)
  87. {
  88.     while (palindrom(x)!=0)
  89.         x++;
  90.     return x;
  91. }
  92.  
  93. void inserare()
  94. {
  95.     nod *p=prim,*nou;
  96.     if(nrprim(p->info))
  97.     {
  98.         nou=new nod;
  99.         nou->info=urm_pal(p->info);
  100.         prim=nou;
  101.     }
  102.  
  103.     while (p->urm!=NULL)
  104.     {
  105.         if (nrprim(p->urm->info))
  106.         {
  107.             nou=new nod;
  108.             nou->info=urm_pal(p->urm->info);
  109.             nou->urm=p->urm;
  110.             p->urm=nou;
  111.             p=p->urm->urm;
  112.         }
  113.         else
  114.             p=p->urm;
  115.     }
  116. }
  117.  
  118. int main()
  119. {
  120.  
  121. creare();
  122. inserare();
  123. afisare();
  124.  
  125.  
  126.  
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement