简体   繁体   English

随机逻辑以以下模式打印数字

[英]random logic to print numbers in following pattern

Currently, I have an array r[1] to r[12] . 目前,我有一个数组r[1]r[12]

Now, I need to print it like this: 现在,我需要这样打印:

r[10]    r[11]    r[12]
r[7]     r[8]     r[9]
r[4]     r[5]     r[6]
r[1]     r[2]     r[3]
for (int i = 0; i < 4; i++){
   for(int j = 0; j < 3; j++){
       print("%d", r[ 12 - (i * 3) + j]);
   }
}

If you need any other numbers than you could generalize the solution 如果您需要除此以外的任何其他数字,则可以对解决方案进行概括

for (int i = 0; i < count / interval; i++){
   for(int j = 0; j < interval; j++){
       print("%d", r[ count - (i * interval) + j]);
   }

supposing you've got an array with n elements and you want to show them in row of 3 elements, you could proceed in this way (in php): 假设您有一个包含n个元素的数组,并且想在3个元素的行中显示它们,则可以以这种方式进行操作(在php中):

// this is your array, it starts with 1 and could end with n (but it must be divisible by 3)
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);

// check if the number of the elements is ok for the script
if (count($array) % 3) {
    echo "The array must be divisible for the number of the element in a row!";
    exit;
}

// for each row, from 1 to number of the elements in array...
for ($row = 1; $row <= count($array) / 3; $row++){

   // ... and for each column (with a max of 3 columns)
   for($column = 0; $column < 3; $column++){

       // print out the last element, less current row multipled by 3 plus the number of the column (which was started with 1).
       echo $array[count($array) - ($row * 3) + $column] . " ";

   }

   // another row is finished, move the cursor to the next line
   echo "<br />";

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM