第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

是否有一個(gè)函數(shù)可以按給定順序不斷增加矩陣中的數(shù)字

是否有一個(gè)函數(shù)可以按給定順序不斷增加矩陣中的數(shù)字

largeQ 2023-10-13 17:24:49
我必須創(chuàng)建一個(gè)矩陣(N*M),其中每個(gè)單元格根據(jù)特定遍歷的順序進(jìn)行標(biāo)記。問題是我讓它只適用于(N N)。我一直在尋找一個(gè)解決方案,以便它也適用于(N M)。例如 N*M 的示例:1  2  5  104  3  6  119  8  7  12N*N 的示例:1  2  5  10 4  3  6  11 9  8  7  12 16 15 14 132x4 看起來像:1 2 5 74 3 6 84x2 看起來像:1 24 35 67 8代碼public static int matrix[][];       public static void main(String[] args) {        borderLayout(5);        print();       }       public static void borderLayout(int size) {           matrix = new int[size][size];           for(int i = 0 ; i < size ; i ++) {               fillBorder(i);           }       }       public static void fillBorder(int n) {           int number = n*n+1;           int row = n;           int column = 0;           for(column = 0; column<=n; column++) {               System.out.println("COLUMN DRAWING");               matrix[row][column]=number++;           }           column--;           for(row = n; row>0 ; row--){               System.out.println("ROW DRAWING");               matrix[row-1][column] = number++;           }           print();           System.out.println("");       }       private static void print() {              int x = matrix.length;              int y = matrix[0].length;              for (int i = 0; i < x; i++) {                 for (int j = 0; j < y; j++) {                    System.out.print(matrix[j][i]);                    System.out.print(" ");                    if (matrix[j][i] < 10) {                       System.out.print(" ");                    }                 }                 System.out.println();              }           }
查看完整描述

3 回答

?
慕標(biāo)5832272

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊

根據(jù)您在我的第一個(gè)答案下的評論:


我也在考慮另一種解決方案......使用 BFS。對于起始點(diǎn),假設(shè)為 0,0,每次迭代總是深度為 1,因此如果從 0,0 開始,則執(zhí)行 1,0 1,1 0,1,當(dāng)?shù)竭_(dá)行或列邊界時(shí),您會(huì)增加深度意味著在下一次迭代中您將使用 0,2 1,2 2,2 2,1 2,0 并且如果矩陣是 nxm 且 n!=m 您只需根據(jù)您所在的位置跳過列或行(檢查矩陣總是綁定 m 和 n)。


忘了說,這樣它也適用于字母。


我決定添加一個(gè)新答案而不是編輯第一個(gè)答案。


由于我不太熟悉圖和廣度優(yōu)先搜索,我無法為基于 BSF 的算法提供任何建議。


但我想我理解你想要如何進(jìn)行的想法,這是一個(gè)簡單的實(shí)現(xiàn)方法。您希望在每次迭代中采取的步驟


0,0

-----

0,1

1,1

1,0

-----

0,2

1,2

2,2

2,1

2,0

-----

0,3

1,3

2,3

3,3

3,2

3,1

3,0

...

無論矩陣是否是正方形,我最初都會(huì)關(guān)注正方形區(qū)域。例如,如果是一個(gè)4x6矩陣,我只看部分4x4,忽略最后兩列。同樣,如果行大于列。對于a,7x5我將查看5x5并忽略最后兩行。在每次迭代中,我將使用兩個(gè)內(nèi)部循環(huán),第一個(gè)循環(huán)增加行,第二個(gè)循環(huán)減少列。


與之前的答案相反,這次我將使用字符串?dāng)?shù)組來完成您的評論:


忘了說,這樣它也適用于字母。


static String[][] fillArray(int rows, int columns){

    char ch = 'A';

    String[][] matrix = new String[rows][columns];

    int square = Math.min(rows, columns);

    for(int i = 0; i < square; i++){

        String curr = String.valueOf(ch);

        for(int r = 0; r <= i ; r++){

            matrix[r][i] = curr;

        }

        for(int c = i-1; c >= 0; c--){

            matrix[i][c] = curr;

        }

        ch++;

    }

    return matrix;

}

例如使用 args 調(diào)用上述方法4x5


public static void main(String[] args) {

    String[][] filled = fillArray(4,5);

    for(String[] row: filled){

        System.out.println(Arrays.toString(row));

    }

}

將導(dǎo)致


[A, B, C, D, null]

[B, B, C, D, null]

[C, C, C, D, null]

[D, D, D, D, null]

現(xiàn)在讓我們看看仍然充滿 的被忽略的區(qū)域null,即 的情況rows > columns or rows < columns。為了簡單起見,我將用 填寫此區(qū)域*。


static String[][] fillArray(int rows, int columns){

    char ch = 'A';

    String[][] matrix = new String[rows][columns];

    int square = Math.min(rows, columns);

    for(int i = 0; i < square; i++){

        String curr = String.valueOf(ch);

        for(int r = 0; r <= i ; r++){

            matrix[r][i] = curr;

        }

        for(int c = i-1; c >= 0; c--){

            matrix[i][c] = curr;

        }

        ch++;

    }


    ch = '*';

    if (rows > columns) {

        for (int i = square; i < rows; i++) {

            for (int j = 0; j < columns; j++) {

                matrix[i][j] = String.valueOf(ch);

            }

        }

    }

    if (rows < columns) {

        for (int i = square; i < columns; i++) {

            for (int j = 0; j < rows; j++) {

                matrix[j][i] = String.valueOf(ch);

            }

        }

    }

    return matrix;

}

