循环模式的正方形

时间:2011-11-26 13:47:03

标签: java for-loop

我正面临着我的大脑无法处理的问题! 我需要创建一个循环来创建这样的模式:

1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1

所以内部数字越大,但我无法想象我怎么能创建这样的循环 我需要这个为我的AI,所以我可以为实体创建兴趣区域,所以这不是学校的分配和我迄今为止所尝试的

for(int i = 0; i < rows; i++){
   for(int j = 0; j < cols; j++){
      System.out.print("?");
   }
   System.out.println();
}

我真的无法想出一种方法来获得代表它的水平的数字! 我一直试图将这个想象给自己等等,以弄清楚如何创造这个或创造这个最好的方法..请帮助我和我的大脑避免头痛! :)我想要的是简单的伪代码或任何易于理解的语言的代码(例如java,c ++,c ...)

9 个答案:

答案 0 :(得分:4)

你可以这样做:

for(int i = 0; i < rows; i++){
   for(int j = 0; j < cols; j++){
      // The distance to the left, right, top and bottom border:
      int dl = j;
      int dr = cols - (j+1);
      int dt = i;
      int db = rows - (i+1);

      // The distance to the closest border:
      int d = Math.min(Math.min(dl, dr), Math.min(dt, db));

      // Print according number
      System.out.print(d+1);
   }
   System.out.println();
}

答案 1 :(得分:2)

您想要的数字是水平或垂直到边缘的最小距离。在伪代码中:

min(i, j , n - 1 - j, n - 1 - i) + 1

答案 2 :(得分:0)

让你的“?”是i,j,(行-1)-i和(cols-1)-j的最小值的1+。

答案 3 :(得分:0)

因此,在一个维度上与外界的距离是这样的(让我们先考虑行):

distanceR = min(i, rows - 1 - i)

其中min是一个返回两个值中较小值的函数。

好的,让我们再次为列做同样的事情:

distanceC = min(j, cols - 1 - j)

现在,您要在每个单元格中计算的值为:

min(distanceR, distanceC) + 1

答案 4 :(得分:0)

    int dim = 5;
    int level = 1;
    String line;
    for (int i = 0; i < dim; i++) {
        int l = 0;
        line = "";
        for (int j = 0; j < dim; j++) {

            if (j < (dim / 2)+1) {
                l++;
                if (l > level) {
                    System.out.print(level);
                } else {
                    System.out.print(l);
                }
            } else {
                l--;
                if (l >= level) {
                    System.out.print(level);

                } else {
                    System.out.print(l);

                }
            }
        }
        System.out.println("");

        if (i<dim/2)
            level++; 
        else level--;
    }

答案 5 :(得分:0)

        //create pyramid

        int intCounterX = 0;
        int intCounterY = 0;
        int intElevationX = 0;
        int intElevationY = 0;
        int intElevation = 1;
        int intSide = 5;
        int intMid = (intSide + 1) / 2;

        for (intCounterY = 0; intCounterY < intSide; intCounterY++)
        {
            for (intCounterX = 0; intCounterX < intSide; intCounterX++)
            {
                //determine if we're on an edge
                if (intCounterX == 0 || intCounterY == 0 || intCounterX == (intSide - 1) || intCounterY == (intSide - 1))
                {
                    //edges are always 1
                    intElevation = 1;
                }
                else
                {
                    //increasing elevation to mid, decreasing after
                    //for X
                    if (intCounterX < (intMid - 1))
                    {
                        //going up
                        intElevationX = intCounterX + 1;
                    }
                    else if (intCounterX == (intMid - 1))
                    {
                        //point
                        intElevationX = intMid;
                    }
                    else
                    {
                        //must be going down
                        intElevationX = intCounterX - 1;
                    }

                    //for Y
                    if (intCounterY < (intMid - 1))
                    {
                        //going up
                        intElevationY = intCounterY + 1;
                    }
                    else if (intCounterY == (intMid - 1))
                    {
                        //point
                        intElevationY = intMid;
                    }
                    else
                    {
                        //must be going down
                        intElevationY = intCounterY - 1;
                    }

                    //take the lower of the two
                    if (intElevationX < intElevationY)
                    {
                        //X is lower
                        intElevation = intElevationX;
                    }
                    else
                    {
                        //Y is lower or equal
                        intElevation = intElevationY;
                    }
                }

                Console.Write(intElevation);
            }
            Console.WriteLine();
        }

