2023-09-20:用go语言,保证一定是n*n的正方形,实现从里到外转圈打印的功能 如果n是奇数,中心点唯一,比如 a b c d e f g h i e是中心点,依次打印 : e f i h g
2023-09-20:用go语言,保证一定是n*n的正方形,实现从里到外转圈打印的功能
如果n是奇数,中心点唯一,比如
a b c
d e f
g h i
e是中心点,依次打印 : e f i h g d a b c
如果n是偶数,中心点为最里层2*2的右下点
比如
a b c d e f
g h i j k l
m n o p q r
s t u v w x
y z 0 1 2 3
4 5 6 7 8 9
最里层是
o p
u v
v是中心点,依次打印 : v u o p q w ....
来自左程云。
答案2023-09-20:
大体步骤如下:
1.定义一个函数print,接收一个二维字节切片m作为参数。
2.获取二维切片m的长度n。
3.设置四个变量a, b, c, d为(n-1)/2, (n-1)/2, n/2, n/2,分别表示每一层的起始点和终止点。
4.使用循环,从最外层到最内层逐层打印。
4.a.在每一层中,调用函数loop打印当前层的内容。
5.在循环结束后,打印换行符。
函数loop的过程如下:
1.判断如果a和c相等,表示只有一个元素,直接打印该元素并返回。
2.对于其他情况,依次打印当前层的四个边。
2.a. 从起始点的下一行开始,按列打印边界元素,即从上到下。
2.b. 从终止点的左侧列开始,按行打印边界元素,即从右到左。
2.c. 从终止点的上一行开始,按列打印边界元素,即从下到上。
2.d. 从起始点的右侧列开始,按行打印边界元素,即从左到右。
在主函数main中,定义了几个测试用例,分别为不同大小的二维字节切片m,然后调用print函数进行打印。
总的时间复杂度为O(n^2),其中n为输入二维切片m的大小。
总的额外空间复杂度为O(1),没有使用额外空间。
go完整代码如下:
package main
import "fmt"
func print(m [][]byte) {
n := len(m)
for a, b, c, d := (n-1)/2, (n-1)/2, n/2, n/2; a >= 0; a, b, c, d = a-1, b-1, c+1, d+1 {
loop(m, a, b, c, d)
}
fmt.Println()
}
func loop(m [][]byte, a, b, c, d int) {
if a == c {
fmt.Printf("%c ", m[a][b])
} else {
for row := a + 1; row <= c; row++ {
fmt.Printf("%c ", m[row][d])
}
for col := d - 1; col >= b; col-- {
fmt.Printf("%c ", m[c][col])
}
for row := c - 1; row >= a; row-- {
fmt.Printf("%c ", m[row][b])
}
for col := b + 1; col <= d; col++ {
fmt.Printf("%c ", m[a][col])
}
}
}
func main() {
map1 := [][]byte{{'a'}}
print(map1)
map2 := [][]byte{{'a', 'b'}, {'c', 'd'}}
print(map2)
map3 := [][]byte{{'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h', 'i'}}
print(map3)
map4 := [][]byte{{'a', 'b', 'c', 'd'}, {'e', 'f', 'g', 'h'}, {'i', 'j', 'k', 'l'}, {'m', 'n', 'o', 'p'}}
print(map4)
map5 := [][]byte{{'a', 'b', 'c', 'd', 'e'}, {'f', 'g', 'h', 'i', 'j'}, {'k', 'l', 'm', 'n', 'o'}, {'p', 'q', 'r', 's', 't'}, {'u', 'v', 'w', 'x', 'y'}}
print(map5)
map6 := [][]byte{{'a', 'b', 'c', 'd', 'e', 'f'}, {'g', 'h', 'i', 'j', 'k', 'l'}, {'m', 'n', 'o', 'p', 'q', 'r'}, {'s', 't', 'u', 'v', 'w', 'x'}, {'y', 'z', '0', '1', '2', '3'}, {'4', '5', '6', '7', '8', '9'}}
print(map6)
}

