Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int N = 5;
- void Cserel(char * szam, int i1, int i2)
- {
- char ch;
- ch = szam[i1];
- szam[i1] = szam[i2];
- szam[i2] = ch;
- }
- char *Keres(char * szam)
- {
- int indA, indB, meddig, i, j;
- indA = -1;
- i = N - 1;
- while (i > 0 && indA == -1)
- {
- if (szam[i - 1] < szam[i])
- {
- indA = i - 1;
- }
- i--;
- }
- if (indA == -1)
- {
- return szam;
- }
- indB = indA + 1;
- for (j = indA + 2; j <= N - 1; j++)
- {
- if (szam[j] > szam[indA] && szam[j] < szam[indB])
- {
- indB = j;
- }
- }
- Cserel(szam, indA, indB);
- for (meddig = N - 1; meddig >= 1; meddig--)
- {
- for (j = indA + 1; j <= meddig - 1; j++)
- {
- if (szam[j] > szam[j + 1])
- {
- Cserel(szam,j,j+1);
- }
- }
- }
- return szam;
- }
- int main()
- {
- char szam[5+1];
- szam[0] = '5';
- szam[1] = '2';
- szam[2] = '6';
- szam[3] = '3';
- szam[4] = '1';
- szam[5] = 0;
- cout << szam << endl;
- cout << Keres(szam) << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment