用字符填充矩阵,使它们形成一个整齐的十字,其中每个较小的对角线是 (char + 1)

发布于 2025-01-15 17:54:53 字数 1640 浏览 0 评论 0原文

情节目标

目标是绘制此模式,其中 N 在 [4,100] 范围内。主对角线始终为 'a'a 的邻居应为 bb 的邻居应为 >c 等等。如果我们已经迭代到 z 字符 - 切换到 a 返回。

下面的代码试图绘制一个更简单的模式,但对角线从左下角到右上角,我在索引方面非常糟糕:\

Easy pattern

import java.util.*;

class Solution {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int number = sc.nextInt();

        char[][] array = new char[number][number];

        char paint = 'a';

        for (int t = 0; t < (number * number - 2); t++) {
            for (int k = 0; k < number; k++) {
                for (int j = 0; j < number; j++) {
                    if (k + j == t) {
                        array[k][j] = paint;
//                        System.out.print(array[k][j] + " ");
                    }
                }

            }
            if (paint == 123) {
                paint = (char) 97;
            } else {
                paint += 1;
            }
            

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

n = 5 的图:

[a, b, c, d, e]
[b, c, d, e, f]
[c, d, e, f, g]
[d, e, f, g, h]
[e, f, g, h, i]

我认为对角线相等总和位于所述对角线上的每个元素的索引,但是还有另一种模式使用中间中心十字,它充当对称线,但计算出矩阵象限的索引公式远远超出了我的能力。

请问有什么建议吗?

Plot goal

The goal is to plot this pattern, where N is in [4,100] range. Main diagonals are always 'a', neighbours of a should be b, neighbours of b should be c and so on. If we have already iterated to z character - switch to a back.

Code below is an attempt to plot an easier pattern but diagonals go from bottom-left to top-right corners, I'm very bad at indices :\

Easy pattern

import java.util.*;

class Solution {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int number = sc.nextInt();

        char[][] array = new char[number][number];

        char paint = 'a';

        for (int t = 0; t < (number * number - 2); t++) {
            for (int k = 0; k < number; k++) {
                for (int j = 0; j < number; j++) {
                    if (k + j == t) {
                        array[k][j] = paint;
//                        System.out.print(array[k][j] + " ");
                    }
                }

            }
            if (paint == 123) {
                paint = (char) 97;
            } else {
                paint += 1;
            }
            

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

plots for n = 5:

[a, b, c, d, e]
[b, c, d, e, f]
[c, d, e, f, g]
[d, e, f, g, h]
[e, f, g, h, i]

I figured the diagonals have equal sums of indices of every element that is located on said diagonal, however there is also another pattern that uses middle central cross, it acts as a line of symmetry but figuring out indices formula for matrix quadrants is way out of my league.

Any advice please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唱一曲作罢 2025-01-22 17:54:53

这是一个有趣的问题。我想我想出了一个解决方案,但首先让我解释一下我是如何想到的。

我认为可以简单地循环矩阵的所有元素,并根据一些算术决定字符,而不是动态更改字符。

正如您所指出的,您的矩阵实际上是高度对称的,并且四个象限是相关的。这意味着您可能只在两个方向上沿着矩阵的一半,并使用正确的字符一次设置四个元素(每个象限一个)。

为了简单起见,从偶数大小的矩阵开始,您可以找到与左上象限索引 i,j 处的字符相对应的公式。这就是定义 indexLetter 的公式。利用对称性,您可以直接设置其他象限中的每个相应元素。

如果你想要一个奇数大小的矩阵,那么,这几乎是相同的,只是你需要采用 (N+1)/2 而不是 N/2。

这给出了下一个结果:

import java.util.*;

class Solution {

    final static String alphabet = "abcdefghijklmnopqrstuvwxyz";

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int number = sc.nextInt();

        char[][] array = new char[number][number];

        for(int i = 0; i < Math.floor((number+1)/2); i++)
        {    
            for (int j = 0; j < Math.floor((number+1)/2); j++)
            {
                //determine the index of the right character 
                //using some arithmetics

                //next number works for both even and odd inputed number
                int nbLetters = (int)Math.floor((number+1)/2);
                int indexLetter = Math.abs(j - i) % nbLetters;
                
                //set it back in all parts of the graphs
                array[i][j] = alphabet.charAt(indexLetter);
                array[i][number-j-1] =  alphabet.charAt(indexLetter);
                array[number-i-1][j] =  alphabet.charAt(indexLetter);                
                array[number-i-1][number-j-1] =  alphabet.charAt(indexLetter);
            }
        }

        //print it back
        for (char[] row : array)
        {
            System.out.println(Arrays.toString(row));
        }
    }
}

注意:如果你想采用大于 52 的大小,你必须顺便调整一下之前的答案(我猜在某个地方添加了一个模数)。

That's a funny problem you have here. I think I came up with a solution, but first let me explain how I thought of it.

Instead of dynamically change the character, I thought it was possible to simply loop over all the elements of the matrix, and decide the character based on some arithmetic.

It seems, as you pointed out, that your matrix is actually highly symmetric, and that the four quadrants are related. That means you might go along half of your matrix only in both directions, and set four elements at once with the right character (one in each quadrant).

Starting with an even size of the matrix for simplicity, you can find a formula that corresponds to the character at index i,j in the top left quadrant. That's the formula which defines indexLetter. Using symmetry, you can set each corresponding element in the other quadrants directly.

If you want a matrix of odd size, well, that's almost the same, except you need to take (N+1)/2 instead of N/2.

That gives next result:

import java.util.*;

class Solution {

    final static String alphabet = "abcdefghijklmnopqrstuvwxyz";

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int number = sc.nextInt();

        char[][] array = new char[number][number];

        for(int i = 0; i < Math.floor((number+1)/2); i++)
        {    
            for (int j = 0; j < Math.floor((number+1)/2); j++)
            {
                //determine the index of the right character 
                //using some arithmetics

                //next number works for both even and odd inputed number
                int nbLetters = (int)Math.floor((number+1)/2);
                int indexLetter = Math.abs(j - i) % nbLetters;
                
                //set it back in all parts of the graphs
                array[i][j] = alphabet.charAt(indexLetter);
                array[i][number-j-1] =  alphabet.charAt(indexLetter);
                array[number-i-1][j] =  alphabet.charAt(indexLetter);                
                array[number-i-1][number-j-1] =  alphabet.charAt(indexLetter);
            }
        }

        //print it back
        for (char[] row : array)
        {
            System.out.println(Arrays.toString(row));
        }
    }
}

Note: if you want to take a size greater than 52, you have to adapt a little the previous answer by the way (adding a modulo somewhere I guess).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文