Guest User

C++ prog valasz

a guest
Feb 14th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 5;
  6.  
  7. void Cserel(char * szam, int i1, int i2)
  8. {
  9.     char ch;
  10.     ch = szam[i1];
  11.     szam[i1] = szam[i2];
  12.     szam[i2] = ch;
  13. }
  14.  
  15. char *Keres(char * szam)
  16. {
  17. a:
  18.     int indA, indB, meddig, i, j;
  19.  
  20.     indA = -1;
  21.     i = N - 1;                      // utolso szamjegy indexe
  22.     while (i > 0 && indA == -1)
  23.     {
  24.         if (szam[i - 1] < szam[i])
  25.         {
  26.             indA = i - 1;           // az utolso szamjegy indexe, ami lokalis maximum elott van
  27.         }
  28.         i--;
  29.     }
  30.     if (indA == -1)
  31.     {
  32.         goto a;     // ez hulyeseg, vegtelen ciklus lesz...
  33.     }
  34.     indB = indA + 1;
  35.     for (j = indA + 2; j < N - 1; j++)
  36.     {
  37.         if (szam[j] > szam[indA] && szam[j] < szam[indB])
  38.         {
  39.             indB = j;       // a lokalis maximum hupli masik vege ... nagyjabol, de nem ertem
  40.         }
  41.     }
  42.     Cserel(szam, indA, indB);
  43.     for (meddig = N - 1; meddig < N; meddig++)
  44.     {
  45.         for (j = indA + 1; j < meddig - 1; j++)
  46.         {
  47.             if (szam[j] > szam[j + 1])
  48.             {
  49.                 Cserel(szam,j,j+1);         // ezt sem ertem, hogy mit akar csinalni
  50.             }
  51.         }
  52.     }
  53.  
  54.     return szam;
  55. }
  56.  
  57. int main()
  58. {
  59.     char szam[5+1];
  60.     szam[0] = '5';
  61.     szam[1] = '2';
  62.     szam[2] = '6';
  63.     szam[3] = '3';
  64.     szam[4] = '1';
  65.     szam[5] = 0;
  66.     cout << szam << endl;
  67.     cout << Keres(szam) << endl;
  68.         system("pause");
  69. }
Advertisement
Add Comment
Please, Sign In to add comment