ITPub博客

首页 > IT职业 > IT图书 > PapaMelon #8 旋转矩阵 [rotate 的使用]

PapaMelon #8 旋转矩阵 [rotate 的使用]

IT图书 作者:tonydeck 时间:2021-09-09 08:52:08 0 删除 编辑

题目链接

  • [旋转矩阵]

题解

  • 经典题目了,选自 [PapaMelon 系统算法课程 - 基础版]。
  • 二维数组看作一维,直接在一维数组上进行旋转,然后转为二维数组输出即可
#include 
#include 
#include 
using namespace std;

const int N = 1e6 + 5;
int n, m, K, a[N];

void solve() {
  cin >> n >> m >> K;
  for (int i = 0; i  n; i++)
    for (int j = 0; j  m; j++)
      cin >> a[i * m + j];
    
  int tot = n * m;
  K %= tot;

  if (K) rotate(a, a + K, a + tot);

  for (int i = 0; i  n; i++) 
    for (int j = 0; j  m; j++) {
      cout  a[i * m + j];
      char c = j == m - 1 ? 'n' : ' ';
      cout  c;
    }
}

int main() {
  int T;
  cin >> T;
  while (T--) solve();
  return 0;
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4615/viewspace-2796960/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-13

  • 博文量
    84
  • 访问量
    960