定义函数方法,便于后期维护
#include <iostream>
using namespace std;
int max_2(int p, int q);
int max_4(int p, int q, int s, int t);
int first_line();
int second_line();
int third_line();
int fourth_line();
int main() {
cout << "原矩阵" << endl;
int arr[4][5] = { {1,2,3,4,5},{2,4,6,8,10},{3,6,9,12,15},{4,8,12,16,20} };
int a = 0, b = 0;
for (;a < 4;++a) {
for (;b < 5;++b) {
cout << arr[a][b] << '\t';
if (b == 4)
cout << endl;
}
b = 0;
}
int sum,sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
int i = 0, r = 0;
for (; r < 5;++r) {
sum1 += arr[i][r];
}
++i,r=0;
for (; r < 5;++r) {
sum2 += arr[i][r];
}
++i,r=0;
for (; r < 5;++r) {
sum3 += arr[i][r];
}
++i,r=0;
for (; r < 5;++r) {
sum4 += arr[i][r];
}
sum = max_4(sum1, sum2, sum3, sum4);
cout << "改后矩阵" << endl;
if (sum == sum1) {
first_line();
second_line();
third_line();
fourth_line();
}
if (sum == sum2) {
second_line();
first_line();
third_line();
fourth_line();
}
if (sum == sum3) {
third_line();
first_line();
second_line();
fourth_line();
}
if (sum == sum4) {
fourth_line();
first_line();
second_line();
third_line();
}
return 0;
}
int max_2(int p, int q){
if (p > q)
{
return p;
}
else
{
return q;
}
}
int max_4(int p, int q, int s, int t){
int m;
m = p;
m = max_2(m, q);
m = max_2(m, s);
m = max_2(m, t);
return m;
}
int first_line()
{
int arr[4][5] = { {1,2,3,4,5},{2,4,6,8,10},{3,6,9,12,15},{4,8,12,16,20} };
int a = 0, b = 0;
for (;b < 5;++b) {
cout << arr[0][b] << '\t';
if (b == 4)
cout << endl;
}
return 0;
}
int second_line()
{
int arr[4][5] = { {1,2,3,4,5},{2,4,6,8,10},{3,6,9,12,15},{4,8,12,16,20} };
int a = 0, b = 0;
for (;b < 5;++b) {
cout << arr[1][b] << '\t';
if (b == 4)
cout << endl;
}
return 0;
}
int third_line()
{
int arr[4][5] = { {1,2,3,4,5},{2,4,6,8,10},{3,6,9,12,15},{4,8,12,16,20} };
int a = 0, b = 0;
for (;b < 5;++b) {
cout << arr[2][b] << '\t';
if (b == 4)
cout << endl;
}
return 0;
}
int fourth_line()
{
int arr[4][5] = { {1,2,3,4,5},{2,4,6,8,10},{3,6,9,12,15},{4,8,12,16,20} };
int a = 0, b = 0;
for (;b < 5;++b) {
cout << arr[3][b] << '\t';
if (b == 4)
cout << endl;
}
return 0;
}