#include #define N 10
#define MAXSIZE 100
typedef struct node{
int arr[N][N];
int len;
struct node * pnext;
} Node;
void array_change(Node * p);
int comp(const void * a, const void * b);
void list_tail_insert(Node ** pphead, Node ** pptail, Node * p);
void list_print(Node * phead);
int main(){
Node * phead = NULL, * ptail = NULL;
int n, i, j;
FILE * fp = fopen("F:\\matrix.txt", "r+");
while(fscanf(fp, "%d", &n), n != 0){
fseek(fp, 2, SEEK_CUR); // 跳过换行符(占两个字符)
Node * pnew = (Node *)calloc(1, sizeof(Node));
pnew->len = n;
for(i = 1; i <= pnew->len; i++){
for(j = 1; j <= pnew->len; j++){
fscanf(fp, "%d", &n);
fseek(fp, 1, SEEK_CUR); // 跳过空格符
pnew->arr[i][j] = n;
}
}
array_change(pnew);
list_tail_insert(&phead, &ptail, pnew);
}
list_print(phead);
return 0;
}
void array_change(Node * p){
int i, j, k, max, lineIndex, m;
int * pNum[N];
int temp[N];
for(i = 1; i <= p->len; i++){ // 列
k = 0;
for(j = i; j <= p->len; j++){ // 行
pNum[k++] = &(p->arr[j][i]);
}
qsort(pNum, k, sizeof(int *), comp);
max = * pNum[k - 1];
for(j = i; j <= p->len; j++){ // 找到当前列最大值所在行号
if(p->arr[j][i] == max){
lineIndex = j;
}
}
for(m = 1; m <= p->len; m++){ // 交换当前列最大值所在行与当前第一行元素
temp[m] = p->arr[lineIndex][m];
p->arr[lineIndex][m] = p->arr[i][m];
p->arr[i][m] = temp[m];
}
}
return;
}
int comp(const void * a, const void * b){
int * x = * (int **)a;
int * y = * (int **)b;
return * x - * y;
}
void list_tail_insert(Node ** pphead, Node ** pptail, Node * p){
if(* pphead == NULL){
* pphead = p;
* pptail = p;
}else{
(* pptail)->pnext = p;
* pptail = p;
}
return;
}
void list_print(Node * phead){
int i, j;
while(phead != NULL){
for(i = 1; i <= phead->len; i++){
for(j = 1; j <= phead->len; j++){
printf("%d ", phead->arr[i][j]);
}
printf("\n");
}
printf("\n");
phead = phead->pnext;
}
return;
}