Guest User

C++ prog válasz, módosított

a guest
Feb 15th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 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.     int indA, indB, meddig, i, j;
  18.  
  19.     indA = -1;
  20.     i = N - 1;
  21.     while (i > 0 && indA == -1)
  22.     {
  23.         if (szam[i - 1] < szam[i])
  24.         {
  25.             indA = i - 1;
  26.         }
  27.         i--;
  28.     }
  29.     if (indA == -1)
  30.     {
  31.         return szam;
  32.     }
  33.     indB = indA + 1;
  34.     for (j = indA + 2; j <= N - 1; j++)
  35.     {
  36.         if (szam[j] > szam[indA] && szam[j] < szam[indB])
  37.         {
  38.             indB = j;
  39.         }
  40.     }
  41.     Cserel(szam, indA, indB);
  42.     for (meddig = N - 1; meddig >= 1; meddig--)
  43.     {
  44.         for (j = indA + 1; j <= meddig - 1; j++)
  45.         {
  46.             if (szam[j] > szam[j + 1])
  47.             {
  48.                 Cserel(szam,j,j+1);
  49.             }
  50.         }
  51.     }
  52.  
  53.     return szam;
  54. }
  55.  
  56. int main()
  57. {
  58.     char szam[5+1];
  59.     szam[0] = '5';
  60.     szam[1] = '2';
  61.     szam[2] = '6';
  62.     szam[3] = '3';
  63.     szam[4] = '1';
  64.     szam[5] = 0;
  65.     cout << szam << endl;
  66.     cout << Keres(szam) << endl;
  67.         system("pause");
  68. }
Advertisement
Add Comment
Please, Sign In to add comment