(第三次上机)已知一个矩阵,储存在一个二维数组中。将矩阵中和值最大的那一行元素与首行对换。
作者:互联网
在这里插入代码片
#include<iostream>
using namespace std;
int main()
{
int arr[5][5]{
{1,2,3,4,5},
{2,3,4,5,6},
{3,4,5,6,7},
{1,1,1,1,1},
{2,2,2,2,2}
};
int arr2[5];
for (int i = 0; i < 5; i++) {
int sum = 0;
for (int j = 0; j < 5; j++) {
sum += arr[i][j];
}
arr2[i] = sum;
}
int max = arr2[0];
for (int i = 0; i < 5; i++) {
if (max < arr2[i]) {
max = arr2[i];
}
}
int t[3][5];
for (int i = 0; i < 5; i++) {
if (max == arr2[i]) {
for (int j = 0; j < 5;j++) {
t[0][j] = arr[0][j];
arr[0][j] = arr[i][j];
arr[i][j] = t[0][j];
}
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cout << arr[i][j];
}
cout << endl;
}
return 0;
}
标签:arr,int,矩阵,sum,对换,首行,++,arr2,max 来源: https://blog.csdn.net/qq_51720038/article/details/110732835