题目描述
编写一个过程,对一个n×n的矩阵,通过行变换,使其每行元素的平均值按递增顺序排列
输入
4
5 5 5 5
4 4 4 4
3 3 3 3
1 1 1 1
输出
1 1 1 1
3 3 3 3
4 4 4 4
5 5 5 5
样例输入
<span style="color:#333333">3
1 2 3
4 5 6
1 1 1</span>
样例输出
<span style="color:#333333">1 1 1
1 2 3
4 5 6</span>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int average;
}Line;
typedef struct {
Line *column[MAXSIZE];
int length;
}Array;
int main() {
int n;
scanf("%d",&n);
int i,j;
int sum;
Array array;
for(i=0;i<n;i++){
Line *line;
line=(Line *)malloc(sizeof(Line));
sum=0;
for(j=0;j<n;j++){
scanf("%d",&line->data[j]);
sum+=line->data[j];
}
line->average=(sum/n)*100;
array.column[i]=line;
}
Line *line1;
int maxIndex;
for(i=0;i<n-1;i++){
//line1=array.column[0];
maxIndex=0;
for (j = 1; j< n-i; ++j) {
if(array.column[j]->average>array.column[maxIndex]->average){
maxIndex=j;
//line1=array.column[j];
}
}
line1=array.column[n-i-1];
array.column[n-i-1]=array.column[maxIndex];
array.column[maxIndex]=line1;
}
for ( i = 0; i <n ; ++i) {
for(j=0;j<n;j++){
printf("%d",array.column[i]->data[j]);
if(j!=n-1){
printf(" ");
} else{
printf("\n");
}
}
}
return 0;
}