fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int MAX_SIZE = 5;
  5.  
  6. int main() {
  7. /*
  8.   Algoritm realizat pentru dimensiuni mari ale matricei
  9.   */
  10. int size, mt[MAX_SIZE + 1][MAX_SIZE + 1];
  11. cin >> size;
  12. int ascendingSums[MAX_SIZE + 1];
  13. for (int line = 1; line <= size; ++line) {
  14. mt[line][0] = 0;
  15. ascendingSums[line] = 0;
  16. for (int col = 1; col <= size; ++col) {
  17. cin >> mt[line][col];
  18. mt[line][0] += mt[line][col];
  19. ascendingSums[line] += mt[line][col];
  20. }
  21. for (int k = 2; k <= line; ++k) {
  22. if (ascendingSums[line] < ascendingSums[k - 1]) {
  23. int aux = ascendingSums[k - 1];
  24. ascendingSums[k - 1] = ascendingSums[line];
  25. ascendingSums[line] = aux;
  26. }
  27. }
  28. }
  29. for (int i = 1; i <= size; ++i) {
  30. for (int line = i; line <= size; ++line) {
  31. if (ascendingSums[i] == mt[line][0]) {
  32. if (line != i) {
  33. for (int col = 0; col <= size; ++col) {
  34. int aux = mt[i][col];
  35. mt[i][col] = mt[line][col];
  36. mt[line][col] = aux;
  37. }
  38. }
  39. for (int col = 1; col <= size; ++col) {
  40. cout << mt[i][col] << " ";
  41. }
  42. line = size + 1;
  43. cout << "\n";
  44. }
  45. }
  46. }
  47. return 0;
  48. }
Success #stdin #stdout 0s 5300KB
stdin
5
9 8 7 6 5
8 7 6 5 4
7 6 5 4 3
6 5 4 3 2
5 4 3 2 1
	 
5 4 3 2 1
6 5 4 3 2
7 6 5 4 3
8 7 6 5 4
9 8 7 6 5
stdout
5 4 3 2 1 
6 5 4 3 2 
7 6 5 4 3 
8 7 6 5 4 
9 8 7 6 5