共一组数据,输入的第一行为一个正整数,表示m,接下来的m行,每行m个整数表示方阵元素。
从大到小排列的一行整数,每个整数后跟一个空格,最后换行。
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<algorithm> using namespace std; bool cmp(const long long & x,const long long & y)//自定义排序,从大到小 { return x > y; } int main(void) { long long m,arr[10][10]; while(cin >> m) { for(int i = 0;i < m;i++) for(int j = 0;j < m;j++) cin >> arr[i][j]; long long arr_sum[22]; int k = 0; for(int i = 0;i < m;i++)//计算各行各列的和 { long long temp = 0; long long temp_2 = 0; for(int j = 0;j < m;j++) { temp += arr[i][j]; temp_2 += arr[j][i]; } arr_sum[k++] = temp; arr_sum[k++] = temp_2; } long long temp = 0; for(int i = 0;i < m;i++)//主对角线的和 temp += arr[i][i]; arr_sum[k++] = temp; temp = 0; int j = 0; for(int i = m - 1;i >= 0;i--)//副对角线的和 { temp += arr[j++][i]; } arr_sum[k++] = temp; sort(arr_sum,arr_sum + k,cmp); //sort(arr_sum,arr_sum + k,greater<long long>());greater<>()从大到小排序,less<>()从小到大排序 for(int i = 0;i < 2 * m + 2;i++) cout << arr_sum[i] << ' '; cout << endl; } return 0; }
//题目有误,包括次对角线 #include <stdio.h> int cmp(const void *a,const void *b){ return *((int *)b)-*((int *)a); } int main(){ int m,i,j,k; while(scanf("%d",&m)!=EOF){ int a[m][m],sum[m*2+1]; for(i=0;i<m*2+2;i++) sum[i]=0; for(i=0,k=0;i<m;i++){ for(j=0;j<m;j++){ scanf("%d ",&a[i][j]); sum[k]+=a[i][j]; } k++; } for(j=0;j<m;j++){ for(i=0;i<m;i++){ sum[k]+=a[i][j]; } k++; } for(i=0;i<m;i++){ sum[k]+=a[i][i]; sum[k+1]+=a[i][m-i-1]; } qsort(sum,m*2+2,sizeof(sum[0]),cmp); for(i=0;i<m*2+1;i++) printf("%d ",sum[i]); printf("%d\n",sum[i]); } }
#include<stdio.h>//1.先求和2.再排序 int main()//多组数据,并且次对角线 { int n,a[10][10],i,j,t; while(scanf("%d",&n)!=EOF) { int b[50]={0},num=0; for(i=0;i<n;i++)//输入 for(j=0;j<n;j++) scanf("%d",&a[i][j]); for(i=0;i<n;i++)//行//行之和 { for(j=0;j<n;j++) b[num]+=a[i][j]; num++;//下一行 } for(i=0;i<n;i++)//列 //列之和 { for(j=0;j<n;j++) b[num]+=a[j][i]; num++;//下一列 } for(i=0;i<n;i++)//对角线1主对角线 for(j=0;j<n;j++) if(i==j) b[num]+=a[i][j]; num++;//下标到0--num-1 一共num个数 for(i=0;i<n;i++)//对角线2次对角线 for(j=0;j<n;j++) if(i+j==n-1) b[num]+=a[i][j]; num++; for(i=0;i<num-1;i++)//冒泡排序 for(j=0;j<num-1-i;j++) if(b[j]<b[j+1]) {//交换 t=b[j];b[j]=b[j+1];b[j+1]=t; } for(i=0;i<num;i++)//输出 printf("%d ",b[i]); printf("\n"); } }
//题目描述太坑,其实还要加上次对角线的值。。。 #include<bits/stdc++.h> using namespace std; bool cmp(int a,int b){ if(a>b) return 1; else return 0; } int main(){ int m; while(cin>>m){ int **a=new int*[m]; int *sum=new int[2*m+2]; for(int i=0;i<2*m+2;i++) sum[i]=0; for(int i=0;i<m;i++) a[i]=new int[m]; for(int i=0;i<m;i++) for(int j=0;j<m;j++) cin>>a[i][j]; for(int i=0;i<m;i++){ sum[2*m]+=a[i][i]; sum[2*m+1]+=a[i][m-i-1]; for(int j=0;j<m;j++){ sum[i]+=a[i][j]; sum[m+i]+=a[j][i]; } } sort(sum,sum+2*m+2,cmp); for(int i=0;i<2*m+1;i++) cout<<sum[i]<<" "; cout<<sum[2*m+1]<<endl; } }
#include <bits/stdc++.h> using namespace std; long long matrix[10][10] = {0}; long long sum[22] = {0}; int main() { int n = 1; while(~scanf("%d",&n)) { for(int i = 0; i < n ;i ++) for(int j = 0 ; j < n; j ++) { scanf("%lld",&matrix[i][j]); } for(int i = 0; i < n; i ++) { for(int j = 0; j < n; j ++) {//TODO gai sum[i] += matrix[i][j];//行 sum[i + n] += matrix[j][i];//列 } //对角线 sum[2 * n ] += matrix[i][i]; sum[2 * n + 1] += matrix[i][n - i - 1]; } sort(sum,sum + 2 * n +2); for(int i = 2 * n + 1 ; i >= 0; i --) { printf("%lld",sum[i]); sum[i] = 0; if(i == 0) printf("\n"); else printf(" "); } } return 0; }
#include<bits/stdc++.h> using namespace std; int m; int a[23]; int main() { ios::sync_with_stdio(false); while(cin >> m) { int x, i, j; fill(a, a+23, 0); for(i = 1;i <= m; i++) { for(j = 1;j <= m; j++) { cin >> x; a[i] += x; a[m+j] += x; if(i == j) a[2*m+1] += x; if(i+j == m+1) a[2*m+2] += x; } } sort(a+1, a+2*m+3 , greater<int>()); for(int i = 1;i <= 2*m+2; i++) cout << a[i] << ' '; cout << '\n'; } return 0; }
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main(){ int n; while(cin >> n){ vector<int> res; int matrix[n][n], sum=0; for(int i=0; i<n; ++i){ sum = 0; for(int j=0; j<n; ++j){ cin >> matrix[i][j]; sum += matrix[i][j]; } res.push_back(sum); } int x=0, y=0; for(int i=0; i<n; ++i){ sum = 0; for(int j=0; j<n; ++j){ sum += matrix[j][i]; } res.push_back(sum); x += matrix[i][i]; y += matrix[i][n-i-1]; } res.push_back(x); res.push_back(y); sort(res.begin(), res.end()); for(int i=res.size()-1; i>=0; --i){ cout << res[i] << ' '; } cout << endl; } }
#include <array> #include <iostream> #include <queue> using namespace std; array<array<int, 10>, 10>matrix; int main() { int m; while (cin >> m) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { cin >> matrix[i][j]; //输入方阵元素 } } priority_queue<int>myPriorityQueue; //优先队列实现从大到小排列 //求每行元素之和并存储 for (int i = 0; i < m; i++) { int sum = 0; for (int j = 0; j < m; j++) { sum += matrix[i][j]; } myPriorityQueue.push(sum); } //求每列元素之和并存储 for (int j = 0; j < m; j++) { int sum = 0; for (int i = 0; i < m; i++) { sum += matrix[i][j]; } myPriorityQueue.push(sum); } //求主对角线元素之和并存储 int sum = 0; for (int i = 0; i < m; i++) { sum += matrix[i][i]; } myPriorityQueue.push(sum); //求副对角线元素之和并存储 sum = 0; for (int i = 0; i < m; i++) { sum += matrix[i][m - i - 1]; } myPriorityQueue.push(sum); //从大到小输出上述求和结果 while (!myPriorityQueue.empty()) { cout << myPriorityQueue.top() << " "; myPriorityQueue.pop(); } cout << endl; } return 0; }
#include <iostream> #include <algorithm> using namespace std; bool cmp(int a,int b){ return a>b; } int main() { int m,Resize; long long a[13][13],re[100]; while(cin>>m){ int sum; Resize=0; for(int i=0;i<m;i++){ for(int j=0;j<m;j++){ cin>>a[i][j]; } } //统计每一行元素和 for(int i=0;i<m;i++){ sum=0; for(int j=0;j<m;j++){ sum+=a[i][j]; } re[Resize]=sum; Resize++; } //统计每一列元素和 for(int j=0;j<m;j++){ sum=0; for(int i=0;i<m;i++){ sum+=a[i][j]; } re[Resize]=sum; Resize++; } //主对角线 sum=0; for(int i=0;i<m;i++){ sum+=a[i][i]; } re[Resize]=sum; Resize++; //次对角线 sum=0; for(int i=0;i<m;i++){ sum+=a[i][m-i-1]; } re[Resize]=sum; Resize++; //排序 sort(re,re+Resize,cmp); //输出 for(int i=0;i<Resize;i++){ if(i!=0){ cout<<" "; } cout<<re[i]; } cout<<endl; } }
#include <cstdio> #include <algorithm> using namespace std; bool comp(int lhs,int rhs){ return lhs > rhs; } int main(){ int m; while(scanf("%d", &m) != EOF){ int arr[11][11]; for(int i = 0; i < m; ++i){ arr[i][m] = 0;//1.行和 for (int j = 0; j < m; ++j) { scanf("%d", &arr[i][j]); arr[i][m] += arr[i][j]; } } for(int j = 0; j < m; ++j){ arr[m][j] = 0;//2.列和 for(int i = 0; i < m; ++i){ arr[m][j] += arr[i][j]; } } int zhu = 0, fu = 0; for(int i = 0; i < m; ++i){ zhu += arr[i][i]; //3.主对角线和 fu += arr[m-i-1][i]; //4.副对角线和 } int num[2*m+2]; //共2m+2个数要比 //1.输入m个行和 for(int i = 0; i < m; ++i){ num[i] = arr[i][m]; } //2.输入m个列和 for(int i = m; i < 2 * m; ++i){ num[i] = arr[m][i - m]; } num[2 * m] = zhu; //3.输入主对角线和 num[2 * m + 1] = fu; //4.输入副对角线和 sort(num, num+2*m+2,comp); for(int i = 0; i < 2 * m + 2; ++i){ printf("%d ", num[i]); } printf("\n"); } return 0; }
注意有多组数据,每组输出要换行
#include <iostream> #include <algorithm> using namespace std; bool cmp(int a, int b) { return a > b; } int main() { int n; while (cin >> n) { // 注意 while 处理多个 case int a[n][n]; int len = n * 2 + 2; int sum[len]; for (int i = 0; i < len; i++) sum[i] = 0; int k = 0; for (int i = 0 ; i < n; i++) for (int j = 0; j < n; j++) { cin >> a[i][j]; } //每行和 for (int i = 0 ; i < n; i++) { for (int j = 0; j < n; j++) { sum[k] += a[i][j]; } k++; } //每列和 for (int i = 0 ; i < n; i++) { for (int j = 0; j < n; j++) { sum[k] += a[j][i]; } k++; } //主对角线 for (int i = 0 ; i < n; i++) sum[k] += a[i][i]; k++; //副对角线 int i = n - 1; int j = 0; while(i >= 0 && j <= n-1){ sum[k] += a[i][j]; i--; j++; } sort(sum, sum + len, cmp); for (int i = 0; i < len; i++) cout << sum[i] << " "; cout << endl; } } // 64 位输出请用 printf("%lld")
#include<iostream> using namespace std; void quick_sort(int *p,int from,int to){ if(from>=to) return; int l=from; int r=to; int pivot=p[l]; while(l<r){ while(l<r && p[r]<=pivot) r--; p[l]=p[r]; l++; while(l<r && p[l]>pivot) l++; p[r]=p[l]; r--; } p[l]=pivot; quick_sort(p,from,l-1); quick_sort(p,l+1,to); } void swap(int *t,int *s){ int temp = *t; *t = *s; *s=temp; } void bubble_sort(int *p,int len){ bool flag=false; for(int i=1;i<len-1;i++){ flag = false; for(int j=0;j<len-i;j++){ if(p[j]<p[j+1]){ swap(p+j,p+j+1); flag = true; } } if(!flag) return; } } void select_sort(int *p,int len){ for(int i=0;i<len;i++){ int max = i; for(int j=i+1;j<len;j++){ if(p[max]<p[j]) max = j; } int t = p[i]; p[i]=p[max]; p[max]=t; } } void insert_sort(int *p,int len){ int i,j; for(i=1;i<len;i++){ int t=p[i]; for(j=i-1;j>=0;j--){ if(p[j]<t) p[j+1]=p[j]; else break; } p[j+1]=t; } } //建堆 void create_heap(int *p,int index){ //index 代表最后一个元素下标 int parent = index%2==1? index/2:index/2-1; int last = parent; while(parent>=0){ int t=parent; while(t<=last){ int min = 2*parent+1; if(min+1<=index && p[min]>p[min+1]) min++; if(p[t]>p[min]){ swap(p+t,p+min); t=min; } else break; } parent--; } } //调堆 void adjust_heap(int *p,int index) { //index代表需要调到的最后元素下标 int parent = 0; int last = index%2==1? index/2:index/2-1; while(parent<=last){ int min = 2*parent+1; if(min+1<=index && p[min]>p[min+1]) min++; if(p[parent]>p[min]){ swap(p+parent,p+min); parent = min; }else break; } } void heap_sort(int *p,int index){//index代表需要调到的最后元素下标 create_heap(p,index); while(index>0){ swap(p,p+index); index--; adjust_heap(p,index); } } int main(){ int n; int a[10][10]; while(scanf("%d",&n)!=EOF){ int len = 2*n+2; int *p=new int[len]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++) scanf("%d",&a[i][j]); } int index=0; for(int i=0;i<n;i++,index++){ p[index]=0; for(int j=0;j<n;j++) p[index]+=a[i][j]; } for(int j=0;j<n;j++,index++){ p[index]=0; for(int i=0;i<n;i++) p[index]+=a[i][j]; } p[index]=0; for(int i=0;i<n;i++) p[index]+=a[i][i]; p[++index]=0; for(int i=0;i<n;i++) p[index]+=a[i][n-i-1]; //heap_sort(p,len-1); //quick_sort(p,0,len-1); //select_sort(p,len); insert_sort(p,len); // bubble_sort(p,len); for(int i=0;i<len;i++){ if(i!=len-1) cout<<p[i]<<" "; else cout<<p[i]<<endl; } } return 0; }
#include <iostream> (720)#include <vector> using namespace std; //行相加,只需要调入一个vector即可,返回一个int结果 int row_and(const vector<int> src) { int result=0; for(auto a : src) { result = result + a; } return result; } //列相加,先将行列调换后调用row_and函数 vector<int> column(const vector<vector<int>> src) { int row = src.size(); int column = src[0].size(); vector<int> result; vector<vector<int>> s2(column,vector<int>()); for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { s2[j].push_back(src[i][j]); } } for(auto i : s2) { result.push_back(row_and(i)); } return result; } //对角线相加函数,返回一个int结果 int duijiao_and(vector<vector<int>> src) { int result = 0; int row = src.size(); int column = src[0].size(); for(int i = 0 ; i < row ; i++) { for (int j = 0; j < column; j++) { if (i == j) { result = result + src[i][j]; } } } return result; } //副对角线相加 int fuduijiao_and(vector<vector<int>> src) { int result = 0; int row = src.size(); int column = src[0].size(); for(int i = 0 ; i < row ; i++) { for (int j = 0; j < column; j++) { if (i + j == src.size()-1) { result = result + src[i][j]; } } } return result; } //冒泡排序算法 void sort(vector<int>& src) { int tem = 0; for (int i = 0; i < src.size(); i++) { for (int j = i; j < src.size(); j++) { if (src[i] < src[j]) { tem = src[i]; src[i] = src[j]; src[j] = tem; } } } } int main() { int m=0; while (cin >> m) { int n=0; vector<vector<int>> src; vector<int> s; vector<int> result; int re=0; cin >> m; for(int i = 0 ; i < m ; i++) { s.clear(); for(int j = 0 ; j < m ; j++) { cin >> n; s.push_back(n); } src.push_back(s); } for(auto a : src) { result.push_back(row_and(a)); } vector<int> a = column(src); result.insert(result.end(),a.begin(),a.end()); result.insert(result.end(),duijiao_and(src)); result.insert(result.end(),fuduijiao_and(src)); sort(result); for(auto i : result) { cout << i << " " ; } cout << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int m; while(cin>>m) { int dsum=0,fsum=0; int rowsum[10]={0},cowsum[10]={0}; for(int i=0;i<m;++i) { for(int j=0;j<m;++j) { int tmp; cin>>tmp; rowsum[i]+=tmp; cowsum[j]+=tmp; if(i==j) dsum+=tmp; if(i+j==m-1) fsum+=tmp; } } vector<int> v; for(int i=0;i<m;++i) { v.push_back(rowsum[i]); v.push_back(cowsum[i]); } v.push_back(dsum); v.push_back(fsum); sort(v.begin(),v.end()); for(int i=v.size()-1;i>=0;--i) cout<<v[i]<<" "; cout<<endl; } return 0; }
#include <stdio.h> #include <stdlib.h> int main() { int a[10][10]; int *sum; int m; while(scanf("%d",&m)!=EOF) { int len=2*m+2; sum=(int*)malloc(sizeof(int)*(len)); for(int i=0;i<len;i++) { sum[i]=0; } for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { scanf("%d",&a[i][j]); } } for( int i=0;i<m;i++) { for(int j=0;j<m;j++) { sum[i]+=a[i][j]; sum[m+j]+=a[i][j]; if(i==j) { sum[len-2]+=a[i][j]; } if(i+j==m-1) { sum[len-1]+=a[i][j]; } } } int flag=1; for(int i=0;i<len&&flag;i++) { flag=0; for(int j=len-1;j>i;j--) { if(sum[j]>sum[j-1]) { flag=1; int temp=sum[j-1]; sum[j-1]=sum[j]; sum[j]=temp; } } } for(int i=0;i<len;i++) { printf("%d \n",sum[i]); } } return 0; }
#include <cstdio> #include <vector> #include <algorithm> using namespace std; int array[10][10]; int main() { // freopen("/home/dong/CLionProjects/untitled/in.txt", "r", stdin); int m; while (EOF != scanf("%d", &m)) { vector<int> vec; for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { scanf("%d", &array[i][j]); } } for (int l = 0; l < m; ++l) { int sum = 0, sum1 = 0; for (int k = 0; k < m; ++k) { sum += array[k][l]; sum1 += array[l][k]; } vec.push_back(sum); vec.push_back(sum1); } int sum2 = 0, sum3 = 0; for (int i1 = 0; i1 < m; ++i1) { sum2 += array[i1][i1]; sum3 += array[i1][m - 1 - i1]; } vec.push_back(sum2); vec.push_back(sum3); sort(vec.begin(), vec.end()); printf("%d", vec[vec.size() - 1]); for (int n = vec.size() - 2; n >= 0; --n) { printf(" %d", vec[n]); } printf("\n"); } return 0; }