图案由数字中顺时针方向绕的矩形移动(长度和宽度各时间减少)的 [英] Pattern consisting of numbers moving in clockwise direction around a rectangular shape (length and breadth decreasing each time)

查看:155
本文介绍了图案由数字中顺时针方向绕的矩形移动(长度和宽度各时间减少)的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了code对于很多模式,但不能写这个.....甚至没有得到任何提示如何继续。

I have written code for many patterns but unable to write for this..... not even getting any hint how to proceed.

我要生成的输出如下:

1  2  3  4  5 

16 17 18 19 6

15 24 25 20 7

14 23 22 21 8

13 12 11 10 9

...其中的矩形的宽度和高度被指定为输入。

...where the width and height of the rectangle are specified as inputs.

推荐答案

我会做这种方式:

初​​始化一个5x5的整数的二维数组为0有一个方向变量,定义常量或枚举的四个方向。从开始移动正确(0,0),填充值增加数组中,直到你击中边或一个数字,是不为0之后,增加的方向(,敷),然后继续。然后打印成排的阵列。

Initialize a 5x5 two-dimensional array of integers to 0. Have a direction variable, and define constants or an enum for the four directions. Start moving 'right' from (0, 0), filling in the array with increasing values until you hit the edge or a number that is not 0. Then, increment the direction (and wrap) and continue. Then print the array in rows.

使用循环的替代方法是通过迭代所有的(X,Y)坐标,并通过x和y为,让你在该位置的值的函数。我写的函数也能做同样的事情会填充该数组,但它不会写入阵列的功能,当它达到给定(X,Y)返回当前值。不是很有效,但它实现的结果。

An alternative using loops is to iterate over all the (x, y) coordinates, and pass x and y into a function that gives you the value at that position. The function I wrote does exactly the same thing as would the function that fills the array, except it doesn't write to an array, and when it reaches the given (x, y) it returns the current value. Not very efficient, but it achieves the result.

这篇关于图案由数字中顺时针方向绕的矩形移动(长度和宽度各时间减少)的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