Advertisement
madidino

teza9-sub3-3

Nov 20th, 2023
1,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int v[10];
  5. int main()
  6. {
  7.     int n;
  8.     ifstream fin("poli.txt");
  9.     fin>>n;
  10.     fin.ignore();
  11.     for(int i=1; i<=n; i++)
  12.     {
  13.         int x;
  14.         fin>>x;
  15.         v[x]++;
  16.     }
  17.     int nr=0;
  18.     for(int i=0; i<=9; i++)
  19.     {
  20.         if(v[i]%2==1)
  21.         {
  22.             nr++;
  23.         }
  24.     }
  25.     int k=8;
  26.     if(nr>=2)
  27.     {
  28.         cout<<-1;
  29.     }
  30.     else
  31.     {
  32.         bool ok=true;
  33.         while(ok && k)
  34.         {
  35.             if(v[k]>=2)
  36.             {
  37.                 cout<<k;
  38.                 v[k]--;
  39.                 ok=false;
  40.             }
  41.             else
  42.             {
  43.                 k=k-2;
  44.             }
  45.         }
  46.         if(!k)
  47.         {
  48.             cout<<-1;
  49.         }
  50.         else
  51.         {
  52.             nr=-1;
  53.             for(int i=9; i>=0; i--)
  54.             {
  55.                 if(v[i]%2==1 && i!=k)
  56.                 {
  57.                     for(int j=1; j<=v[i]/2; j++)
  58.                     {
  59.                         cout<<i;
  60.                     }
  61.                     nr=i;
  62.                     v[i]=v[i]-(v[i]/2)-1;
  63.                 }
  64.                 else
  65.                 {
  66.                     for(int j=1; j<=v[i]/2; j++)
  67.                     {
  68.                         cout<<i;
  69.                     }
  70.                     v[i]=v[i]-(v[i]/2);
  71.                 }
  72.             }
  73.             if(nr!=-1)
  74.             {
  75.                 cout<<nr;
  76.             }
  77.             for(int i=0; i<=9; i++)
  78.             {
  79.                 if(v[i]!=1 || i!=k)
  80.                 {
  81.                     for(int j=1; j<=v[i]; j++)
  82.                     {
  83.                         cout<<i;
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.     }
  89.     cout<<k;
  90.     fin.close();
  91.     return 0;
  92. }
  93.  
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement