打印及判断回文数组、打印N阶数组、蛇形矩阵

打印回文数组

1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1

方法1: 对角线对称

左上和右下是对称的。
所以先考虑左上打印, m i n ( i + 1 , j + 1 ) \text min(i+1,j+1) min(i+1,j+1),打印出来:

1 1 1 1
1 2 2 2
1 2 3 3
1 2 3 4

考虑右下打印 m i n ( n − i , n − j ) \text min(n-i,n-j) min(ni,nj),打印出来如下:

4 3 2 1
3 3 2 1
2 2 2 1
1 1 1 1

然后两者重合一下,取最小值, m i n ( 左上,右下 ) \text min(左上,右下) min(左上,右下),代码如下

#include <iostream>
#define endl "\n"
using namespace std;
// 对称思想
int main() {int n;while (cin >> n, n) {for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {cout << min(min(i + 1, j + 1), min(n - i, n - j)) << " ";}cout << endl;}cout << endl;}return 0;
}

vector:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;int main() {int N;while (cin >> N && N != 0) {vector<vector<int>> matrix(N, vector<int>(N));for (int i = 0; i < N; ++i) {for (int j = 0; j < N; ++j) {int min_val = min({i, N - 1 - i, j, N - 1 - j});matrix[i][j] = min_val + 1;}}for (int i = 0; i < N; ++i) {for (int j = 0; j < N; ++j) {if (j != 0) {cout << " ";}cout << matrix[i][j];}cout << endl;}cout << endl;}return 0;
}

方法2 :中心点出发

通过计算矩阵中每个位置 ( i , j ) (i, j) (i,j) 到中心点的距离,来确定该位置的值。奇数的时候,中心点在两个像素之间,所以用浮点数来确定中心点位置。

#include <cmath>
#include <iostream>
#define endl "\n"
using namespace std;int main() {int n;while (cin >> n, n) {for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {if (n % 2)cout << (n + 1) / 2 - max(abs(n / 2 - i), abs(n / 2 - j)) << " ";elsecout << (n + 1) / 2.0 -max(abs((n - 1) / 2.0 - i), abs((n - 1) / 2.0 - j))<< " ";}cout << endl;}cout << endl;}return 0;
}

判断回文数组 / 字符串

方法 1:对称

通过比较数组两端的元素,逐步向中心移动,利用对称性来判断是否为回文数组。如果两端元素相等,则继续向中心移动;否则,返回 false

时间复杂度为 O ( n ) O(n) O(n),其中 n n n 是数组的长度。

#include <iostream>
#include <vector>
using namespace std;bool isPalindrome(const vector<int>& arr) {int left = 0, right = arr.size() - 1;while (left < right) {if (arr[left] != arr[right]) {return false;}left++;right--;}return true;
}int main() {vector<int> arr = {1, 2, 3, 2, 1};if (isPalindrome(arr)) {cout << "yes" << endl;} else {cout << "no" << endl;}return 0;
}

方法 2:反转

将数组反转后与原数组进行比较,如果相等,则为回文数组。

需要额外的空间来存储反转后的数组,空间复杂度为 O ( n ) O(n) O(n)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;bool isPalindromeReverse(const vector<int>& arr) {vector<int> reversed = arr;reverse(reversed.begin(), reversed.end());return arr == reversed;
}int main() {vector<int> arr = {1, 2, 3, 2, 1};if (isPalindromeReverse(arr)) {cout << "yes" << endl;} else {cout << "no" << endl;}return 0;
}

方法 3:字符串

和方法二差不多的意思。

#include <iostream>
#include <vector>
#include <string>
using namespace std;bool isPalindromeString(const vector<int>& arr) {string str;for (int num : arr) {str += to_string(num);}string reversed = str;reverse(reversed.begin(), reversed.end());return str == reversed;
}int main() {vector<int> arr = {1, 2, 3, 2, 1};if (isPalindromeString(arr)) {cout << "yes" << endl;} else {cout << "no" << endl;}return 0;
}

打印N阶数组

数组长这个样子:

1 2 3 4 5
2 1 2 3 4
3 2 1 2 3
4 3 2 1 2
5 4 3 2 1

方法1:对角线

打印最外围,里面的每个元素 a [ i ] [ j ] = a [ i − 1 ] [ j − 1 ] a[i][j] = a[i-1][j-1] a[i][j]=a[i1][j1],可以自己画个图理解一下。

#include <iostream>
#define endl "\n"
using namespace std;
const int N = 10010;
int a[N][N];int main() {int n;while (cin >> n, n) {// 打印最外围的12345(行和列)for (int i = 0; i < n; i++) {a[i][0] = i + 1;a[0][i] = i + 1;}for (int i = 1; i < n; i++) {for (int j = 1; j < n; j++) {a[i][j] = a[i - 1][j - 1];}}for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {cout << a[i][j] << " ";}cout << endl;}cout << endl;}return 0;
}

方法2:规律

#include <algorithm>
#include <iostream>
#define endl "\n"
using namespace std;
int n;
int main() {while (cin >> n) {for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {cout << abs(i - j) + 1 << " ";}cout << endl;}if (n) cout << endl;}return 0;
}

