如何用数字1在75%的数组中实现对角线,其余的(25%)用数字-1实现?

时间:2018-02-13 15:17:01

标签: java

我必须编写函数,创建具有[50,100]随机大小的二维整数数组,并且可以被4整除。行数等于列数。接下来,阵列满足来自范围[a,b)的随机数(对角线上的元素除外),其中a和b由用户输入。

对角线上的数值随机排列,数量为75%,其余为25%,数字为-1。

函数,应打印出来控制数量小于单元格的行和列的索引乘积的单元格数量。

我不知道如何处理这些对角线和细胞数量......

到目前为止,我发现了类似的事情:

public static void createArray()
{
    Random generator = new Random();
    Scanner in = new Scanner(System.in);

    int drawed = 1, rows, cols;

    while (drawed %4 != 0)
    {
        drawed = 50 + generator.nextInt(51);
    }
    rows = drawed;
    cols = rows;

    int[][] array = new int[rows][cols];

    System.out.println("Input a: ");
    int a = in.nextInt();

    System.out.println("Input b: ");
    int b = in.nextInt();

    for (int i = 0; i < array.length; i++)
        for (int j = 0; j < array[i].length; j++)
        {
            if (i != j)
                array[i][j] = a + generator.nextInt(b - a);
        }
}

如何用数字1实现75%的对角线,其余的(25%)数字-1? 如何计算值的单元格是否小于行和列索引的乘积?

2 个答案:

答案 0 :(得分:1)

您好我无法理解您向控制台显示输出的最后一点。但是,以下程序将解决您在对角线值方面遇到的问题。

public class Application {

    public static void main(String[] args) {
        doJob(12, 20, 200);

    }

    private static void doJob(int size, int a, int b) {
        if (size % 4 == 0) {

            int[][] array = new int[size][size];

            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {

                    if (i != j && (i + j) != (size - 1))
                        array[i][j] = generateRandomNumber(a, b);

                }
            }

            int positiveOneSize = calculateLocationRequiredForOne(size * 2);
            int negetiveOneSize = (size - 1) - positiveOneSize;

            /* Fill the diagonals with random values for positive one */

            while (positiveOneSize > 0 || negetiveOneSize > 0) {

                int location = generateRandomNumber(0, size - 1); // Random loc
                int posOrNeg = generateRandomNumber(0, 2);
                if (posOrNeg == 0) {

                    array[location][location] = 1;
                    array[location][(size - 1) - location] = 1;

                } else {
                    array[location][location] = -1;
                    array[location][(size - 1) - location] = -1;

                }
                positiveOneSize--;
                negetiveOneSize--;
            }

            /* Print array */

            for (int m = 0; m < size; m++) {
                for (int n = 0; n < size; n++) {
                    System.out.print(" " + array[m][n]);
                }
                System.out.println();
            }
        }

        else {
            System.out.println("Error");
        }
    }

    private static int generateRandomNumber(int a, int b) {
        return a + (int) (Math.random() * ((b - a) + 1));
    }

    private static int calculateLocationRequiredForOne(int size) {
        return (int) (0.75 * size);
    }

}

答案 1 :(得分:0)

  

我不知道如何处理这些对角线

首先,让我们尝试绘制任何字符的对角线,如this answer

基本上你必须知道索引何时属于对角线,这可以用一些逻辑来完成,让我们使用N x NN = 8

[0][0] [0][1] [0][2] [0][3] [0][4] [0][5] [0][6] [0][7]
[1][0] [1][1] [1][2] [1][3] [1][4] [1][5] [1][6] [1][7]
[2][0] [2][1] [2][2] [2][3] [2][4] [2][5] [2][6] [2][7]
[3][0] [3][1] [3][2] [3][3] [3][4] [3][5] [3][6] [3][7]
[4][0] [4][1] [4][2] [4][3] [4][4] [4][5] [4][6] [4][7]
[5][0] [5][1] [5][2] [5][3] [5][4] [5][5] [5][6] [5][7]
[6][0] [6][1] [6][2] [6][3] [6][4] [6][5] [6][6] [6][7]
[7][0] [7][1] [7][2] [7][3] [7][4] [7][5] [7][6] [7][7]

对角线有一种模式:你能看到吗?

  • \行和列的编号相同
  • / 行=数字 - 列 - 1
  

如何用数字1实现75%的对角线,其余的(25%)数字为-1?

你需要知道你要使用多少个数字,但是......你已经知道了!还记得N吗?好吧,我们将使用公式来了解75%的{​​{1}}:

  • N
  • 75% of N = N * 75 / 100

在我们的案例中使用25% of N = N * 25 / 100成为:

  • 8 * 75 = 600/100 = 6
  • 8 * 25 = 200/100 = 2

然后创建自己的函数,根据您已添加的正数或负数,返回N = 81,如下面的代码所示

  

如何计算值的单元格是否小于行和列索引的乘积?

在代码中,您需要知道有多少值小于-1

i * j

加入所有内容(不使用扫描仪,因为我很懒):

if (array[i][j] < i * j) {
    count++;
}

为此产生类似的输出:

import java.util.Random;

public class SquareDiagonalNumbers {
    private static int positive = 0;
    private static int negative = 0;

    private static int n = 8;

    private static int positiveOnes = n * 75 / 100; //75%
    private static int negativeOnes = n * 25 / 100; //25%

    private static int a = 10;
    private static int b = 20;

    public static void main(String[] args) {
        int[][] array = new int[n][n];

        int products = 0;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                array[i][j] = i == j || i == (n - j - 1) ? randomOnes() : randomNumber();
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (array[i][j] < i * j) {
                    products++;
                    System.out.println("At [" + i + ", " + j + "] number " + array[i][j] + " is lower than [i * j]: " + (i * j));
                }
            }
        }

        System.out.println("Total of numbers lower than the product of their row and column: " + products);
    }

    private static int randomNumber() {
        Random r = new Random();
        return r.nextInt(b - a) + a;
    }

    private static int randomOnes() {
        Random r = new Random();
        boolean isPositive = r.nextBoolean();

        if (isPositive) {
            if (positive < positiveOnes) {
                positive++;
                return 1;
            } else {
                negative++;
                return -1;
            }
        } else {
            if (negative < negativeOnes) {
                negative++;
                return -1;
            } else {
                positive++;
                return 1;
            }
        }
    }
}