Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<vector>
- #include<iostream>
- #include<algorithm>
- #include<math.h>
- using namespace std;
- bool compare(int prev, int next)
- {
- return prev > next;
- }
- void sort_matrix(int** matrix, int N, int M)
- {
- vector<int> tmp;
- for(int i = 0; i <N; i++)
- for(int j = 0; j<M; j++)
- tmp.push_back(matrix[i][j]);
- sort(tmp.begin(), tmp.end(), compare);
- int s = -1;
- int e = 0;
- for(int i = 1; i <= N; i++)
- {
- e = s+1;
- s = i*M-1;
- for (int j = s; j >= e; j--)
- {
- int x = i-1;
- int y = abs((j % 3)-(M-1));
- matrix[x][y] = tmp[j];
- }
- }
- }
- int main()
- {
- int input[3][3] = { {20,40,80}, {5,60,90}, {45,50,55}};
- int ** matrix = new int*[3];
- for (int y = 0; y <3; y++)
- matrix[y] = new int[3];
- for (int y = 0; y < 3; y++)
- for (int x = 0; x<3; x++)
- matrix[y][x] = input[y][x];
- sort_matrix(matrix, 3, 3);
- for (int y = 0; y < 3; y++)
- {
- for (int x = 0; x<3; x++)
- cout<<matrix[y][x]<<" ";
- cout<<endl;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement