Re: [問題] 請教有無方法簡化我的code

看板C_and_CPP作者時間13年前 (2010/10/05 09:31), 編輯推噓8(805)
留言13則, 7人參與, 最新討論串2/5 (看更多)
簡單的化簡 //pre:level,type分別控制三角形層級與種類 //post:印出三角形 void printTriangle(int level,int type){ int line,cnt; if (type < 1 || type > 3) { printf("你輸入了錯誤的三角形種類!!"); return; } for (line = 1; line <= level; line++) { for (cnt = 1; cnt <= line; cnt++) { if (type == 1) printf("1"); else if (type == 2) printf("%d",cnt); else /* type == 3 */ printf("%d",line); }//end for-cnt printf("\n"); }//end for-line }//end -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.120.42.179 ※ 編輯: lgen7604 來自: 122.120.42.179 (10/05 09:36)

10/05 09:42, , 1F
雖然化簡了,可是判斷次數好像太多。
10/05 09:42, 1F

10/05 09:50, , 2F
if 拿出去, type==1 target=1; type==2 target=cnt;
10/05 09:50, 2F

10/05 09:50, , 3F
我沒睡醒, 當我沒說 Orz
10/05 09:50, 3F

10/05 09:55, , 4F
好華麗的code
10/05 09:55, 4F

10/05 10:19, , 5F

10/05 10:29, , 6F
請問樓上您的code有compile過嗎?
10/05 10:29, 6F

10/05 10:49, , 7F
好像錯了XD,當做沒看到
10/05 10:49, 7F

10/05 11:06, , 8F
http://codepad.org/1m1gdy2F 改成這樣應該就沒問題了
10/05 11:06, 8F

10/05 11:41, , 9F
http://codepad.org/1oFozQiA 這樣也可以吧
10/05 11:41, 9F

10/05 12:25, , 10F
樓上的printf用的超技巧 XD
10/05 12:25, 10F

10/05 13:01, , 11F
人腦compiler算嘛?
10/05 13:01, 11F

10/05 13:01, , 12F
感謝各位的指教,不勝感激orz
10/05 13:01, 12F

10/05 14:49, , 13F
stupid0319 的 code 跟這篇等價
10/05 14:49, 13F
文章代碼(AID): #1Cgd_b9u (C_and_CPP)
文章代碼(AID): #1Cgd_b9u (C_and_CPP)