---
tags: DICE C
---
6-5 預算夠不夠?
===
> [name=CHAWTeam]
---
目錄:[DICE C語言程式破解](https://hackmd.io/@CHAWTeam/DiceC)
上一篇:[6-4 輸出絕對值](https://hackmd.io/@CHAWTeam/DiceC-6-4)
下一篇:[7-1 比大小](https://hackmd.io/@CHAWTeam/DiceC-7-1)
---
題目
---
```!
小梅想要購物,她的預算只有3000元,請幫她算算想購買的商品組合,是否會超過預算?
以編號來代表商品,一次會輸入2項數字編號,請把所有商品價格相加,判斷是否超過預算,
分別輸出「符合預算」或「超出預算」。
商品有下列:
1.椅子 2300
2.書籍 800
3.帽子 500
4.櫃子 1500
因為此題輸出有多種狀況,故需將程式碼複製2次,都判斷正確才給分,每個輸出都要換行。
輸入範例1:
1 4
輸出範例1:
超出預算
輸入範例2:
2 3
輸出範例2:
符合預算
```
程式碼
---
```c=
#include <stdio.h>
int main()
{
int a, b, e, f;
scanf("%d %d", &a, &b);
if (a == 1)
e = 2300;
if (a == 2)
e = 800;
if (a == 3)
e = 500;
if (a == 4)
e = 1500;
if (b == 1)
f = 2300;
if (b == 2)
f = 800;
if (b == 3)
f = 500;
if (b == 4)
f = 1500;
if (e + f < 3000)
printf("符合預算\n");
if (e + f > 3000)
printf("超出預算\n");
scanf("%d %d", &a, &b);
if (a == 1)
e = 2300;
if (a == 2)
e = 800;
if (a == 3)
e = 500;
if (a == 4)
e = 1500;
if (b == 1)
f = 2300;
if (b == 2)
f = 800;
if (b == 3)
f = 500;
if (b == 4)
f = 1500;
if (e + f < 3000)
printf("符合預算\n");
if (e + f > 3000)
printf("超出預算\n");
return 0;
}
```
:::success
好的排版,可以讓程式碼淺顯易懂!
:::
輸出
---
```!
1 4
超出預算
2 3
符合預算
```
---
[查看我們在HackMD上的所有筆記](https://hackmd.io/@CHAWTeam)
目錄:[DICE C語言程式破解](https://hackmd.io/@CHAWTeam/DiceC)
上一篇:[6-4 輸出絕對值](https://hackmd.io/@CHAWTeam/DiceC-6-4)
下一篇:[7-1 比大小](https://hackmd.io/@CHAWTeam/DiceC-7-1)
---
{%hackmd Iiu5mOixR7yWkPHKCkabBg %}
<iframe class="LikeCoin" height="235" src="https://button.like.co/in/embed/chawteam/button?referrer=https://hackmd.io/@CHAWTeam/DiceC-6-5" width="100%"></iframe>
---
{%hackmd i1nMRrZcTFmTvoF897K9zg %}