c++已知一个矩阵,存储在一个二维数组中。将矩阵中和值为最大的那一行 元素与首行对换。

定义函数方法,便于后期维护

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值