#include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<vector<int>> matrix(n, vector<int>(n)); vector<int> rowSum(n, 0); // 橫行總和 p0~p(n-1) vector<int> colSum(n, 0); // 直行總和 m0~m(n-1) // 讀取矩陣並計算總和 for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ cin >> matrix[i][j]; rowSum[i] += matrix[i][j]; colSum[j] += matrix[i][j]; } } // 比較 mi 和 pi,計算直行表現較佳的位置數量 int colBetterCount = 0; for(int i = 0; i < n; i++){ if(colSum[i] > rowSum[i]){ colBetterCount++; } } if(colBetterCount > n - colBetterCount){ // 直行表現較佳的位置較多,以直行為橫行重新排列 // 創建 (直行總和, 直行索引) 的配對並排序 vector<pair<int, int>> colPairs; for(int j = 0; j < n; j++){ colPairs.push_back({colSum[j], j}); } sort(colPairs.begin(), colPairs.end()); // 輸出重組後的矩陣(原直行變成新橫行) for(int i = 0; i < n; i++){ int colIndex = colPairs[i].second; for(int j = 0; j < n; j++){ cout << matrix[j][colIndex]; if(j < n-1) cout << " "; } cout << endl; } } else{ // 橫行表現較佳的位置較多,以橫行為基礎重組 // 創建 (橫行總和, 橫行索引) 的配對並排序 vector<pair<int, int>> rowPairs; for(int i = 0; i < n; i++){ rowPairs.push_back({rowSum[i], i}); } sort(rowPairs.begin(), rowPairs.end()); // 輸出重組後的矩陣(原橫行按總和排序後轉置為直行) for(int j = 0; j < n; j++){ for(int i = 0; i < n; i++){ int rowIndex = rowPairs[i].second; cout << matrix[rowIndex][j]; if(i < n-1) cout << " "; } cout << endl; } } return 0; }