题目描述
问题描述:
任意给定一个n*n的矩阵,矩阵的行数与列数均为n。你的任务是通过行变换,使得矩阵每行元素的平均值按递增顺序排列。如果出现有的行平均值相同的情况,则按照原顺序输出。
输入与输出要求:
输入一个整数n代表矩阵的行数(列数),n的范围是1—100。然后输入n*n个整数,即此矩阵的元素。矩阵元素的绝对值不会超过1000000。输出经过行变换后的新矩阵。每行的每个元素用空格分隔,注意最后一个元素后为换行符。
程序运行效果:
Sample 1:
3↙
5 5 5↙
3 3 3↙
1 1 1↙
1 1 1
3 3 3
5 5 5
#include <stdio.h>
int main(void)
{
int n;
scanf(\"%d\",&n);
int a[110][110];
int row,col;
for(row=0;row<n;row++) /*读入二维数组*/
for(col=0;col<n;col++)
scanf(\"%d\",&a[row][col]);
int h;
for(row=0;row<n;row++) //计算出每行的平均值,并保存在每行的后面
{ h=0;
for(int i=0;i<n;i++)
h=h+a[row][i];
a[row][n]=h/n;
}
int loc,i,temp[110],q;
for(loc=0;loc<n-1;loc++) //冒泡排序,对刚才求得的平均值进行排序,
{
for(i=n-1;i>=loc+1;i--)
{
if(a[i][n]<a[i-1][n])
{
for(q=0;q<=n;q++)
{
temp[q]=a[i][q]; //两行交换
a[i][q]=a[i-1][q];
a[i-1][q]=temp[q];
}
}
}
}
for(row=0;row<n;row++) // 打印出矩阵
{
for(col=0;col<n;col++)
printf(\"%d \",a[row][col]);
printf(\"\\n\");
}
return 0;
}
**
> 为什么oj上过不去?
**
继续阅读与本文标签相同的文章
-
选择按钮搭配VBA实现数据小型自动化
2026-05-18栏目: 教程
-
Python高级进阶#011 pyqt5按钮QPushButton应用
2026-05-18栏目: 教程
-
Apache Solr Velocity模版注入远程命令执行漏洞复线
2026-05-18栏目: 教程
-
从订货会的功能变迁看出版业的沧海桑田
2026-05-18栏目: 教程
-
ASP.NET Core on K8S深入学习(9)Secret & Configmap
2026-05-18栏目: 教程
