Is it possible to condense my code into less than

2019-09-12 07:20发布

问题:

public class MagicSquare
{
    public static int[][] grid = new int[3][3];
    public static int i = 0;
    public static int j = 0;

    public static void main(String[] args) {

       int x = 1;
       int y = 2;
       int z = 0;

       while(z < 9)
       {
             int holdx = x;
             int holdy = y;
             z++;
             x++;
             y--;

       if(x == 3)
       {
             x = 0;
       }

       if(y == -1)
       {
             y = 2;
       }

       if(y == 3)
       {
             y = 0;
       }

       if(grid[x][y] == 0)
       {
            grid[x][y] = z;
       }

       else
       {
            holdy++;

            if(holdy == 3)
            {
                 holdy = 0;
            }

            grid[holdx][holdy] = z;
            x = holdx;
            y = holdy;
       }
       }       

       for(int i = 0; i < 3; i++)
       {
            System.out.print(grid[i][0]+", ");
       }

       System.out.println(" ");

       for(int i = 0; i < 3; i++)
       {
            System.out.print(grid[i][1]+", ");
       }

       System.out.println(" ");

       for(int i = 0; i < 3; i++)
       {
            System.out.print(grid[i][2]+", ");
       }
}

THE OUTPUT LOOKS LIKE THIS:

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

Hello, I wrote a Black Magic code that is able to fill in the grids of the square up and the right of it, but if it is filled with a number then the next number would be put in the square that is below its current spot.

Then, go one square up and to the right and put the next integer there, and if I also go off the grid, then the next number will wrap around to the bottom and/or left. This program run until all the squares are filled.

I was wondering if it is possible to condense my code starting at my while loop to the end of my for loop into a shorter code?

Someone said that I would be able to code this USING JUST 2 lines, and I think that is bizarre... but they said it's doable!

Any hints, help, or pointer would be appreciated!

Thank you so much!

回答1:

Not sure about less than 3 lines (at least without sacrificing readability), but you can condense these if statements for sure.

if(x == 3) {
     x = 0;
}

if(y == -1) {
     y = 2;
}

if(y == 3) {
     y = 0;
}

Down into simply

x = x % 3;
y = (y + 3) % 3;

And you could pull those into the previous x++ and y--

x = (x + 1) % 3;
y = ((y - 1) + 3) % 3;

Similarly with holdy (if you actually need that value, I do not know).

Then, if you just want to print the array, the for loops can be shortened.

for(int[] row : grid) {
    System.out.println(Arrays.toString(row));
}


回答2:

About 10 years ago, I wrote the following code for computing the entries of a magic square. This, in some sense, boils down to a single line of code, and works for arbitrary odd edge lengths:

class M
{
    public static void main(String args[])
    {
        int n = 5;
        int a[][] = new int[n][n];
        f(n,1,n/2,n-1,a);
        print(a);
    }

    static int f(int j,int i,int k,int l,int I[][])
    {
        return i>j*j?i-1:(I[k][l]=f(j,i+1,(k+(i%j==0?0:1))%j,(l+(i%j==0?-1:1))%j,I))-1;
    }

    public static void print(int a[][])
    {
        for (int i=0; i<a.length; i++)
        {
            for (int j=0; j<a[i].length; j++)
            {
                System.out.print((a[j][i]<10?" ":"")+a[j][i]+" ");
            }
            System.out.println();
        }
    }
}

Of course, it is somehow inspired by the IOCCC and would be better suited for Programming Puzzles & Code Golf, but might show how much you can press into a single line of code when you (ab)use the ternary operator and recursion appropriately...