fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n = 5;
  7. int a[n] = {4,3,5,1,2};
  8. int t,i,j,k;
  9.  
  10. for (i = 1; i < n; i++) {
  11. for (k = 0; k < n; k++)
  12. cout << a[k] << " ";
  13. cout << endl;
  14.  
  15. for (j = 0; j < n-i; j++) {
  16. if (a[j] < a[j+1]) {
  17. t = a[j];
  18. a[j] = a[j+1];
  19. a[j+1] = t;
  20. }
  21. }
  22. }
  23. cout << "排列好的陣列為" << endl;
  24. for (i = 0; i < n; i++)
  25. cout << a[i] << " ";
  26. return 0;
  27. }
Success #stdin #stdout 0s 5660KB
stdin
Standard input is empty
stdout
4 3 5 1 2 
4 5 3 2 1 
5 4 3 2 1 
5 4 3 2 1 
排列好的陣列為
5 4 3 2 1