這邊來記錄一下使用Java和Python來輸出各個不同類型的三角形圖形
通常這類型的題目都是將幾個階層利用參數傳進來,再看問題要求是什麼,有些題目是要求用print的,有些則是回覆String字串就好1 2 3 4 5 EX :input : 3output: *
等腰三角形 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 static String getTri2(int n) { StringBuilder res = new StringBuilder() for(int i= 0 for(int j= 0 res.append(" " ) } for(int j= 0 res.append("*" ) } res.append("\n" ) } return res.toString() } // n= 3 // * // *** //***** static String getTri6(int n) { StringBuilder res = new StringBuilder() for(int i= 0 for(int j= 0 res.append(" " ) } for(int j= 0 if(j%4 = = 0 ) res.append("*" ) else res.append(" " ) } res.append("\n" ) } return res.toString() } // n= 5 // * // * * // * * * // * * * * // * * * * * static String getTri(int n) { StringBuilder res = new StringBuilder() for(int i= 0 for(int j= 0 res.append(" " ) } if(i= = n-1 ) { for(int j= 0 res.append("*" ) } else { for(int j= 0 if(j%4 = = 0 ) res.append("*" ) else res.append(" " ) } } res.append("\n" ) } return res.toString() } // n= 5 // * // * * // * * // * * // *****************
python 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 n = eval(input()) for i in range(n): for a in range(n-i-1): print (' ' ,end ='' ) for b in range(2*i+1): print ('*' ,end ='' ) print ('\n' ,end ='' ) def pyramid(n): for i in range(n+1): print (' ' *(n-i)+'*' *(2*i-1)) n = eval(input()) pyramid(n) n = eval(input()) for i in range(5): print (" " *(n-i),end ="" ) print (" * " *(i+1))
倒等腰三角形 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 static String getTri7(int n) { StringBuilder res = new StringBuilder(); for (int i=n;i>0 ;i--) { for (int j=0 ;j<n-i;j++) { res.append (" " ); } for (int j=0 ;j<2 *i-1 ;j++) { res.append ("*" ); } res.append ("\n" ); } return res.toString(); }
python 1 2 3 4 5 6 7 8 9 10 11 def reverse_pyramid (n ): for i in range (n,0 ,-1 ): print (' ' *(n-i)+'*' *(2 *i-1 )) n = eval (input ()) reverse_pyramid(n)
直角三角形(直角左下) java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 static String getTri2(int n ) { StringBuilder res = new StringBuilder() ; for (int i=0 ;i<n;i++) { for (int j=0 ;j<i+1 ;j++) { res.append("*" ); } res.append("\n" ); } return res.to String() ; }
python 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 t = eval (input ()) for v in range (t): for i in range (v+1 ): print ('*' ,end='' ) print ('\n' ,end='' ) n = eval (input ()) for i in range (n+1 ): print (i*'*' )
直角三角形(直角右下) java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 static String getTri4(int n) { StringBuilder res = new StringBuilder(); for (int i=0 ;i<n;i++) { for (int j=0 ;j<n-i-1 ;j++) { res.append (" " ); } for (int j=0 ;j<i+1 ;j++) { res.append ("*" ); } res.append ("\n" ); } return res.toString(); }
python 1 2 3 4 5 6 7 8 9 10 11 12 13 a = eval(input()) for v in range(a): for j in range(a-v-1): print (' ' ,end ='' ) for i in range(v+1):#根據外層行號,透過加1可以剛好等於星數 print ('*' ,end ='' ) print ('\n' ,end ='' )
直角三角形(直角左上) python 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 b = eval (input ()) for v in range (b): for i in range (b-v): print ('*' ,end='' ) print ('\n' ,end='' ) n = eval (input ()) for i in range (n): print ((n-i)*'*' )
直角三角形(直角右上) java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 static String getTri3(int n) { StringBuilder res = new StringBuilder(); for (int i=n;i>0 ;i--) { for (int j=0 ;j<n-i;j++) { res.append (" " ); } for (int j=0 ;j<i;j++) { res.append ("*" ); } res.append ("\n" ); } return res.toString(); }
python 1 2 3 4 5 6 7 8 9 10 11 12 13 14 a = int(input()) for v in range(a): for i in range(v): print (' ' ,end ='' ) for j in range(a-v) : print ('*' ,end ='' ) print ('\n' ,end ='' )
菱形 python 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 rows = eval(input()) for i in range(1, rows): for b in range(rows - i): print (' ' ,end = '' ) for k in range(i*2 -1): if k ==0 or k ==i*2-2: print ('*' ,end = '' ) else : print (' ' ,end = '' ) print () for i in range(rows): for b in range(i): print (' ' ,end = '' ) for k in range((rows-i)*2 -1): if k ==0 or k ==(rows-i)*2-2: print ('*' ,end = '' ) else : print (' ' ,end = '' ) print ()
參考:
Java Code To Create Pyramid and Pattern