調(diào)用 fillArray(4,6)


public static void main(String[] args) {

    String[][] filled = fillArray(4,6);

    for(String[] row: filled){

        System.out.println(Arrays.toString(row));

    }

}

現(xiàn)在應(yīng)該導(dǎo)致:


[A, B, C, D, *, *]

[B, B, C, D, *, *]

[C, C, C, D, *, *]

[D, D, D, D, *, *]

這篇文章比我實(shí)際想象的要長。但我希望它很容易理解。


查看完整回答
反對 回復(fù) 2023-10-13
?
蕭十郎

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊

如果您查看 nxn 矩陣,就會(huì)發(fā)現(xiàn)某種模式需要識(shí)別。


   1    2    5   10   17 

   4    3    6   11   18 

   9    8    7   12   19 

  16   15   14   13   20 

  25   24   23   22   21 

第一列中總是有平方數(shù)

在第一行中,從最小的平方數(shù) 1 開始,添加奇數(shù)序列,即1,3,5,7,9,11 ....

每行的模式


[1]  +1 +3 +5 +7 ....

[4]  -1 +3 +5 +7 ....

[9]  -1 -1 +5 +7 ....

[16] -1 -1 -1 +7 ....

[25] -1 -1 -1 -1 ....

有了這些知識(shí),現(xiàn)在就可以輕松填充n x n矩陣了。1創(chuàng)建一個(gè)包含從到 的奇數(shù)的列表columns-1,通過逐個(gè)添加列表的元素來填充第一行,每次迭代后將列表中的第 i 個(gè)數(shù)字替換為-1能夠使用該行的列表i+1th。


現(xiàn)在對于非方矩陣,可以使用上述方法填充矩陣的方區(qū)域,并分別處理剩余的行或列。找到正方形區(qū)域的大小以定義其余行/列的計(jì)數(shù)器相對簡單。


只要計(jì)算一下Math.pow(Math.min(rows, columns), 2);


我采用了您的方法名稱并稍微更改了您的打印方法。我沒有評論代碼,但希望清楚正在做什么。如果有任何不清楚的地方,請隨時(shí)詢問。


import java.util.List;

import java.util.stream.Collectors;

import java.util.stream.IntStream;


public class Test{


    public static int matrix[][];


    public static void main(String[] args) {

        borderLayout(5, 5);

        print();

    }


    public static void borderLayout(int rows, int columns) {

        //create a list (1,3,5, ...) with size = columns - 1

        List<Integer> list = IntStream.iterate(1, i -> i + 2)

                .limit(columns - 1).boxed()

                .collect(Collectors.toList());

        matrix = new int[rows][columns];

        int square = Math.min(rows, columns);

        for (int i = 0; i < square; i++) {

            int temp = (i + 1) * (i + 1);

            for (int j = 0; j < square; j++) {

                if (j == 0) {

                    matrix[i][j] = temp;

                } else {

                    matrix[i][j] = matrix[i][j - 1] + list.get(j - 1);

                }

            }

            if (i < columns - 1) {

                list.set(i, -1);

            }

        }

        if (rows > columns) {

            int counter = (int) Math.pow(Math.min(rows, columns), 2);

            int sqrt = (int) Math.sqrt(counter);

            for (int i = sqrt; i < rows; i++) {

                for (int j = 0; j < columns; j++) {

                    matrix[i][j] = ++counter;

                }

            }

        } else if (rows < columns) {

            int counter = (int) Math.pow(Math.min(rows, columns), 2);

            int sqrt = (int) Math.sqrt(counter);

            for (int i = sqrt; i < columns; i++) {

                for (int j = 0; j < rows; j++) {

                    matrix[j][i] = ++counter;

                }

            }

        }

    }


    private static void print() {

        for (int[] row : matrix) {

            for (int i : row) {

                System.out.printf("%4d ", i);

            }

            System.out.println();

        }

    }

}


查看完整回答
反對 回復(fù) 2023-10-13
?
牧羊人nacy

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊

如果你沿著左欄看,就會(huì)發(fā)現(xiàn)一個(gè)模式:


 1

 4

 9

16

其中 是(r + 1)^2,其中r是行號(hào)。


沿行,值遞減 1,直到對角線,其中r == c:


 1

 4  3 

 9  8  7

16 15 14 13

這是(r + 1)^2 - c.


如果你沿著頂行看,就會(huì)發(fā)現(xiàn)一個(gè)模式:


1   2   5   10

這是c^2 + 1.


沿著列行向下,值增加 1,直到對角線,其中r == c:


1   2   5   10

    3   6   11

        7   12

            13

這是c^2 + 1 + r


因此,您可以使用嵌套循環(huán)計(jì)算數(shù)組元素:


for (int r = 0; r < N; ++r) {

  for (int c = 0; c < M; ++c) {

    matrix[r][c] = (r >= c) ? ((r + 1)*(r + 1) - c) : (c * c + 1 + r);

  }

}


查看完整回答
反對 回復(fù) 2023-10-13
  • 3 回答
  • 0 關(guān)注
  • 165 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)