伊莉討論區

標題: 列印三角形之大三角型 [打印本頁]

作者: hl35193    時間: 2014-6-10 10:51 PM     標題: 列印三角形之大三角型

我想印一個 以星號* 印出等腰三角形
以3階層為例  第一階 "1顆"  第二階 "3顆"  第三階 "5顆"以上這部分不是問題
但我想再把上面印出來的三角形為一個單位(小三角形)


之後 以同樣的規則 在印出一個大三角形
一樣以3階層為例
第一階 "1個小三角"  第二階 "3個小三角"  第三階 "5個小三角"



有嘗試將小三角寫在函數裡  之後以同樣for迴圈方式印出函數
發覺 它是垂直印出

懇請高手解答...


補充內容 (2014-6-11 12:25 PM):
忘記註明 大三角也須呈現等腰
作者: theloserbm    時間: 2014-6-11 12:46 AM

本帖最後由 theloserbm 於 2014-6-11 12:47 AM 編輯

你可以考慮在function裡接受一個parameter, 讓它列印一排有n個小三角

triangle(1,5);
triangle(3,5);
triangle(5,5);
作者: snowflying    時間: 2014-6-13 04:33 AM

以 3 階舉例,3個小三角形的邊,就與上面的三角形不相接了
要如何等腰?

            *   (0,0)
          ***  (1,-1)
        ***** (2,-2)
    *      *        * (5,-3)  <--- 根本沒接在一起
  ***   ***    ***
***************


作者: hl35193    時間: 2014-6-14 02:26 PM

snowflying 發表於 2014-6-13 04:33 AM
以 3 階舉例,3個小三角形的邊,就與上面的三角形不相接了
要如何等腰?

sorry  大三角形 可以不用等腰   
作者: theloserbm    時間: 2014-6-14 05:01 PM

本帖最後由 theloserbm 於 2014-6-14 05:03 PM 編輯
hl35193 發表於 2014-6-14 02:26 PM
sorry  大三角形 可以不用等腰

我太失望了, 我還以為你要做這樣的

          *
         ***
        *****
       *     *
      ***   ***
     ***** *****
    *     *     *
   ***   ***   ***
  ***** ***** *****


作者: snowflying    時間: 2014-6-14 05:25 PM

參考看看,若有錯就算了
我只測試一次
反正就是經過經過數學程序計算就是了

  1. public static void big_tri(int height , int small_height)
  2. {
  3.     int h = 1;
  4.         
  5.     do
  6.     {
  7.         small_tri(small_height , (height - h) * (2 * small_height - 1) , 2 * h - 1);
  8.         pad_line(small_height);
  9.     }while(h++ < height);
  10. }
  11. public static void small_tri(int height , int pad , int num)
  12. {
  13.     for(int h = 1 ; h <= height ; ++h)
  14.     {
  15.         pad_space(pad);
  16.         for(int n = 1 ; n <= num ; ++n)
  17.         {
  18.             pad_space(height - h);
  19.             for(int star = 1 ; star <= 2 * h - 1 ; ++star)
  20.                 System.out.print('*');
  21.             pad_space(height - h);
  22.         }
  23.         System.out.println();
  24.     }
  25. }
  26. public static void pad_line(int n)
  27. {
  28.     while(n-- > 0)
  29.         System.out.println();
  30. }
  31. public static void pad_space(int n)
  32. {
  33.     while(n-- > 0)
  34.         System.out.print(' ');
  35. }
複製代碼




作者: theloserbm    時間: 2014-6-14 06:27 PM

本帖最後由 theloserbm 於 2014-6-14 06:36 PM 編輯

堅持1,2,3版本
並無恥抄襲var和func名字
  1. public class Triangle {
  2.   public static void main(String[] args) {
  3.     int small_height = Integer.parseInt(args[0]);
  4.     int height = Integer.parseInt(args[1]);
  5.    
  6.     big_tri(small_height, height);
  7.   }
  8.   
  9.   public static void big_tri(int small_height, int height) {
  10.     for (int i=1; i<=height; i++) {
  11.       small_tri(small_height, height, i);
  12.     }
  13.   }
  14.   
  15.   public static void small_tri(int small_height, int height, int line) {
  16.     for (int i=1; i<=small_height; i++) {
  17.       // pre space
  18.       print_space((height - line) * small_height);
  19.       
  20.       for (int j=1; j<=line; j++) {
  21.         print_space(small_height - i);
  22.         print_star(1 + (i-1)*2);
  23.         print_space(small_height - i + 1);
  24.       }
  25.       System.out.println();
  26.     }
  27.   }
  28.   
  29.   public static void print_space(int count) {
  30.     for (int i=1; i<=count; i++) {
  31.       System.out.print(' ');
  32.     }
  33.   }
  34.   
  35.   public static void print_star(int count) {
  36.     for (int i=1; i<=count; i++) {
  37.       System.out.print('*');
  38.     }
  39.   }
  40. }
複製代碼
結果多美妙
  1. C:\Users\Icecool\Desktop>java Triangle 3 5
  2.               *
  3.              ***
  4.             *****
  5.            *     *
  6.           ***   ***
  7.          ***** *****
  8.         *     *     *
  9.        ***   ***   ***
  10.       ***** ***** *****
  11.      *     *     *     *
  12.     ***   ***   ***   ***
  13.    ***** ***** ***** *****
  14.   *     *     *     *     *
  15. ***   ***   ***   ***   ***
  16. ***** ***** ***** ***** *****
複製代碼





歡迎光臨 伊莉討論區 (http://ww2.eyny.com/) Powered by Discuz!