rust完整代码如下:
fn print(m: &[Vec<char>]) {
let n = m.len() as i32;
let mut a = (n - 1) / 2;
let mut b = (n - 1) / 2;
let mut c = n / 2;
let mut d = n / 2;
while a >= 0 {
loop2(&m, a, b, c, d);
a -= 1;
b -= 1;
c += 1;
d += 1;
}
println!();
}
fn loop2(m: &[Vec<char>], a: i32, b: i32, c: i32, d: i32) {
if a == c {
print!("{} ", m[a as usize][b as usize]);
} else {
for row in a + 1..=c {
print!("{} ", m[row as usize][d as usize]);
}
for col in (b..=d - 1).rev() {
print!("{} ", m[c as usize][col as usize]);
}
for row in (a..=c - 1).rev() {
print!("{} ", m[row as usize][b as usize]);
}
for col in b + 1..=d {
print!("{} ", m[a as usize][col as usize]);
}
}
}
fn main() {
let map1: Vec<Vec<char>> = vec![vec!['a']];
print(&map1);
let map2: Vec<Vec<char>> = vec![vec!['a', 'b'], vec!['c', 'd']];
print(&map2);
let map3: Vec<Vec<char>> = vec![
vec!['a', 'b', 'c'],
vec!['d', 'e', 'f'],
vec!['g', 'h', 'i'],
];
print(&map3);
let map4: Vec<Vec<char>> = vec![
vec!['a', 'b', 'c', 'd'],
vec!['e', 'f', 'g', 'h'],
vec!['i', 'j', 'k', 'l'],
vec!['m', 'n', 'o', 'p'],
];
print(&map4);
let map5: Vec<Vec<char>> = vec![
vec!['a', 'b', 'c', 'd', 'e'],
vec!['f', 'g', 'h', 'i', 'j'],
vec!['k', 'l', 'm', 'n', 'o'],
vec!['p', 'q', 'r', 's', 't'],
vec!['u', 'v', 'w', 'x', 'y'],
];
print(&map5);
let map6: Vec<Vec<char>> = vec![
vec!['a', 'b', 'c', 'd', 'e', 'f'],
vec!['g', 'h', 'i', 'j', 'k', 'l'],
vec!['m', 'n', 'o', 'p', 'q', 'r'],
vec!['s', 't', 'u', 'v', 'w', 'x'],
vec!['y', 'z', '0', '1', '2', '3'],
vec!['4', '5', '6', '7', '8', '9'],
];
print(&map6);
}

c++完整代码如下:
#include<iostream>
#include<vector>
using namespace std;
void loop(vector<vector<char>> m, int a, int b, int c, int d);
void print(vector<vector<char>> m) {
int n = m.size();
for (int a = (n - 1) / 2, b = (n - 1) / 2, c = n / 2, d = n / 2; a >= 0; a--, b--, c++, d++) {
loop(m, a, b, c, d);
}
cout << endl;
}
void loop(vector<vector<char>> m, int a, int b, int c, int d) {
if (a == c) {
cout << m[a][b] << " ";
}
else {
for (int row = a + 1; row <= c; row++) {
cout << m[row][d] << " ";
}
for (int col = d - 1; col >= b; col--) {
cout << m[c][col] << " ";
}
for (int row = c - 1; row >= a; row--) {
cout << m[row][b] << " ";
}
for (int col = b + 1; col <= d; col++) {
cout << m[a][col] << " ";
}
}
}
int main() {
vector<vector<char>> map1 = { {'a'} };
print(map1);
vector<vector<char>> map2 = { {'a', 'b'}, {'c', 'd'} };
print(map2);
vector<vector<char>> map3 = { {'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h', 'i'} };
print(map3);
vector<vector<char>> map4 = { {'a', 'b', 'c', 'd'}, {'e', 'f', 'g', 'h'}, {'i', 'j', 'k', 'l'}, {'m', 'n', 'o', 'p'} };
print(map4);
vector<vector<char>> map5 = { {'a', 'b', 'c', 'd', 'e'},
{'f', 'g', 'h', 'i', 'j'},
{'k', 'l', 'm', 'n', 'o'},
{'p', 'q', 'r', 's', 't'},
{'u', 'v', 'w', 'x', 'y'} };
print(map5);
vector<vector<char>> map6 = { {'a', 'b', 'c', 'd', 'e', 'f'},
{'g', 'h', 'i', 'j', 'k', 'l'},
{'m', 'n', 'o', 'p', 'q', 'r'},
{'s', 't', 'u', 'v', 'w', 'x'},
{'y', 'z', '0', '1', '2', '3'},
{'4', '5', '6', '7', '8', '9'} };
print(map6);
return 0;
}

