fork download
  1. /*
  2.  * File: HoanVi.cpp
  3.  * Author: NVT
  4.  * Email: tiennv@ptitvl.com
  5.  * Created on December 6, 2014, 0:45 PM
  6.  */
  7.  
  8. #include <stdio.h>
  9.  
  10. //Nội dung bài thi
  11.  
  12. void HoanViKeTiep(int a[], int n) {
  13. int i = n - 1;
  14. for (i = 0; i <= n; i++) {
  15. a[i] = i;
  16. }
  17. while (i > 0) {
  18. for (i = 1; i <= n; i++) {
  19. printf("%d ", a[i]);
  20. }
  21. i = n - 1;
  22. printf("\n");
  23. while (i > 0 && a[i] > a[i + 1]) {
  24. i--;
  25. }
  26. int j = n;
  27. while (a[j] < a[i]) {
  28. j--;
  29. }
  30. int tg = a[i];
  31. a[i] = a[j];
  32. a[j] = tg;
  33. int l = i + 1;
  34. int r = n;
  35. while (l < r) {
  36. int tg = a[l];
  37. a[l] = a[r];
  38. a[r] = tg;
  39. l++;
  40. r--;
  41. }
  42. }
  43. }
  44. //Nội dung bài Thi
  45.  
  46. int main() {
  47. int a[10];
  48. HoanViKeTiep(a, 4);
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
1 2 3 4 
1 2 4 3 
1 3 2 4 
1 3 4 2 
1 4 2 3 
1 4 3 2 
2 1 3 4 
2 1 4 3 
2 3 1 4 
2 3 4 1 
2 4 1 3 
2 4 3 1 
3 1 2 4 
3 1 4 2 
3 2 1 4 
3 2 4 1 
3 4 1 2 
3 4 2 1 
4 1 2 3 
4 1 3 2 
4 2 1 3 
4 2 3 1 
4 3 1 2 
4 3 2 1