[問題] 關於for迴圈問題

看板C_and_CPP (C/C++)作者 (幻想神)時間2年前 (2022/04/22 21:00), 2年前編輯推噓2(2011)
留言13則, 4人參與, 2年前最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) w10 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) devc++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 用for數字迴圈畫三角型 可是超過一定的數字就不會寫了 行數超過10就用星星取代 卡很久都跑不出來 初學者 餵入的資料(Input): #include<stdio.h> int main() { int i,j,k; printf("輸入行數 = "); scanf("%d",&k); for(i=1;i<=k;i++){ for(j=1;j<=i;j++) { printf("%d",i); } printf("\n"); } } 預期的正確結果(Expected Output): 1 22 333 4444 55555 666666 7777777 88888888 999999999 ********** 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.252.100.153 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1650632428.A.5A7.html

04/22 21:05, 2年前 , 1F
"%c", i > 9 ? '*' : (i + '0')
04/22 21:05, 1F
結果出來沒錯 可是不懂原理 可以解釋一下嗎? ※ 編輯: lovesaber (111.252.100.153 臺灣), 04/22/2022 21:08:34

04/22 21:08, 2年前 , 2F
阿都寫C++了 control variable(i,j) 就放for裡面吧
04/22 21:08, 2F

04/22 21:39, 2年前 , 3F
印出字元 如果 i > 9 輸出 * 反之則 '0'~'9'
04/22 21:39, 3F

04/22 21:40, 2年前 , 4F
如果你不知道?: 可以查conditional operator
04/22 21:40, 4F

04/22 21:40, 2年前 , 5F
如果你不知道 i + '0' 去理解一下ASCII
04/22 21:40, 5F

04/23 13:22, 2年前 , 6F
新手不懂ternary也不懂ascii的話土炮一點用 if else?
04/23 13:22, 6F

04/23 13:22, 2年前 , 7F
if (i > 9) {
04/23 13:22, 7F

04/23 13:22, 2年前 , 8F
// print *
04/23 13:22, 8F

04/23 13:22, 2年前 , 9F
} else {
04/23 13:22, 9F

04/23 13:22, 2年前 , 10F
// print 1-9
04/23 13:22, 10F

04/23 13:22, 2年前 , 11F
}
04/23 13:22, 11F

04/24 22:46, 2年前 , 12F
2樓,其實 C11 好像就有 int 宣告在 for loop 裡面了
04/24 22:46, 12F

04/25 01:37, 2年前 , 13F
C99,好像就有了
04/25 01:37, 13F
文章代碼(AID): #1YOgRiMd (C_and_CPP)
文章代碼(AID): #1YOgRiMd (C_and_CPP)