打印及判斷回文數組、打印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(n?i,n?j),打印出來如下:

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[i?1][j?1],可以自己畫個圖理解一下。

#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.pswp.cn/diannao/80909.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/80909.shtml
英文地址,請注明出處:http://en.pswp.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…