2014-09-02 166 views
-1

我碰巧出现在测试中,并得到以下问题。我无法弄清楚如何继续。该场景是编写打印以下与相应N. Java程序如果假设N = 3,它必须有2个* N行和输出必须是,java中的镜像三角形生成

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 
} 

我不明白为什么会这样宣布的行,2行是否应该与N.出现不同请解释的逻辑。

+1

作业?你写的代码的任何问题? – VinayVeluri 2014-09-02 13:36:16

+0

在面试过程中遇到了这个问题。我不知道继续。 – Chandni 2014-09-02 13:38:01

+0

如果'given'意味着你可以只在'//逻辑'的地方写你的代码,而不是你误解/误解了某些东西。这没有意义(而且问题是不可能的)。确切的问题制定可能会有帮助。如果这只是代码结构的一个例子 - 是的,答案会看起来像这样,没什么可解释的。 – Deltharis 2014-09-02 13:39:55

回答

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

输出:对对于n = 4:

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

谢谢!解决了! – Chandni 2014-09-04 05:34:27

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

Down投票不显示星号;-) – BatScream 2014-09-02 14:59:57

+0

@BatScream * Screams * dafuq?大声笑这个想法是让OP找出答案。我很高兴你不能两次投反对票。查看我使用的数字序列。它与OP所要求的不同) – 2014-09-02 15:18:54

+0

希望我有一个选项可以再次投票,在代码中没有解释注释,OP已经要求。无论如何,你的记忆优化是好的。 – BatScream 2014-09-02 15:25:18

0

是否有递归明确要求?这是由任何地方没有提到的问题的结构所暗示的。

int rows=2就是一个例子可能是,你不能做任何事情,“聪明”的使用指针一样问题的目的...

我也会认为你是不允许使用值“> 100 '这样你就可以超载价值的意义 - 同样适用于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(); 
} 
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 
+0

代码在StackOverflow上缩进4个空格。您可以选择代码并按下Ctrl + K同时缩进所有代码。 – 2015-03-28 16:09:18

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