Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中镜像三角形的生成_Java_Loops - Fatal编程技术网

java中镜像三角形的生成

java中镜像三角形的生成,java,loops,Java,Loops,我碰巧参加了一个测试,得到了以下问题。我想不出该怎么办。该场景是编写一个java程序,用相应的N打印以下内容。如果假设N=3,它必须有2*N行,并且输出必须为 一, 2*3 4*5*6 4*5*6 2*3 一, 输出必须仅包含数字和星号。N在0到100之间变化。此外,鉴于 public static void main(String[] args){ int rows=2; mirrorTriangle(rows); } public void mirrorTriangle(in

我碰巧参加了一个测试,得到了以下问题。我想不出该怎么办。该场景是编写一个java程序,用相应的N打印以下内容。如果假设N=3,它必须有2*N行,并且输出必须为

一, 2*3 4*5*6 4*5*6 2*3 一,

输出必须仅包含数字和星号。N在0到100之间变化。此外,鉴于

public static void main(String[] args){
    int rows=2;
    mirrorTriangle(rows);
}
public void mirrorTriangle(int n){
    //Logic 
}
我不明白,如果行应该随N变化,为什么要将行声明为2。请解释一下逻辑

def N = 3
def i = 0
def j = 0
int[][] numbers = new int[N][]

// Generate, print, and store numbers
while( i < numbers.length ){
    numbers[i] = new int[i+1]
    j = 0
    while( j < numbers[i].length ){
        numbers[i][j] = j+1
        ++j
        print j
    }
    println ""
    i++
}

// Print them again, in reverse order
i = numbers.length - 1
while( i >= 0 ){
    j = 0
    while( j < numbers[i].length ){
        print numbers[i][j]
        j++
    }
    println ""
    i--
}  

这段代码很容易解释。您只需要N行,但打印2N行,因为,等待它。。。对称性如果您有6行,前3行是新的,而其他3行只是镜像图像,那么当您可以再次打印它们时,为什么要浪费内存空间呢?

请找到问题的解决方案,并提供解释注释

public static void main(String[] args) throws Exception
    {
        // initialize n
        int n = 4;
        // initialize x to 1 from where our printing will start.
        int x = 1;
        /* We will store our generated numbers in an array.
         * For example, the array after we generate 
         * the numbers would look like:
         * [1,0,0,
            2,3,0,
            4,5,6,
            4,5,6,
            2,3,0,
            1,0,0]
         * 
         * When n = 3, there are going to be 3*2 i.e, n*2 rows.
         * in our case 6 rows. 
         * visualize with the above values.
         * The first n/2 rows will be the numbers we print, 
         * the next n/2 will be the mirror image of the first n/2 rows.
         * no. of columns in each row will be equal to n, in our example:3
         */
        int arr[][] = new int[n*2][n];
        /*
         * Start populating the matrix
         * Each row will contain number of elements eaual to the row number,
         *  so 1st row -> 1 element, 2nd - > 2,.. and so on.
         */
        for(int row=0;row<n;row++)
        {
            int col = 0;
            while(col < row+1)
            {
                arr[row][col] = arr[n*2-row-1][col] = x++;
                col++;
            }
        }
        /*
         * Now our task is just to read out the array.
         * The tricky part is adding the astricks.
         * We notice that row1 will have 1-1 asticks, row2 -> 2-1 astricks ,.. and so on.
         * So in between the numbers while reading out,
         * for each row we maintain the number of astricks.
         */
        for(int i=0;i<arr.length;i++)
        {
            StringBuilder build = new StringBuilder();
            for(int j=0;j<arr[i].length;j++)
            {
                if(arr[i][j] > 0)
                {
                    build.append((arr[i][j])).append("*");
                }
            }
            System.out.print(build.delete(build.length()-1,build.length()).toString());
            System.out.println();
        }
    }

递归是否有明确的要求?这是隐含的结构问题没有提到任何地方

int rows=2可能就是一个例子,为了解决这个问题,你不能做任何“聪明”的事情,比如使用指针

我还假设不允许使用值'>100',这样就可以重载n值的含义-2的补码也是如此

如果允许循环,作为递归的替代,您可以生成三角形,而无需将状态保存在堆栈之外:

public static void main(String[] args){
    int rows=3;
    mirrorTriangle(rows);
}

public static void mirrorTriangle(int n){

    for (int i = 0 ; i < n + 1 ; i++) {

        renderLine(i);
    }

    for (int i = n ; i > 0 ; i--) {

        renderLine(i);
    }
}

private static void renderLine(int n) {

    int j = n * (n - 1) / 2 + 1;
    int k = j + n;

    while (j < k) {

        System.out.print(j);
        j++;
        if (j < k) System.out.print('*');
    }

    System.out.println();
}

尝试以下新代码:

public class String4 {    
    public static void main(String[] args) {    
        int rows = 3;    
        mirrorTriangle(rows);    
    }    
    private static void mirrorTriangle(int rows) {    
        for(int i=1;i<=rows;i++)    
        {    
            for(int j=1;j<=i;j++)    
            {    
                System.out.print(i);    
                if(j>0&&j<i)    
                System.out.print("*");    
            }    
            System.out.println();    
        }    
        for(int k=rows;k>0;k--)    
        {    
            for(int l=1;l<=k;l++)    
            {    
                System.out.print(k);    
                if(l>0&&l<k)    
                    System.out.print("*");    
            }    
            System.out.println();    
        }    
    }    
}
输出:

1 2*2 3*3*3 3*3*3 2*2 1
我认为这是一个比选择的解决方案更好、更简单的解决方案

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.println("Enter limit");
    int limit = s.nextInt();
    int start[] = new int[limit];
    int v = 1;
    for (int i=1; i<=limit; i++) {
        start[i-1] = v;
            for (int j=1; j<=i; j++) {
                System.out.print(v++);
                if(j==i)
                    continue;
                System.out.print("*");
            }
            System.out.print("\n");
    }
    for (int i=limit-1; i>=0; i--) {
        v=start[i];
            for (int j=i; j>=0; j--) {
                System.out.print(v++);
                if(j==0)
                    continue;
                System.out.print("*");
            }
            System.out.print("\n"); 
    }
}