c完整代码如下:
#include <stdio.h>
void loop(char** m, int a, int b, int c, int d);
void print(char** m, int n) {
for (int a = (n - 1) / 2, b = (n - 1) / 2, c = n / 2, d = n / 2; a >= 0; a--, b--, c++, d++) {
loop(m, a, b, c, d);
}
printf("\n");
}
void loop(char** m, int a, int b, int c, int d) {
if (a == c) {
printf("%c ", m[a][b]);
}
else {
for (int row = a + 1; row <= c; row++) {
printf("%c ", m[row][d]);
}
for (int col = d - 1; col >= b; col--) {
printf("%c ", m[c][col]);
}
for (int row = c - 1; row >= a; row--) {
printf("%c ", m[row][b]);
}
for (int col = b + 1; col <= d; col++) {
printf("%c ", m[a][col]);
}
}
}
int main() {
char* map1[] = { "a" };
int n1 = sizeof(map1) / sizeof(char*);
print(map1, n1);
char* map2[] = { "ab", "cd" };
int n2 = sizeof(map2) / sizeof(char*);
print(map2, n2);
char* map3[] = { "abc", "def", "ghi" };
int n3 = sizeof(map3) / sizeof(char*);
print(map3, n3);
char* map4[] = { "abcd", "efgh", "ijkl", "mnop" };
int n4 = sizeof(map4) / sizeof(char*);
print(map4, n4);
char* map5[] = { "abcde", "fghij", "klmno", "pqrst", "uvwxy" };
int n5 = sizeof(map5) / sizeof(char*);
print(map5, n5);
char* map6[] = { "abcdef", "ghijkl", "mnopqr", "stuvwx", "yz0123", "456789" };
int n6 = sizeof(map6) / sizeof(char*);
print(map6, n6);
return 0;
}