答案 6 :(得分:0)

在Matlab中,它将第一个元素编入索引为1:

n = 5;
for i = 1:n
    for j = 1:n
        a = [i, j , n - j + 1, n - i + 1];
        fprintf('%d ',(min(a)));
    end
    fprintf('\n');
end

答案 7 :(得分:0)

var rows:int=7;
var cols:int=7;

var mid:int=Math.floor(rows / 2); //Gives the 0 based index of middle row
var max:int=mid+1;
for(var i:int=0; i<rows; i++) {
    var s:String="";
    var diff:int=mid - i;
    var maxNum:int=max - Math.abs(mid - i);
    //trace(maxNum);
    for(var j:int=0; j<cols; j++) {
        var dta:int=Math.abs(j - mid)
        if(max - dta > maxNum) {
            s += maxNum;
        } else {
            s += (max - dta);
        }
    }

    trace(s);
}

这是我在ActionScript中的代码(java环境在我的comp上不是很方便)。如果你等几分钟,我也会发布java代码

修改

int  rows=7;
int  cols=7;

int  mid=(int) Math.Floor((double)(rows / 2)); //Gives the 0 based index of middle row
int  max=mid+1;
for(int  i=0; i<rows; i++) {
    int  diff=mid - i;
    int  maxNum=max - Math.Abs(mid - i);
    //trace(maxNum);
    for(int  j=0; j<cols; j++) {
        int dta=Math.Abs(j - mid);
        if(max - dta > maxNum) {
            System.out.write(maxNum);
        } else {
            System.out.write(max - dta);
        }
    }

    System.out.writeLine();
}

<强>输出

1111111   
1222221   
1233321   
1234321   
1233321   
1222221   
1111111

答案 8 :(得分:0)

批处理,因为我是个疯子

C:\>t
11111
12221
12321
12221
11111

C:\>

您可以从m = 5 n = 5更改为m = 7 n = 7 e.t.c。

@ECHO OFF

setlocal EnableDelayedExpansion


set m=5
set n=5
set /A m_minus1=m-1
set /A n_minus1=n-1

SET /A amid=(%m%+1)/2
SET /A bmid=(%n%+1)/2

for /L %%a in (1,1,%m%) do (
  for /L %%b in (1,1,%n%) do (
    IF %%a==1 SET STRING=!STRING!1
    IF %%a==%m% SET STRING=!STRING!1
     IF %%a GEQ 2 (
         IF %%a LEQ %m_minus1% (
              IF %%a==%amid% (
                 if %%b==%bmid% (
                       SET STRING=!STRING!3
                  ) ELSE (

                   IF %%b==1  SET STRING=!STRING!1
                   IF %%b==%n%  SET STRING=!STRING!1
                   IF %%b GEQ 2 (
                      IF %%b LEQ %n_minus1% (
                         SET STRING=!STRING!2
                      ) %/IF LEQ %
                   ) % /IF GEQ %

                  ) % /ELSE %




              ) ELSE (   % /IF a mids equal/not equal % 


                   IF %%b==1  SET STRING=!STRING!1
                   IF %%b==%n%  SET STRING=!STRING!1
                   IF %%b GEQ 2 (
                      IF %%b LEQ %n_minus1% (
                         SET STRING=!STRING!2
                      ) %/IF LEQ %
                   ) % /IF GEQ %

              ) % /ELSE of IF a mids equal/not equal % 

        ) % LEQ 4 %        
    ) % GEQ 2 %
  ) % b for %

  ECHO/!STRING!
  SET STRING=
) % a for % 

endlocal