以spiralshape打印数字

编程入门 行业动态 更新时间:2024-10-26 23:33:39
本文介绍了以spiralshape打印数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

编写一个程序,将数字N作为输入 然后它必须以1到N的螺旋形状打印 Ex:输入9 1 2 3 8 9 4 7 6 5

Write a program to take a number N as an input and then it has to print from 1 to N in a spiral shape Ex: input 9 1 2 3 8 9 4 7 6 5

推荐答案

这应该解释算法 This should explain the algorithm // function to print the top-right peel of the matrix and // recursively call the print bottom-left on the submatrix. void printTopRight(int a[][COL], int x1, int y1, int x2, int y2) { int i = 0, j = 0; // print values in the row. for(i = x1; i<=x2; i++) { printf("%d ", a[y1][i]); } // print values in the column. for(j = y1 + 1; j <= y2; j++) { printf("%d ", a[j][x2]); } // see if more layers need to be printed. if(x2-x1 > 0) { // if yes recursively call the function to // print the bottom left of the sub matrix. printBottomLeft(a, x1, y1 + 1, x2-1, y2); } } // function to print the bottom-left peel of the matrix and // recursively call the print top-right on the submatrix. void printBottomLeft(int a[][COL], int x1, int y1, int x2, int y2) { int i = 0, j = 0; // print the values in the row in reverse order. for(i = x2; i>=x1; i--) { printf("%d ", a[y2][i]); } // print the values in the col in reverse order. for(j = y2 - 1; j >= y1; j--) { printf("%d ", a[j][x1]); } // see if more layers need to be printed. if(x2-x1 > 0) { // if yes recursively call the function to // print the top right of the sub matrix. printTopRight(a, x1+1, y1, x2, y2-1); } } void printSpiral(int arr[][COL]) { printTopRight(arr,0,0,COL-1,ROW-1); printf("\n"); }

更多推荐

以spiralshape打印数字

本文发布于:2023-08-07 06:57:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317618.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   spiralshape

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!