2023-09-20:用go语言,保证一定是n*n的正方形,实现从里到外转圈打印的功能 如果n是奇数,中心点唯一,比如 a b c d e f g h i e是中心点,依次打印 : e f i h g的更多相关文章
- 2021.11.09 P2292 [HNOI2004]L语言(trie树+AC自动机)
2021.11.09 P2292 [HNOI2004]L语言(trie树+AC自动机) https://www.luogu.com.cn/problem/P2292 题意: 标点符号的出现晚于文字的出 ...
- 1034 有理数四则运算 (20 分)C语言
题目描述 本题要求编写程序,计算2个有理数的和.差.积.商. 输入描述: 输入在一行中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整 ...
- 【转】DBMS_STATS.GATHER_TABLE_STATS详解 2012-04-22 09:20:10
[转]DBMS_STATS.GATHER_TABLE_STATS详解 2012-04-22 09:20:10 分类: Linux 由于Oracle的优化器是CBO,所以对象的统计数据对执行计划的生成至 ...
- Python Cookbook(第3版)中文版:15.20 处理C语言中的可迭代对象
15.20 处理C语言中的可迭代对象¶ 问题¶ 你想写C扩展代码处理来自任何可迭代对象如列表.元组.文件或生成器中的元素. 解决方案¶ 下面是一个C扩展函数例子,演示了怎样处理可迭代对象中的元素: s ...
- idea报错:[2016-08-31 09:20:10,763] Artifact xxx:war exploded: Error during artifact deployment.
[2016-08-31 09:20:10,763] Artifact newClassProject1:war exploded: Error during artifact deployment. ...
- C语言保证,0永远不是有效的数据地址,因此,返回址0可用来表示发生的异常事件
C语言保证,0永远不是有效的数据地址,因此,返回址0可用来表示发生的异常事件
- js 对时间进行判断 现在的时间是否在后台给的开始时间 和 结束时间 内 (时间格式为:2018-09-03 09:20:30)
function status(item){ let now = Date.parse(new Date()); let startString = Date.parse(new Date(Date. ...
- afx.h(78): fatal error C1083: 无法打开包括文件: “new.h”: No such file or directory
vs2015新建mfc工程,编译错误: D:\program files (x86)\microsoft visual studio 14.0\vc\atlmfc\include\afx.h(78): ...
- \G 用法:查询结果按列打印
\G 用法:查询结果按列打印 \G 放到sql语句后,可以使每个字段打印到单独的行, 如: mysql \G; mysql> select * from t \G;*************** ...
- D:\Software\Keil5\ARM\PACK\Keil\STM32F1xx_DFP\2.1.0\Device\Include\stm32f10x.h(483): error: #5: cannot open source input file "core_cm3.h": No such file or directory
1. 错误提示信息: D:\Software\Keil5\ARM\PACK\Keil\STM32F1xx_DFP\2.1.0\Device\Include\stm32f10x.h(483): erro ...
随机推荐
- 安装 mysql-community-server报错
错误1. 报错: 所有的匹配结果均已经被参数的模块化过滤条件筛除: mysql-community-server 错误:没有任何匹配: mysql-community-server 解决办法: yum ...
- 语雀崩了,免费送VIP6个月,赶紧薅!!
一.前言 在一个无聊的周一,下午浑浑噩噩的时候,一条公众号信息引起我的关注. 什么东西?语雀这种量级的产品也能崩? 看了一下还真是官方公众号发的!! 心里不由得出现,完蛋整个团队要打包遣散了. 其实小 ...
- c#使用正则表达式匹配提取日期
string target_p ="2021/09/18"; string target_q ="2021-09-18"; 格式yyyy/MM/dd: Matc ...
- springboot整合jpa sqlite
前言 最近有关项目需要用到SQLITE,我先是使用Mybatis去连接SQLITE,然后发现SQLITE对BLOB支持不好,在网上看到相关教程可以写mapper.xml文件,加一个handler解决B ...
- JUC并发编程学习(五)集合类不安全
集合类不安全 List不安全 单线程情况下集合类和很多其他的类都是安全的,因为同一时间只有一个线程在对他们进行修改,但是如果是多线程情况下,那么集合类就不一定是安全的,可能会出现一条线程正在修改的同时 ...
- C/C++ extern “C“ 的问题
声明 文章中的部分代码引用来在: https://blog.csdn.net/u012234115/article/details/43272441 场景 今天在CSDN中看到了一篇关于 extern ...
- STM8 STM32 GPIO 细节配置问题
在MCU的GPIO配置中我们经常需要预置某一 IO 上电后为某一固定电平, 如果恰好我们需要上电后的某IO为高电平, 那么在配置GPIO的流程上面需要特别注意. 配置如下: (以下问题仅在STM8 / ...
- hci0 command 0xfc20 tx timeout(Realtek 8761B Chipset, Bluetooth 5.0)
当前使用的Linux内核版本: 4.4.189 插上USB Bluetooth 5.0 Adapter后,dmesg显示如下log: [ 240.348480] usb 3-1.2: new full ...
- 字节跳动AB实验经验分享:企业如何构建数据驱动的实验文化?
更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 近日,CCF TF 举办了第 123 期分享活动,本期主题为"用户体验工程". CCF TF ...
- Spyder5老是闪退报错 "Blowfish has been deprecated "的解决方法
目录 一.前言 我的运行环境 二.解读报错 三.解决方法 四.打开spyder5 一.前言 本人在学习python时图省事选择直接安装Anaconda3,用spyder5来进行学习,可是比较蛋疼的是安 ...