PHP 螺旋,数字 0 到 9

发布于 2024-11-19 08:05:35 字数 455 浏览 4 评论 0原文

我正在寻找可以为我编写一个脚本来输出以下模式的人:

http:// /img84.imageshack.us/img84/3038/82351644.png

它从中心的 0 开始,然后是 1 到向左,向下 2,向右 3...你明白了。 - 它总是从 0 到 9 并重新开始...

我找到了这个 主题 ,但它与我的要求明显不同。 因为我对 php 不太了解,而“优点”就在这里, 我善意地询问是否有人愿意花一些时间为我做这件事。 那太好了!另外,如果我可以在变量中设置脚本执行的“轮数”,那就太棒了! - 多谢

I am searching for someone who could write me a script to output the following pattern:

http://img84.imageshack.us/img84/3038/82351644.png

its starting with 0 in the center, then 1 to the left, 2 down, 3 right...you get the idea. - its always 0 to 9 and starting over again...

i found this topic , but it is clearly different from my request.
since i have no good understanding of php and the "pros" are here,
i am asking nicely if someone would spend some time doing this for me.
That would be great! Also if I could set in a variable how many "rounds" the script is performing, that would be awesome! - Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-11-26 08:05:35

只是因为我没有更好的事情可做,而且我总是喜欢挑战:

<?php

// A few constants.
define('DOWN', 0);
define('LEFT', 3);
define('RIGHT', 1);
define('UP', 2);

// Dictates the size of the spiral.
$size = 11;

// The initial number.
$number = 0;

// The initial direction.
$direction = RIGHT;

// The distance and number of points remaining before switching direction.
$remain = $distance = 1;

// The initial "x" and "y" point.
$y = $x = round($size / 2);

// The dimension of the spiral.
$dimension = $size * $size;

// Loop
for ( $count = 0; $count < $dimension; $count++ )
{
  // Add the current number to the "x" and "y" coordinates.
  $spiral[$x][$y] = $number;

  // Depending on the direction, set the "x" or "y" value.
  switch ( $direction )
  {
    case LEFT: $y--; break;
    case UP: $x--; break;
    case DOWN: $x++; break;
    case RIGHT: $y++; break;
  }

  // If the distance remaining is "0", switch direction.
  if ( --$remain == 0 )
  {
    switch ( $direction )
    {
      case DOWN:
        $direction = LEFT;
        $distance++;

        break;
      case UP:
        $distance++;

      default:
        $direction--;

        break;
    }

    // Reset the distance remaining.
    $remain = $distance;
  }

  // Increment the number or reset it to 0 if the number is 9.
  if ( $number < 9 )
    $number++;
  else
    $number = 0;
}

// Sort by "x" numerically.
ksort($spiral, SORT_NUMERIC);

foreach ( $spiral as &$x )
{
  // Sort by "y" numerically.
  ksort($x, SORT_NUMERIC);

  foreach ( $x as $ykey => $y )
    // Output the number.
    echo $y . ' ';

  // Skip a line.
  echo PHP_EOL;
}

输出:

0 1 2 3 4 5 6 7 8 9 0 
9 2 3 4 5 6 7 8 9 0 1 
8 1 2 3 4 5 6 7 8 9 2 
7 0 1 0 1 2 3 4 5 0 3 
6 9 0 9 6 7 8 9 6 1 4 
5 8 9 8 5 0 1 0 7 2 5 
4 7 8 7 4 3 2 1 8 3 6 
3 6 7 6 5 4 3 2 9 4 7 
2 5 6 5 4 3 2 1 0 5 8 
1 4 3 2 1 0 9 8 7 6 9 
0 9 8 7 6 5 4 3 2 1 0 

Just because I had nothing better to do and I always like a challenge:

<?php

// A few constants.
define('DOWN', 0);
define('LEFT', 3);
define('RIGHT', 1);
define('UP', 2);

// Dictates the size of the spiral.
$size = 11;

// The initial number.
$number = 0;

// The initial direction.
$direction = RIGHT;

// The distance and number of points remaining before switching direction.
$remain = $distance = 1;

// The initial "x" and "y" point.
$y = $x = round($size / 2);

// The dimension of the spiral.
$dimension = $size * $size;

// Loop
for ( $count = 0; $count < $dimension; $count++ )
{
  // Add the current number to the "x" and "y" coordinates.
  $spiral[$x][$y] = $number;

  // Depending on the direction, set the "x" or "y" value.
  switch ( $direction )
  {
    case LEFT: $y--; break;
    case UP: $x--; break;
    case DOWN: $x++; break;
    case RIGHT: $y++; break;
  }

  // If the distance remaining is "0", switch direction.
  if ( --$remain == 0 )
  {
    switch ( $direction )
    {
      case DOWN:
        $direction = LEFT;
        $distance++;

        break;
      case UP:
        $distance++;

      default:
        $direction--;

        break;
    }

    // Reset the distance remaining.
    $remain = $distance;
  }

  // Increment the number or reset it to 0 if the number is 9.
  if ( $number < 9 )
    $number++;
  else
    $number = 0;
}

// Sort by "x" numerically.
ksort($spiral, SORT_NUMERIC);

foreach ( $spiral as &$x )
{
  // Sort by "y" numerically.
  ksort($x, SORT_NUMERIC);

  foreach ( $x as $ykey => $y )
    // Output the number.
    echo $y . ' ';

  // Skip a line.
  echo PHP_EOL;
}

Outputs:

0 1 2 3 4 5 6 7 8 9 0 
9 2 3 4 5 6 7 8 9 0 1 
8 1 2 3 4 5 6 7 8 9 2 
7 0 1 0 1 2 3 4 5 0 3 
6 9 0 9 6 7 8 9 6 1 4 
5 8 9 8 5 0 1 0 7 2 5 
4 7 8 7 4 3 2 1 8 3 6 
3 6 7 6 5 4 3 2 9 4 7 
2 5 6 5 4 3 2 1 0 5 8 
1 4 3 2 1 0 9 8 7 6 9 
0 9 8 7 6 5 4 3 2 1 0 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文