2015-10-09 83 views
0

該查詢用於編寫一個Java方法,該方法根據給定的輸入(字母數量,三角形的每邊)輸出以下三角形, 。二維數組(Java) - 按循環順序打印帶有字母的三角形

public void triangle(int side); 

輸出預期: 三角形(3)

* * A * * 
* F * B * 
E * D * C 

三角形(4)

* * * A * * * 
* * I * B * * 
* H * * * C * 
G * F * E * D 

我想出來的,其不只是一個方法,但代碼我用有限的經驗寫了更多for循環。你們中的任何人都可以查看我的代碼,併爲相同的問題提出建議或優化代碼?

public void triangle(int input) { 
     int x = input; 
     int y = 2 * input - 1; 
     int mid = y/2; 
     char character = 'A'; 

     String[][] partitionArray1 = new String[x][y]; 
      \\Following for loop will add letters on the side-1 
     for (int i = 0; i < x; i++) { 
      for (int j = 0; j < y; j++) { 
       if (i + mid == j) { 
        partitionArray1[i][j] = "" + character++; 
       } else { 
        partitionArray1[i][j] = "*"; 
       } 
      } 
     } 
      \\Following for loop will add letters on the side-2 (horizontal)  
     for (int j = y - 2; j >= 0; j--) { 
      j--; 
      if (j >= 0) { 
       partitionArray1[x - 1][j] = "" + character++; 
      } else { 
       break; 
      } 
     } 
      \\Following for loop will add letters on the side-3 
     for (int i = x - 2; i >= 0; i--) { 
      for (int j = 0; j < y; j++) { 
       if ((i == mid - j) && (j < mid)) { 
        partitionArray1[i][j] = "" + character++; 
       } 
      } 
     } 

     for (int i = 0; i < x; i++) { 
      for (int j = 0; j < y; j++) { 
       System.out.print(partitionArray1[i][j] + ""); 
      } 
      System.out.println(); 
     } 
    } 

是否有算法可以回答這樣的問題?

回答

1

嘗試:

public static void triangle(int n) { 
    for (int i = 0; i < n; ++i) { 
     if (i == n-1) { 
      for (int j = 0; j < 2*n-1; ++j) 
       if (j % 2 == 0) 
        System.out.printf("%c ", 'A' + 2*n-2-j/2); 
       else 
        System.out.printf("* "); 
      System.out.println(); 
      break; 
     } 

     for (int j = 0; j < 2*n-1; ++j) { 
      if (j == n-1+i) 
       System.out.printf("%c ", 'A'+i); 
      else if (j == n-1-i) 
       System.out.printf("%c ", 'A'+3*n-i-3); 
      else 
       System.out.printf("* "); 

     } 
     System.out.println(); 
    } 
} 

的想法是從其他打印列#n分開。該行的其餘部分恰好有兩個元素(除了第一個元素是一個退化的情況)相對於中心對稱。

triangle(9); 
* * * * * * * * A * * * * * * * * 
* * * * * * * X * B * * * * * * * 
* * * * * * W * * * C * * * * * * 
* * * * * V * * * * * D * * * * * 
* * * * U * * * * * * * E * * * * 
* * * T * * * * * * * * * F * * * 
* * S * * * * * * * * * * * G * * 
* R * * * * * * * * * * * * * H * 
Q * P * O * N * M * L * K * J * I 
+0

相當類似上述的解決做了,但愛的代碼最小化的理念! –

+0

他的解決方案可能類似,它的速度快了7倍 –

+0

完全同意這一點@TaharBakir –

1

我很無聊,所以我用一個陣列

public static void mimi(int size){ 
    int sizetab=size*size*2; 
    char res[] = new char[sizetab]; 
    Arrays.fill(res,'*'); 


    int pos=size-1; 
    int JumpGoRight=(2*size)+1; 
    int JumpGoLeft=2; 
    char letter='A'; 
    boolean changed = false; 

    int nbLetters = size -1; 
    for (int s=size;s>1;s--) 
    nbLetters+=2; 

    int i=0; 
    while(i<(size-1)){ 
    res[pos]=letter++; 
    pos+=JumpGoRight; 
    i++; 
    } 

    int limit=(sizetab-(size*2))+1; 
    while(i<nbLetters){ 
     res[pos]=letter++; 
     pos-=JumpGoLeft; 
     if(!changed && (pos<limit)){ 
     JumpGoLeft=(size*2)-1 ; 
      changed=true; 
     } 
     i++; 
    } 


    int index = 0; 
    int doublesize=size*2; 
    for(char c: res){ 
    if(((++index)%doublesize)==0) 
     System.out.print('\n'); 
    else 
     System.out.print(c); 
    } 
}