打印蛇形矩阵

方法1:填充

#include <iostream>
#include <vector>
using namespace std;void print(int m, int n) {vector<vector<int>> matrix(m, vector<int>(n, 0));int num = 1;int top = 0, bottom = m - 1, left = 0, right = n - 1;while (num <= m * n) {// 左右for (int i = left; i <= right && num <= m * n; ++i) {matrix[top][i] = num++;}top++;// 上下for (int i = top; i <= bottom && num <= m * n; ++i) {matrix[i][right] = num++;}right--;// 右左if (top <= bottom) {for (int i = right; i >= left && num <= m * n; --i) {matrix[bottom][i] = num++;}bottom--;}// 下上if (left <= right) {for (int i = bottom; i >= top && num <= m * n; --i) {matrix[i][left] = num++;}left++;}}// 打印矩阵for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {cout << matrix[i][j] << " ";}cout << endl;}
}int main() {int m, n;cin >> m >> n;print(m, n);return 0;
}

方法 2 :偏移量

#include <iostream>
using namespace std;
int res[10010][10010];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};int main() {int n, m;cin >> n >> m;int x = 0, y = 0, d = 1;for (int i = 1; i <= n * m; i++) {res[x][y] = i;int a = x + dx[d];int b = y + dy[d];if (a < 0 || a >= n || b < 0 || b >= m || res[a][b]) {d = (d + 1) % 4;  // 1是右 2是下 3是上 4是左a = x + dx[d], b = y + dy[d];}x = a, y = b;}for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {cout << res[i][j] << " ";}cout << endl;}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/80909.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

详解UnityWebRequest类

什么是UnityWebRequest类 UnityWebRequest 是 Unity 引擎中用于处理网络请求的一个强大类&#xff0c;它可以让你在 Unity 项目里方便地与网络资源进行交互&#xff0c;像发送 HTTP 请求、下载文件等操作都能实现。下面会详细介绍 UnityWebRequest 的相关内容。 UnityWebRequ…

UE5 在旋转A的基础上执行旋转B

用径向slider实现模型旋转时&#xff0c;得到的结果与ue编辑器里面的结果有很大出入。 问题应该是 两个FRotator&#xff08;0&#xff0c;10&#xff0c;0&#xff09;和&#xff08;10&#xff0c;20&#xff0c;30&#xff09;&#xff0c; 两个FRotator的加法结果为&…

4.2 Prompt工程与任务建模:高效提示词设计与任务拆解方法

提示词工程&#xff08;Prompt Engineering&#xff09;和任务建模&#xff08;Task Modeling&#xff09;已成为构建高效智能代理&#xff08;Agent&#xff09;系统的核心技术。提示词工程通过精心设计的自然语言提示词&#xff08;Prompts&#xff09;&#xff0c;引导大型语…

MySQL 索引的最左前缀匹配原则是什么?

MySQL 索引的最左前缀匹配原则详解 最左前缀匹配原则&#xff08;Leftmost Prefix Principle&#xff09;是 MySQL 复合索引&#xff08;联合索引&#xff09;查询优化中的核心规则&#xff0c;理解这一原则对于高效使用索引至关重要。 核心概念 定义&#xff1a;当查询条件…

SQL命令

一、控制台中查询命令 默认端口号&#xff1a;3306 查看服务器版本: mysql –version 启动MySQL服务&#xff1a;net start mysql 登录数据库&#xff1a;mysql -u root -p 查看当前系统下的数据库&#xff1a;show databases&#xff1b; 创建数据库&#xff1a;create…

新增 29 个专业,科技成为关键赛道!

近日&#xff0c;教育部正式发布《普通高等学校本科专业目录&#xff08;2025年&#xff09;》&#xff0c;新增 29 个本科专业&#xff0c;包括区域国别学、碳中和科学与工程、海洋科学与技术、健康与医疗保障、智能分子工程、医疗器械与装备工程、时空信息工程、国际邮轮管理…

零基础上手Python数据分析 (23):NumPy 数值计算基础 - 数据分析的加速“引擎”

写在前面 —— 超越原生 Python 列表,解锁高性能数值计算,深入理解 Pandas 的底层依赖 在前面一系列关于 Pandas 的学习中,我们已经领略了其在数据处理和分析方面的强大威力。我们学会了使用 DataFrame 和 Series 来高效地操作表格数据。但是,你是否好奇,Pandas 为何能够…

Android 13.0 MTK Camera2 设置默认拍照尺寸功能实现

Android 13.0 MTK Camera2 设置默认拍照尺寸功能实现 文章目录 需求&#xff1a;参考资料架构图了解Camera相关专栏零散知识了解部分相机源码参考&#xff0c;学习API使用&#xff0c;梳理流程&#xff0c;偏应用层Camera2 系统相关 修改文件-修改方案修改文件&#xff1a;修改…

HarmonyOS 框架基础知识

参考文档&#xff1a;HarmonyOS开发者文档 第三方库&#xff1a;OpenHarmony三方库中心仓 基础特性 Entry&#xff1a;关键装饰器 Components&#xff1a;组件 特性EntryComponent​​作用范围仅用于页面入口可定义任意可复用组件​​数量限制​​每个页面有且仅有一个无数量…

前端分页与瀑布流最佳实践笔记 - React Antd 版

前端分页与瀑布流最佳实践笔记 - React Antd 版 1. 分页与瀑布流对比 分页&#xff08;Pagination&#xff09;瀑布流&#xff08;Infinite Scroll&#xff09;展示方式按页分批加载&#xff0c;有明确页码控件滚动到底部时自动加载更多内容&#xff0c;无明显分页用户控制用…

Linux网络编程:TCP多进程/多线程并发服务器详解

Linux网络编程&#xff1a;TCP多进程/多线程并发服务器详解 TCP并发服务器概述 在Linux网络编程中&#xff0c;TCP服务器主要有三种并发模型&#xff1a; 多进程模型&#xff1a;为每个客户端连接创建新进程多线程模型&#xff1a;为每个客户端连接创建新线程I/O多路复用&am…

详解springcloudalibaba采用prometheus+grafana实现服务监控

文章目录 1.官网下载安装 prometheus和grafana1.promethus2.grafana 2. 搭建springcloudalibaba集成prometheus、grafana1. 引入依赖,springboot3.2之后引入如下2. 在yml文件配置监控端点暴露配置3. 在当前启动的应用代码中添加&#xff0c;在prometheus显示的时候附加当前应用…

数据分析1

一、常用数据处理模块Numpy Numpy常用于高性能计算&#xff0c;在机器学习常常作为传递数据的容器。提供了两种基本对象&#xff1a;ndarray、ufunc。 ndarray具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组。 ufunc提供了对数组快速运算的标准数学函数。 ndar…

DeepSeek智能时空数据分析(六):大模型NL2SQL绘制城市之间连线

序言&#xff1a;时空数据分析很有用&#xff0c;但是GIS/时空数据库技术门槛太高 时空数据分析在优化业务运营中至关重要&#xff0c;然而&#xff0c;三大挑战仍制约其发展&#xff1a;技术门槛高&#xff0c;需融合GIS理论、SQL开发与时空数据库等多领域知识&#xff1b;空…

2023ICPC合肥题解

文章目录 F. Colorful Balloons(签到)E. Matrix Distances(思维小结论)J. Takeout Delivering(最短路)G. Streak Manipulation(二分dp)C. Cyclic Substrings(回文自动机) 题目链接 F. Colorful Balloons(签到) int n;cin>>n;for(int i1;i<n;i) cin>>s[i];map<…

数字技术驱动下教育生态重构:从信息化整合到数字化转型的路径探究

一、引言 &#xff08;一&#xff09;研究背景与问题提出 在当今时代&#xff0c;数字技术正以前所未有的速度和深度渗透到社会的各个领域&#xff0c;教育领域也不例外。从早期的教育信息化整合到如今的数字化转型&#xff0c;教育系统正经历着一场深刻的范式变革。 回顾教…

terraform 动态块(Dynamic Blocks)详解与实践

在 Terraform 中&#xff0c;动态块&#xff08;Dynamic Blocks&#xff09; 是一种强大的机制&#xff0c;允许你根据变量或表达式动态生成配置块&#xff0c;避免重复编写相似的代码。这在处理需要重复定义的结构&#xff08;如资源参数、嵌套配置&#xff09;时特别有用。以…

Unity3D引擎框架及用户接口调用方式相关分析及汇总

分析目的 目前外网3D手游绝大部基于Unity3D引擎进行开发,Unity3D引擎属于商业引擎,引擎整理框架的运行机制较为神秘,本文介绍Unity引擎框架、对象组织方式、用户接口与引擎交互方式等原理,通过本文的分析和介绍可了解Unity3D框架中大致执行原理。 实现原理 Unity引擎作为…

react-09React生命周期

1.react生命周期&#xff08;旧版&#xff09; 1.1react初始挂载时的生命周期 1:构造器-constructor // 构造器constructor(props) {console.log(1:构造器-constructor);super(props)// 初始化状态this.state {count: 0}} 2:组件将要挂载-componentWillMount // 组件将要挂载…

【NVM】管理不同版本的node.js

目录 一、下载nvm 二、安装nvm 三、验证安装 四、配置下载镜像 五、使用NVM 前言&#xff1a;不同的node.js版本会让你在使用过程很费劲&#xff0c;nvm是一个node版本管理工具&#xff0c;通过它可以安装多种node版本并且可以快速、简单的切换node版本。 一、下载nvm htt…