fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. void rotateSectors(vector<vector<int>>& matrix, int n) {
  7. vector<vector<int>> result(n, vector<int>(n));
  8.  
  9. for (int i = 0; i < n; ++i) {
  10. for (int j = 0; j < n; ++j) {
  11. if (i < n / 2 && j < n / 2) {
  12. // Top-left sector to top-right
  13. result[i][j + n / 2 + n % 2] = matrix[i][j];
  14. } else if (i < n / 2 && j >= n / 2 + n % 2) {
  15. // Top-right sector to bottom-right
  16. result[i + n / 2 + n % 2][j] = matrix[i][j];
  17. } else if (i >= n / 2 + n % 2 && j >= n / 2 + n % 2) {
  18. // Bottom-right sector to bottom-left
  19. result[i][j - n / 2 - n % 2] = matrix[i][j];
  20. } else if (i >= n / 2 + n % 2 && j < n / 2) {
  21. // Bottom-left sector to top-left
  22. result[i - n / 2 - n % 2][j] = matrix[i][j];
  23. }
  24. }
  25. }
  26.  
  27. // Print the result matrix
  28. for (const auto& row : result) {
  29. for (int elem : row) {
  30. cout << elem << " ";
  31. }
  32. cout << endl;
  33. }
  34. }
  35.  
  36. int main() {
  37. int n;
  38. cin >> n;
  39.  
  40. vector<vector<int>> matrix(n, vector<int>(n));
  41. for (int i = 0; i < n; ++i) {
  42. for (int j = 0; j < n; ++j) {
  43. cin >> matrix[i][j];
  44. }
  45. }
  46.  
  47. rotateSectors(matrix, n);
  48.  
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0s 5280KB
stdin
3
1 2 3
4 5 6
7 8 9
stdout
7 0 1 
0 0 0 
9 0 3