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

螺旋矩阵、旋转矩阵、矩阵Z字打印

螺旋矩阵

在这里插入图片描述

#include <iostream>
#include <vector>
void display(std::vector<std::vector<int>>&nums){for(int i = 0; i < nums.size(); ++i){for(int j = 0; j < nums[0].size(); ++j){std::cout<<nums[i][j]<<' ';}std::cout<<std::endl;}
}
void Process(int x1, int y1, int x2, int y2, std::vector<std::vector<int>>&nums){if(x1 == x2){while (y1 <= y2){std::cout<<nums[x1][y1++]<<' ';}    } else if(y1 == y2){while (x1 <= x2){std::cout<<nums[x1++][y1]<<' ';}    }else{for(int i = y1; i <= y2; ++i){std::cout<<nums[x1][i]<<' ';}for(int i = x1 + 1; i <= x2; ++i){std::cout<<nums[i][y2]<<' ';}for(int i = y2 - 1; i >= y1; --i){std::cout<<nums[x2][i]<<' ';}for(int i = x2 - 1; i > x1; --i){std::cout<<nums[i][y1]<<' ';}}
}
int main(){std::vector<std::vector<int>>nums;int n = 6;int m = 6;nums.resize(n);int num = 1;for(int i = 0; i < n;  ++i){std::vector<int>tmp(m, 0);for(int j = 0; j < m; ++j){tmp[j] = num++;}nums[i] = tmp;}display(nums);// 矩阵类型的题目可以枚举一些特殊的点来做左上角点和右下角点, 然后在往内部收缩int x1 = 0, y1 = 0, x2 = n-1, y2 = m-1;while (x1 <= x2){Process(x1, y1, x2, y2, nums);++x1;++y1;--x2;--y2;}std::getchar();}

旋转矩阵

类似于这个螺旋矩阵我们也是在每次处理最外层的矩形,然后往内收缩。
对于一个矩形我们选取四个点依次进行交换即可

#include <iostream>
#include <vector>
void display(std::vector<std::vector<int>>&nums){for(int i = 0; i < nums.size(); ++i){for(int j = 0; j < nums[0].size(); ++j){std::cout<<nums[i][j]<<' ';}std::cout<<std::endl;}
}
void Process(int x1, int y1, int x2, int y2, std::vector<std::vector<int>>&nums){if(x1 == x2){while (y1 <= y2){std::cout<<nums[x1][y1++]<<' ';}    } else if(y1 == y2){while (x1 <= x2){std::cout<<nums[x1++][y1]<<' ';}    }else{for(int i = y1; i <= y2; ++i){std::cout<<nums[x1][i]<<' ';}for(int i = x1 + 1; i <= x2; ++i){std::cout<<nums[i][y2]<<' ';}for(int i = y2 - 1; i >= y1; --i){std::cout<<nums[x2][i]<<' ';}for(int i = x2 - 1; i > x1; --i){std::cout<<nums[i][y1]<<' ';}}
}
int main(){std::vector<std::vector<int>>nums;int n = 4;int m = 4;nums.resize(n);int num = 1;for(int i = 0; i < n;  ++i){std::vector<int>tmp(m, 0);for(int j = 0; j < m; ++j){tmp[j] = num++;}nums[i] = tmp;}display(nums);// 枚举矩阵的四个点把最外圈的给调整好再往内圈收缩int x1, y1, x2, y2, x3, y3, x4, y4;int count = 0;while(count < n -2){x1 = count, y1 = count;x2 = count, y2 = n - 1 - count;x3 = n - 1 - count, y3 = count;x4 = n - 1 - count, y4 = n - 1 - count;while(y1 < y2 && x2 < x4 && y4 > y3 && x3 > x1){std::swap(nums[x1][y1], nums[x2][y2]);std::swap(nums[x1][y1], nums[x4][y4]);std::swap(nums[x1][y1], nums[x3][y3]);++y1;++x2;--y4;--x3;}++count;}display(nums);std::getchar();}

矩阵Z字打印

也是和螺旋矩阵类似选取两个点进行循环

#include <iostream>
#include <vector>
void display(int x1, int y1, int x2, int y2, std::vector<std::string>&strs){if(x1 < x2){while ( x1 <= x2 ){std::cout<<strs[x1++][y1--]<<' ';}std::cout<<std::endl;      } else{while ( x1 >= x2  ){std::cout<<strs[x1--][y1++]<<' ';}std::cout<<std::endl;          }
}
int main(){std::vector<std::string>strs = {"abcd", "efgh", "jklh"};int n = strs.size();int m = strs[0].size();int x1 = 0, y1 = 0;int x2 = 0, y2 = 0;int num = 0;while(x1 < n){if(num % 2 == 0){display(x1, y1, x2, y2, strs);} else{display(x2, y2, x1, y1, strs);}if(y1 < m - 1){y1++;}else{x1++;}if(x2 < n - 1){x2++;}else{y2++;}num++;} std::getchar();}

矩阵打印的题目可以从选取若干个点然后处理一层之后再往内进行收缩的做法先进行考虑

http://www.lryc.cn/news/153078.html

相关文章:

  • Seaborn绘制热力图的子图
  • C++二级题目4
  • Tomcat 部署时 war 和 war exploded区别
  • Delphi IdTcpServer IdTcpClient 传输简单文本
  • 界面控件Telerik UI for WPF——Windows 11主题精简模式提升应用体验
  • PoseC3D 基于人体姿态的动作识别新范式
  • html2canvas 截图空白 或出现toDataURL‘ on ‘HTMLCanvasElement或img标签没截下来 的所有解决办法
  • Eclipse错误提示: Symbol ‘xxxx‘ could not be resolved
  • 基于Java的OA办公管理系统,Spring Boot框架,vue技术,mysql数据库,前台+后台,完美运行,有一万一千字论文。
  • 正则表达式(JAVA)
  • 264_BOOST中的Json库解析_BOOST_AUTO(itrpromodel, doc.FindMember(“productmodel“));
  • linux rpm 离线安装 nginx 自用,仅供参考
  • 第十二章 YOLO的部署实战篇(上篇)
  • 无涯教程-Android - List View函数
  • stable diffusion实践操作-重绘
  • C# 静态构造函数未执行 .net core框架
  • Java 复习笔记 - 学生管理系统篇
  • 【UIPickerView-UIDatePicker-应用程序对象 Objective-C语言】
  • 仿京东 项目笔记1
  • huggingface transformers库中LlamaForCausalLM
  • 04-过滤器和拦截器有什么区别?【Java面试题总结】
  • 如何用selenium或pyppeteer来启动多个AdsPower窗口
  • 京东店铺所有商品API接口数据
  • stm32之27.iic协议oled显示
  • paddle 1-高级
  • ChatGPT帮助高职院校学生实现个性化自适应学习与对话式学习
  • 如何通过python写接口自动化脚本对一个需要调用第三方支付的报名流程进行测试?
  • 将OSGB格式数据转换为3d tiles的格式
  • 【易售小程序项目】小程序首页完善(滑到底部数据翻页、回到顶端、基于回溯算法的两列数据高宽比平衡)【后端基于若依管理系统开发】
  • 素数求原根