在某些规则下动态创建数组 [英] Dynamically create arrays under certain rules

查看:88
本文介绍了在某些规则下动态创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建具有遵循这些模式的某些值/属性的数组。

I need to create arrays with certain values/properties that follow these patterns.

不幸的是,我的数学知识无法让我找到模式。

Unfortunately, my mathematics knowledge doesn't let me find the pattern.

下面是一个数组的示例,该数组应在(自下而上)n = 1、2和3(沿每个边的红色框计数)处输出。

Here is an example of the array that should be output at (bottom to top) n = 1, 2 and 3 (counting the red boxes along each edge).

所有红色和绿色方块应分配一些值,而所有

All red and green squares should have assigned some value, while all white ones need to be undefined (or empty, null, something).

创建数组时,如何动态分配它们各自的红色/绿色/白色值?

Creating the array, how do I dynamically assign them their respective red/green/white values?

    for (var x = 0; x < n * 4 - 1; x++)
    {
        for (var y = 0; y < n * 4 - 1; y++)
        {
            board[x][y] = green, red or white?;
        }
    }

PS:如果不明显,该模式创建边长为 n 的六边形。

PS: If it isn't obvious, The pattern creates hexagons with a "red" edge length of n.

推荐答案

每个六边形都由三部分组成(从上到下):

Each of your hexagons is composed of three parts (from top to bottom):


  1. 上半部分以绿线覆盖整个宽度

  1. an upper half terminated by a green line covering the entire width

一条红色方格线

下半部分以a绿线覆盖整个宽度

a lower half starting with a green line covering the entire width

上半部分和下半部分是它们自身的镜像,在2的线上反映出来

The upper and lower halves are mirror images of themselves, reflected over the line in 2.

对于给定的 n ,总行数为 2 n-1 + 2 n 2 n-1 项是由于红线造成的:其中有 2 n 条,但是其中有两条(最长的一条)彼此重叠。 2 n 项归因于绿线(它是红线的数量+ 1)。

For a given n, the total number of lines is 2 n - 1 + 2 n. The 2 n - 1 term is due to the red lines: there are 2 n of them, but two of them (the longest ones) are one on top of each other. The 2 n term is due to the green lines (it is the number of red lines + 1).

下面是Python代码绘制的上半部分(开头的项目1):

Here is Python code drawing the top half (item 1 at the beginning):

def print_hex_upper_half(n):
    num_lines = 2 * (n - 1) + 1
    width = (2 * n - 1) + 2 * n
    for i in range(1, num_lines + 1):
        if i % 2 == 1:
            s = n - i / 2 - 1
            print ' ' * s + 'G' * (width - 2 * s) + ' ' * s
        else:
            s = n - i / 2 + 1
            rw = (width - 2 * s + 1) / 2
            print ' ' * s + ' '.join('R' * rw) + ' ' * s

这是运行时的位置:

>>> print_hex_upper_half(4)
   GGGGGGGGG   
    R R R R    
  GGGGGGGGGGG  
   R R R R R   
 GGGGGGGGGGGGG 
  R R R R R R  
GGGGGGGGGGGGGGG

>>> print_hex_upper_half(3)
  GGGGGGG  
   R R R   
 GGGGGGGGG 
  R R R R  
GGGGGGGGGGG

>>> print_hex_upper_half(2)
 GGGGG 
  R R  
GGGGGGG

>>> print_hex_upper_half(1)
GGG

中间一行很容易,而下半部分是

The middle line is easy, and the bottom half is a reflection, so it is just a matter of manipulating the indices.

这里是代码的详细说明:

Here is a detailed explanation of the code:


  • 由于上述原因,上半部分的行数为 2(n-1)+ 1 ,宽度如上所示。

奇数行为绿色,偶数行为红色。

Odd rows are green, and even rows are red.

由于上述原因,绿色行以 n-i / 2-1 空格开头和结尾。其余部分为绿色。

For the same reasons given above, a green row starts and ends with n - i / 2 - 1 spaces. The rest is green.

同样,出于上述相同的原因,红色行以 i / 2 + 1 空格。其余部分为红色,中间有空格。如果从宽度减去该数量,再加1,然后除以2,就可以得到红色块的数量。

Again, for the same reasons given above, a red row starts and ends with i / 2 + 1 spaces. The rest is red interspersed with spaces. If we subtract from the width this amount, add 1, and divide by 2, we get the number of red blocks.

这篇关于在某些规则下动态创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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