fork download
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int compare(const void *a, const void *b){return *(char *)a - *(char *)b;}
  5. char *f(char *s, int n) {
  6. char c, *p, *q, *min, *e = s + strlen(s);
  7. while (0 < --n)
  8. for (p = e - 2; s <= p; p--)
  9. if (p[0] < p[1]) {
  10. for (min = q = p + 1; q < e; q++)
  11. if (*p < *q && *q < *min) min = q;
  12. c = *p;*p = *min;*min = c;
  13. qsort(p + 1, e - (p + 1), sizeof *p, compare);
  14. break;
  15. }
  16. return s;
  17. }
  18. int main() {
  19. char buff[64];
  20. #define g(s, n) printf("%s\n", f(strcpy(buff, (s)), (n)))
  21. g("123456789", 1);
  22. g("123456789", 2);
  23. g("123456789", 3);
  24. g("123456789", 123456);
  25. g("123456789", 234567);
  26. g("123456789", 362880);
  27. g("111222333444", 1);
  28. g("111222333444", 2);
  29. g("111222333444", 3);
  30. g("111222333444", 123456);
  31. g("111222333444", 234567);
  32. g("111222333444", 3369600);
  33. #undef g
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0.07s 5320KB
stdin
Standard input is empty
stdout
123456789
123456798
123456879
416589732
684753219
987654321
111222333444
111222334344
111222334434
222331434114
324424331112
444333222111