Pushok

task6

Jun 17th, 2014
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. bool IsPalindrome(int n)
  2. {
  3.     int s = 0, m = n;
  4.     do
  5.         s = s*10+m % 10;
  6.     while(m /= 10);
  7.  
  8.     return s == n;
  9. }
  10.  
  11. void Sort(int* a, const int* pal, int np)
  12. {
  13.     for (int i = 0; i < np - 1; ++i)
  14.     {
  15.         for (int j = np - 2; j >= i; --j)
  16.         {
  17.             if (a[pal[j]] > a[pal[j + 1]])
  18.             {
  19.                 std::swap(a[pal[j]], a[pal[j + 1]]);
  20.             }
  21.         }
  22.     }
  23. }
  24.  
  25. void main()
  26. {
  27.     FILE *f = fopen("file.txt", "r");
  28.     int s, *a, *pa, s1 = 0;
  29.     fscanf(f, "%d", &s);
  30.     a = (int*)malloc(sizeof(int)* s);
  31.     pa = (int*)malloc(sizeof(int)* s);//массив для палиндромов
  32.  
  33.     for (int i = 0; i < s; ++i)
  34.     {
  35.         fscanf(f, "%d", &a[i]);
  36.         if (IsPalindrome(a[i]))
  37.             pa[s1++] = i;
  38.     }
  39.  
  40.     Sort(a, pa, s1);
  41.  
  42.     for (int i = 0; i < s; ++i)
  43.         std::cout << a[i] << "\n";
  44.  
  45.     fclose(f);
  46.     getchar();
  47. }
Advertisement
Add Comment
Please, Sign In to add comment