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. int lineSum[MAX_SIZE + 1];
  12. for (int line = 1; line <= size; ++line) {
  13. lineSum[line] = 0;
  14. for (int col = 1; col <= size; ++col) {
  15. cin >> mt[line][col];
  16. lineSum[line] += mt[line][col];
  17. }
  18. ++sumFreq[lineSum[line]];
  19. }
  20. int nextLinePos = 0;
  21. for (int i = 0; i <= size * MAX_VALUE; ++i) {
  22. if (sumFreq[i] == 1) {
  23. ++nextLinePos;
  24. for (int line = nextLinePos; line <= size; ++line) {
  25. if (i == lineSum[line] /*&& line != nextLinePos*/) {
  26. if (line == nextLinePos) {
  27. for (int col = 1; col <= size; ++col) {
  28. cout << mt[nextLinePos][col] << " ";
  29. }
  30. } else {
  31. int aux1 = lineSum[nextLinePos];
  32. lineSum[nextLinePos] = lineSum[line];
  33. lineSum[line] = aux1;
  34. for (int col = 1; col <= size; ++col) {
  35. int aux2 = mt[nextLinePos][col];
  36. mt[nextLinePos][col] = mt[line][col];
  37. mt[line][col] = aux2;
  38. cout << mt[nextLinePos][col] << " ";
  39. }
  40. }
  41. line = size + 1;
  42. cout << "\n";
  43. }
  44. }
  45. }
  46. }
  47. return 0;
  48. }
Success #stdin #stdout 0.01s 5288KB
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