当前位置: 首页 > news >正文

第十六届蓝桥杯青少组C++省赛[2025.8.9]第二部分编程题(4、矩阵圈层交错旋转)

参考程序:

#include <bits/stdc++.h>
using namespace std;const int MAXN = 105;
int a[MAXN][MAXN];int main() {int n;if (!(cin >> n)) return 0;for (int i = 0; i < n; ++i)for (int j = 0; j < n; ++j)cin >> a[i][j];int layers = n / 2; // 每次处理一圈,外层到里层共有 floor(n/2) 层// 对每一层做 (right-left) 次四元循环替换for (int layer = 0; layer < layers; ++layer) {int top = layer;int left = layer;int bottom = n - 1 - layer;int right = n - 1 - layer;// 对当前圈上的每一个偏移 i 做一次 4 格循环替换for (int i = 0; i < right - left; ++i) {int rTop = top,        cTop = left + i;int rRight = top + i,  cRight = right;int rBottom = bottom,  cBottom = right - i;int rLeft = bottom - i, cLeft = left;if (layer % 2 == 0) {// 顺时针 90° (CW):左 -> 上, 下 -> 左, 右 -> 下, 上 -> 右int tmp = a[rLeft][cLeft];a[rLeft][cLeft]     = a[rBottom][cBottom];a[rBottom][cBottom] = a[rRight][cRight];a[rRight][cRight]   = a[rTop][cTop];a[rTop][cTop]       = tmp;} else {// 逆时针 90° (CCW):上 -> 左, 右 -> 上, 下 -> 右, 左 -> 下int tmp = a[rTop][cTop];a[rTop][cTop]       = a[rRight][cRight];a[rRight][cRight]   = a[rBottom][cBottom];a[rBottom][cBottom] = a[rLeft][cLeft];a[rLeft][cLeft]     = tmp;}}}// 输出结果for (int i = 0; i < n; ++i) {for (int j = 0; j < n; ++j) {if (j) cout << ' ';cout << a[i][j];}cout << '\n';}return 0;
}

http://www.dtcms.com/a/332753.html

相关文章:

  • Docker 实战:情感分析系统-容器化部署全流程(sa-logic、sa-webapp、sa-frontend )
  • 48Days-Day19 | ISBN号,kotori和迷宫,矩阵最长递增路径
  • 10.0 UML的介绍以及VisualStudio中查看类图
  • whisper 语种检测学习笔记
  • 迅为RK3588S开发板usb连接adb方式
  • USB ADB 简介
  • 机器学习 - Kaggle项目实践(4)Toxic Comment Classification Challenge 垃圾评论分类问题
  • python爬虫(四)----requests
  • 组合模式及优化
  • 新手向:Python循环结构(for/while)及控制语句(break/continue)
  • 中国象棋人机对战
  • 使用转换函数重载布尔值类
  • 申请第二个域名还要备案吗
  • 《软件工程导论》实验报告四 详细设计工具
  • 两幅美国国旗版权挂钩专利发起跨境诉讼
  • 云原生俱乐部-杂谈2
  • 机器学习之PCA降维
  • uniapp 开发微信小程序,获取经纬度并且转化详细地址(单独封装版本)
  • week1-[顺序结构]跑道
  • IStoreOS(OpenWrt)开启IPV6
  • 设备数据采集服务器软件TOP Server OPC Server详细介绍
  • wsl安装完美教程
  • Vulnhub Deathnote靶机复现攻略
  • 告别手动优化!React Compiler 自动记忆化技术深度解析
  • 16进制pcm数据转py波形脚本
  • Vim 常用快捷键及插件
  • 关于simplifyweibo_4_moods数据集的分类问题
  • 大白话解析“入口点合约”
  • Linux系统--库制作与原理
  • Java—注解