2312:顺序排列【数组】。

2 篇文章 0 订阅
2 篇文章 0 订阅

题目描述

编写一个过程,对一个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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值