#include <stdio.h>
int calculate_pizza_share(int number_of_people);
int main() {
int num_of_people;
printf("how many people are shairng this pizza? ");
scanf("%d", &num_of_people);
calculate_pizza_share(num_of_people);
return 0;
}
int calculate_pizza_share(int number_of_people) {
if (number_of_people == 1) {
printf("You get %d slice(s) each\n", 8/number_of_people);
} else if (number_of_people == 2) {
printf("you get 4 slice(s) each\n");
} else if (number_of_people == 3) {
printf("you get 2 slice(s) each\n");
} else if (number_of_people == 4) {
printf("you get 2 slice(s) each\n");
} else if (number_of_people == 5) {
printf("you get one slice(s) each\n");
} else if (number_of_people == 6) {
printf("you get one slice(s) each\n");
} else if (number_of_people == 7) {
printf("you get one slice(s) each\n");
} else if (number_of_people == 8) {
printf("you get one slice(s) each\n");
}
}
-4
A
回答
2
如何一行:
printf("You get %d slice(s) each\n",8/number_of_people);
你沒有得到的 「一」,而不是 「1」,但我認爲簡單將覆蓋。
的代稱,整個if/else
「階梯」 可以被替換爲switch/case
UPDATE:
固定我與編輯器的問題,代碼現在在該職位。
好的,這裏有兩種方法。我認爲單行仍然是最好的,但我已經取代了梯子,用switch
:
#include <stdio.h>
int calculate_pizza_share(int number_of_people);
int
main()
{
int num_of_people;
printf("how many people are shairng this pizza? ");
scanf("%d", &num_of_people);
calculate_pizza_share(num_of_people);
return 0;
}
int
calculate_pizza_share(int number_of_people)
{
#ifndef SWITCH
printf("You get %d slice(s) each\n", 8/number_of_people);
#else
switch (number_of_people) {
case 5:
case 6:
case 7:
case 8:
printf("You get one slice each\n");
break;
default:
printf("You get %d slice(s) each\n", 8/number_of_people);
break;
}
#endif
}
+0
請注意,如果你已經把代碼放在這個頁面上的代碼塊中,我會發佈一個'switch/case'例子,但是我沒有[並且很少有人會想要手工輸入。] –
0
有時候if/else if
鏈條中去,產生涉及數字量的英語短語尤其是在正確的道路。但是想辦法減少條件的數量。在這個例子中,你真的只有三個(有效的)條件
- 1人:獲得了整個比薩
- 2,3或4人:整數運算滴師的小數部分,所以
8/number_of_people
會得到正確的答案,所有這些 - 5,6,7,8:整數除法給出了正確的答案,但消息是不同的,所以對待這些作爲一個單獨的條件
此外,你應該總是假設用戶將輸入無意義(當有機會時)。所以,你需要從scanf
檢查返回值,並準備處理是小於1且大於8
考慮到這一點的數字,這裏的代碼如下所示:
#include <stdio.h>
void calculate_pizza_share(int number_of_people)
{
if (number_of_people < 1)
printf("You need some people to eat that pizza\n");
else if (number_of_people == 1)
printf("You get the whole pizza\n");
else if (number_of_people <= 4)
printf("You get %d slices each\n", 8/number_of_people);
else if (number_of_people <= 8)
printf("You get one slice each\n");
else
printf("You need to order more pizza\n");
}
int main(void)
{
int num_of_people;
printf("how many people are sharing this pizza? ");
if (scanf("%d", &num_of_people) != 1)
printf("Was hoping for a number between 1 and 8\n");
else
calculate_pizza_share(num_of_people);
}
相關問題
- 1. Mercurial - 我可以做得更好嗎?
- 2. C#泛型 - 這可以做得更好嗎?
- 3. 瞭解Java泛型 - 這可以做得更好嗎?
- 4. 這個JS時間碼會工作嗎?我可以做得更好嗎?
- 5. 奇怪的PHP字符串比較效果,這可以做得更好嗎?
- 6. 我可以比二進制搜索做得更好嗎?
- 7. 我可以做得更簡單嗎?
- 8. 這可以做得更容易嗎? Java初學者的問題
- 9. 這個循環數據引用可以設計得更好嗎?
- 10. 這可以寫得更好嗎? PHP代碼
- 11. 這是一個足夠好的抽象,還是我可以做得更好?
- 12. 可以jquery這樣做嗎?
- 13. Java可以這樣做嗎?
- 14. 可以這樣做嗎?
- 15. 可以這樣做嗎?
- 16. 縮小器可以做到嗎? (....這是一個好主意嗎?)
- 17. 這樣做是「更好的方式」嗎?
- 18. 這可以寫得更好嗎?根據以前的結果調用函數
- 19. 這可以做得更漂亮嗎?匹配字符串到大小寫字段
- 20. PHP我可以做這個循環嗎?
- 21. .NET AppDomains可以這樣做嗎?
- 22. 憑證 - 我可以這樣做嗎?
- 23. Xaml - '計算值' - 可以這樣做嗎?
- 24. CouchDB可以做到這一點嗎?
- 25. drupal可以做到這一點嗎?
- 26. 可以這樣做嗎? (多列ul)
- 27. [「var」+1] = someValue - 可以這樣做嗎?
- 28. 我可以做這樣的事嗎?
- 29. NSMutableArrays - 我可以這樣做嗎?
- 30. 我可以做這個JavaScript循環嗎?
莫非你請添加代碼? –
'printf(「你得到%d片(s)每個\ n」,8/number_of_people);' –
把代碼放在問題中,你爲什麼要發佈一個鏈接到你的編輯器的截圖? –