intcompare_ints(constvoid* a, constvoid* b){ int arg1 = *(constint*)a; int arg2 = *(constint*)b;
if (arg1 < arg2) return-1; if (arg1 > arg2) return1; return0;
// return (arg1 > arg2) - (arg1 < arg2); // possible shortcut // return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present) }
intcompare_pos(constvoid* a, constvoid* b){ int arg1 = ((const pos*)a)->val; int arg2 = ((const pos*)b)->val;
if (arg1 < arg2) return-1; if (arg1 > arg2) return1; return0;
// return (arg1 > arg2) - (arg1 < arg2); // possible shortcut // return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present) }
intmain(){ int m, n; scanf("%d%d", &m, &n);
memset(sum, 0, sizeof(sum));
for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { int r = scanf("%d", &data[j][i]); if (r == EOF) { printf("ERROR"); return0; } sum[j].val += data[j][i]; } }
/* save as * n row, m col */
for (int i = 0; i < n; ++i) { qsort(&data[i], m, sizeof(int), compare_ints); sum[i].prev = i; }
qsort(sum, n, sizeof(pos), compare_pos);
/* for (int i = 0; i < n; ++i) { */ /* printf("%d ", sum[i].val); */ /* } */ /* printf("\n"); */
for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (j) printf(" "); int k = sum[j].prev; printf("%d", data[k][i]); } printf("\n"); }