【问题标题】:How do I generate a magic square using only a 1 dimensional array?如何仅使用一维数组生成幻方?
【发布时间】:2015-10-29 23:28:48
【问题描述】:

我被指派提示用户给出幻方的顺序(3 阶幻方将是 3x3 矩阵),然后在不使用二维数组的情况下生成该顺序的幻方。

以下是我所知道和理解的:

  • 数组将包含平方元素。
  • 打印出表格时,数组中数字的位置为[order × row + col]
  • 数组在表中位置的索引是(索引/顺序,索引%顺序)

这是给我的,但不明白如何正确实施:

  1. row = order − 1, col = order/2 and i = 1
  2. 重复以下操作,直到 i = order^2 + 1:

    (a) 魔法[索引] = i

    (b) row 和 col 加 1。即 row = (row + 1) mod order 和 col = (col + 1) % order

    (c) 如果魔法[index] != 0

       i. row = (row + order − 2) % order
    
       ii. col = (col + order − 1) % order
    

    (d) 将 i 加 1

这是我目前的代码:

package magicsquarecreator;

import java.io.IOException;
import java.util.Scanner;


public class MagicSquareDemo
{
   public static void main(String[] args) 
   {
      Scanner keyIn = new Scanner(System.in); 
      String option; 
      do
      {
         System.out.println();        
         System.out.println("             MAGIC SQUARE APPLICATION             ");
         System.out.println("==================================================");
         System.out.println("Generate a Magic Square........................[1]");
         System.out.println("Test for a Magic Square........................[2]");
         System.out.println("Quit the Program...............................[0]");
         System.out.println();
         System.out.print("Select an option -> ");
         option = keyIn.next(); 
         System.out.println(); 

         switch(option)
         {
            case "1": 
               try
               {
                    System.out.println("Enter the dimension of the magic square -> ");
                    int order = keyIn.nextInt();
                    if (order < 1)
                    {
                        System.out.println("The order must be positive and odd");
                    }
                    else if (order % 2 == 0)
                    {
                        System.out.println("The program can only generate a magic square"
                                + "\nwhose dimension is positive odd number.");
                    }
                    else if (order % 2 != 0)
                    {
                        int i=1, j=1;
                        int row = order - 1;
                        int col = order/2;
                        int[] magic = new int[order*order];
                        for (i=1; i<=order; i++)
                        {
                            magic[i] = i;
                            row = (row + 1) % order;
                            col = (col + 1) % order;
                            if (magic[i] != 0)
                            {
                              row = (row + order − 2) % order;
                              col = (col + order − 1) % order;
                            }
                        }
                    }
               }
               catch(IOException e)
               {
                  System.out.println(e);
               }
               break;                    
            case "2": 
               try
               {


               }
               catch(IOException e)
               {
                  System.out.println(e);
               }
               break;    
            case "0": 
               break;    
            default: System.out.println("Invalid choice...please select a valid menu option.");
         }
      } 
      while (option.compareTo("0") != 0); 
   } 
}

现在我只是担心获取和理解案例 1。用于文件输出的 Try Catch 语句,但我可以自己做。目前最大的问题是理解如何创建这个循环。

【问题讨论】:

  • 在标题中您询问一维数组,但在问题正文中您说任务是使用二维数组。是哪个?
  • 如果基于两个轴计算偏移量,一维数组可以用作二维数组,例如row*row_width+column。毕竟,这就是语言在内部实现它的方式,假设数组是连续存储的,而不是作为子列表的列表。
  • aahh 我上次编辑错了,不要确认
  • @keshlam 我不确定我是否理解它是如何工作的。你能解释一下吗

标签: java arrays magic-square


【解决方案1】:

说实话,我也没有真正理解 taks,因为它是你写的。如果我按照我的理解去做,它就行不通。 但我看了看它应该如何工作,最后我明白了。 这是 else if (order % 2 != 0) 条件作为方法的部分(我测试了它,它现在正在工作):

public static void magicSqare(int order){
    System.out.println(order);
    int row = order - 1;
    int col = order /2;
    int [] magic = new int [order*order];
    int index;

    index = (row)*order + col;
    magic[index]= 1;

    for (int i = 2; i <= order*order; i++) {
        if (magic[((row +1) % order)*order + ((col +1) % order)] == 0) {
        row = (row +1) % order;
        col = (col +1) % order;
        }

        else {
            row = (row + order -1) % order;
            //col = (col + order -1) % order;
        }
        index = (row)*order + col;
        magic[index]= i;
    }

    //System.out.println(Arrays.toString(magic));
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-16
  • 2018-02-28
  • 2015-04-22
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
相关资源
最近更新 更多