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