问题 F: Problem B

2017-01-12 19:07:02来源:CSDN作者:qq_22194315人点击

4
15 8 -2 6
31 24 18 71
-3 -9 27 13
17 21 38 69

159 145 144 135 81 60 44 32 28 27

`#include<iostream>#include<string>#include<vector>#include<algorithm>using namespace std;bool cmp(int a, int b){	return a > b;}//记住左小右大，正常的从小到大排序是return a<b;,那么从大到小就反过来就行int main(){	int m;	 int temp;	vector<vector<int> > ss; vector<int> s;//声明二维向量容器和一维向量容器	while (cin >> m)//这题题目有点BUG，应该是测试多组数据，否则过不了	{		for (int i = 0; i < m; i++)		{			for (int j = 0; j < m; j++)			{				cin >> temp;				s.push_back(temp);			}			ss.push_back(s);			s.clear();//要记得每次用完容器后要倒干净		}		int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;		for (int i = 0; i < m; i++)		{			sum1 = 0; sum2 = 0;			for (int j = 0; j < m; j++)			{				sum1 += ss[i][j];//计算每行的和				sum2 += ss[j][i];//计算每列的和				if (i == j)					sum3 += ss[i][j];//计算主对角线的和				if (i + j == m - 1)					sum4 += ss[i][j];//计算副对角线的和，题目的另一个BUG，不想吐槽了我			}			s.push_back(sum1);			s.push_back(sum2);		}		s.push_back(sum3);		s.push_back(sum4);		sort(s.begin(), s.end(), cmp);		for (int i = 0; i < s.size(); i++)		{			if (i != s.size() - 1)				cout << s[i] << " ";			else				cout << s[i] << endl;		}		s.clear();//用完容器一定要倒干净		ss.clear();//由于是多组数据，二维的容器也要倒干净	}	}`