輸入車速(整數),判斷是否超速:
Speed: X - Speeding。Speed: X - OK。1 /** 2 * B1-分支-01. 超速判斷(10) 3 * C語言實現 4 * 測試通過 5 */ 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 10 int main() 11 { 12 int speed; 13 14 scanf("%d", &speed); 15 if (speed < 0 || speed > 500) 16 exit(1); 17 18 if (speed > 60) 19 printf("Speed: %d - Speeding\n", speed); 20 else 21 printf("Speed: %d - OK\n", speed); 22 23 return 0; 24 }
exit(1) 異常退出,返回非零狀態碼表明輸入錯誤。if-else 完成核心邏輯,清晰簡潔。%d 佔位符直接嵌入車速值,輸出固定格式字符串。if-else if-else 或 switch(配合區間技巧)。float/double 並調整比較邏輯。