Advertisement
icatalin

Problema joi, neterm.

Mar 6th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. //Se citesc un nr. cu max 18 cifre. Verificati daca cu cifrele nr. citit se pooate forma un palindrom, daca da,
  2. //afisati cel mai mare si cel mai mic palindrom.
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     long long x,fw[10]={0,0,0,0,0,0,0,0,0,0},nr[19],i,k,imp=-1,kk=0,j,v[100];
  11.     cout<<"x= ";cin>>x;
  12.     while (x)
  13.     {
  14.         fv[x%10]++;
  15.         kk++;
  16.         x=x/10;
  17.     }
  18.  
  19.     for (i=0;i<=9;i++)
  20.     {
  21.         if(fv[i]%2==1)
  22.         k++;imp=i;
  23.     }
  24.     if (k>1)
  25.     {cout<<"NU";
  26.     return 0;
  27.     }
  28.     else
  29.     cout<<"DA"<<'\n';
  30.     // construim palindromul cel mai mic. TEMA: De facut cel mai mare.
  31.     k=0;
  32.     for (cif=0;cif<=9;cif++)
  33.     for (i=1;i<=fv[cif]/2;i++)
  34.     {
  35.         k++;
  36.         nr[k]=cif;
  37.     }
  38.     if (imp!=-1)
  39. {
  40.     k++;
  41.     nr[k]=imp;
  42. }
  43.    if (nr[1]==0)
  44.    {
  45.       i=2;
  46.       while (v[i]==0)
  47.       i++;
  48.       v[1]=v[i];
  49.       v[i]=0;
  50.    }
  51.    for (i=1,j=kk;i<j;i++,j--)
  52.    nr[j]=nr[i];
  53.    for (i=1;i<=kk;i++)
  54.    cout<<nr[i];
  55.  
  56.  
  57.  
  58.     return 0;
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement