java中的镜像三角生成

时间:2014-09-02 13:31:27

标签: java loops

我碰巧出现了测试,并得到以下问题。我无法弄清楚如何继续。方案是编写一个java程序,用相应的N打印以下内容。如果假设N = 3,它必须有2 * N行,输出必须是,

1
2 * 3
4 * 5 * 6
4 * 5 * 6
2 * 3
1

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

public static void main(String[] args){
    int rows=2;
    mirrorTriangle(rows);
}
public void mirrorTriangle(int n){
    //Logic 
}

我不明白为什么如果行应该随N变化,那些行被声明为2。请解释逻辑。

5 个答案:

答案 0 :(得分:1)

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--
}  

输出:

1
12
123
123
12
1

代码非常明显。您只需N行但打印2N因为,等待它......对称。如果你有6行,前3个是新的,而其他3个只是镜像图像,那么为什么要再次打印它们时会浪费内存空间呢?

答案 1 :(得分:1)

请找到问题的解决方案,并附上解释说明。

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();
        }
    }

o:p代表n = 4:

1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1

答案 2 :(得分:0)

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

int rows=2可能就是一个例子,出于问题的目的,你不能像使用指针一样“聪明”......

我还假设您不允许使用值'&gt; 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();
}

答案 3 :(得分:0)

试试这个新代码:

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

答案 4 :(得分:0)

我认为这是一个比选择的更好,更简单的解决方案。

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"); 
    }
}