作业你写的代码有什么问题吗?在面试过程中遇到过。我不知道如何继续。如果给定意味着你只能在//逻辑所在的地方编写代码,那么你就会误解/错误地记住某些东西。这毫无意义,问题也不可能解决。精确的问题公式可能会有所帮助。如果这只是一个代码结构的例子-是的,答案是这样的,没有更多的解释了。我认为你需要研究递归和维护递归索引。然后事情可能会为你澄清。这就是我们所给予和要求的:@BatScream尖叫dafuq?哈哈,这个想法是让OP来解决的。我很高兴你不能投两次反对票。请看我使用的数字顺序。这与OP想要的不同;但愿我有机会再次否决投票,在你的代码中没有解释性评论,OP要求的。“不管怎样,你的记忆优化是好的。”巴特尖叫警员得到这个问题后去面试了。我想我在代码中已经说得够多了:代码在StackOverflow上缩进了4个空格。您可以选择代码并按Ctrl+K同时缩进所有代码。 1 2*2 3*3*3 3*3*3 2*2 1
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.println("Enter limit");
    int limit = s.nextInt();
    int start[] = new int[limit];
    int v = 1;
    for (int i=1; i<=limit; i++) {
        start[i-1] = v;
            for (int j=1; j<=i; j++) {
                System.out.print(v++);
                if(j==i)
                    continue;
                System.out.print("*");
            }
            System.out.print("\n");
    }
    for (int i=limit-1; i>=0; i--) {
        v=start[i];
            for (int j=i; j>=0; j--) {
                System.out.print(v++);
                if(j==0)
                    continue;
                System.out.print("*");
            }
            System.out.print("\n"); 
    }
}