java中如何统计奇数个数_Java中的奇数魔术方块

java中如何统计奇数个数

It's unclear who first came up with a magic square. There is a story about a huge flood in China a long time ago. The people were worried they would be washed away and tried to appease the river god by making sacrifices. Nothing seemed to work until a child noticed a turtle sporting a magic square on it's back that kept circling the sacrifice. The square told the people how big their sacrifice needed to be in order to save themselves. Since then magic squares have been the height of fashion for any discerning turtle.

目前尚不清楚谁首先提出了一个魔方。 一个故事是关于很久以前中国发生的大水灾。 人们担心他们会被冲走,并试图通过做出牺牲来安抚河神。 直到一个孩子注意到乌龟背上有一个魔方一直绕着牺牲物时,似乎什么都没有起作用。 广场告诉人们为了挽救自己需要付出多大的牺牲。 从那以后,魔方一直是任何有识之士的时尚潮流。

Level: Beginner

级别:初学者

Focus: Logic, Arrays, Methods

重点:逻辑, 数组 ,方法

奇幻广场 ( Odd Magic Squares )

In case you've never come across one before, a magic square is an arrangement of sequential numbers in a square so that the rows, columns, and diagonals all add up to the same number. For instance, a 3x3 magic square is:

万一您从未遇到过,魔术正方形是正方形中连续数字的排列,因此行,列和对角线的总和都相同。 例如,一个3x3的魔术方块为:

 8 1 6
 3 5 7
 4 9 2

Each row, column and diagonal adds up to 15.

每行,一列和对角线总计为15。

奇幻广场问题 ( Odd Magic Squares Question )

This programming exercise is concerned with creating odd sized magic squares (i.e., the size of the square can only be an odd number, 3x3, 5x5, 7x7, 9x9, and so on). The trick with making such a square is to place the number 1 in the first row and middle column. To find where to place the next number, move diagonally upwards to the right (i.e., one row up, one column across). If such a move means you fall off the square, wrap around to the row or column on the opposite side. Finally, if the move takes you to a square that is already filled, go back to the original square and move downwards by one. Repeat the process until all the squares are filled.

此编程练习与创建奇数大小的幻方有关(即,正方形的大小只能是奇数,3x3、5x5、7x7、9x9等)。 制作这样一个正方形的技巧是将数字1放在第一行和中间一列。 要找到下一个数字的放置位置,请向右斜向上移动(即,向上一行,跨过一列)。 如果这样移动意味着您掉下了正方形,请绕到另一侧的行或列。 最后,如果此举将您带到一个已经填充的正方形,请返回原始正方形并向下移动一个。 重复该过程,直到所有正方形都填满。

For example, a 3x3 magic square would start like so:

例如,一个3x3的魔术方块将像这样开始:

 0 1 0
 0 0 0
 0 0 0

A move diagonally upwards means we wrap around to the bottom of the square:

对角线向上移动意味着我们环绕到正方形的底部:

 0 1 0
 0 0 0
 0 0 2

Likewise, the next diagonal move upwards means we wrap around to the first column:

同样,下一个对角线向上移动意味着我们回绕到第一列:

 0 1 0
 3 0 0
 0 0 2

Now the diagonal move upwards results in a square that is already filled, so we go back to where we came from and drop down a row:

现在,对角线向上移动将导致一个已经被填充的正方形,因此我们回到原来的位置并下拉一行:

 0 1 0
 3 0 0
 4 0 2

and it continues on and on until all the squares are full.

并一直持续到所有方格都填满为止。

课程要求 ( Program Requirements )

  • a user must be able to enter in the size of the magic square.

    用户必须能够输入魔术方块的大小。
  • they must only be allowed to enter in an odd number.

    仅允许输入一个奇数。
  • use a method to create the magic square.

    使用一种方法来创建魔术方块。
  • use a method to display the magic square.

    使用一种方法来显示魔术方块。

The question is can your program create a 5x5 magic square like the one below?

问题是您的程序可以创建一个5x5的魔术方块吗?

 17 24  1   8 15 
 23  5   7 14 16 
  4   6 13 20 22 
 10 12 19 21  3 
 11 18 25  2   9 

Hint: Apart from the programming aspects of this exercise it's also a test of logic. Take each step of creating the magic square in turn and figure how it can be done with a two-dimensional array.

提示:除了本练习的编程方面之外,它也是对逻辑的检验。 依次执行创建魔方的每个步骤,并弄清楚如何使用二维数组来完成。

奇幻广场解决方案 ( Odd Magic Square Solution )

Your program should have been capable of creating the 5x5 magic square below:

您的程序应该已经能够创建下面的5x5魔术方块:

 17 24  1   8 15 
 23  5   7 14 16 
  4   6 13 20 22 
 10 12 19 21  3 
 11 18 25  2   9 

Here's my version:

这是我的版本:

 import java.util.Scanner;
 public class MagicOddSquare {
   public static void main(String[] args) {
     Scanner input = new Scanner(System.in);
     int[][] magicSquare;
     boolean isAcceptableNumber = false;
     int size = -1;
     //only accept odd numbers
     while (isAcceptableNumber == false)
     {
       System.out.println("Enter in size of square: ");
       String sizeText = input.nextLine();
       size = Integer.parseInt(sizeText);
       if (size % 2 == 0)
       {
         System.out.println("The size must be an odd number");
         isAcceptableNumber = false;
       }
       else
       {
         isAcceptableNumber = true;
       }
     }
     magicSquare = createOddSquare(size);
     displaySquare(magicSquare); 
   }
   private static int[][] createOddSquare(int size)
   {
     int[][] magicSq = new int[size][size];
     int row = 0;
     int column = size/2;
     int lastRow = row;
     int lastColumn = column;
     int matrixSize = size*size; 
     magicSq[row][column]= 1;
     for (int k=2;k < matrixSize+1;k++)
     {
       //check if we need to wrap to opposite row
       if (row - 1 < 0)
       {
         row = size-1;
       }
       else
       {
         row--;
       }
       //check if we need to wrap to opposite column
       if (column + 1 == size)
       {
         column = 0;
       }
       else
       {
         column++;
       }
       //if this position isn't empty then go back to where we
       //started and move one row down
       if (magicSq[row][column] == 0)
       {
         magicSq[row][column] = k;
       }
       else
       {
         row = lastRow;
         column = lastColumn;
         if (row + 1 == size)
         {
           row=0;
         }
          else
         {
           row++;
         }
         magicSq[row][column] = k;
       }
       lastRow = row;
       lastColumn= column; 
     }
     return magicSq;
   }
   private static void displaySquare(int[][] magicSq)
   {
     int magicConstant = 0;
     for (int j=0;j<(magicSq.length);j++)
     {
       for (int k=0;k<(magicSq[j].length);k++)
       {
         System.out.print(magicSq[j][k] + " ");
       }
       System.out.print;
       magicConstant = magicConstant + magicSq[j][0];
     }
      System.out.print("The magic constant is " + magicConstant);
   }
 } 

翻译自: https://www.thoughtco.com/odd-magic-squares-2034028

java中如何统计奇数个数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值