我想了解問題集給出的代碼并嘗試從那里弄清楚。我什至無法開始解決問題,因為我不理解給出的代碼。我很難理解所有這些退貨操作員以及計算硬幣美分、硬幣、鎳幣和便士的計算方法。
當我在 todo 和 return 部分中撰寫代碼時,代碼應該提示用戶詢問欠了多少美分,并輸出“Cents owed:”。它還應該使用貪婪演算法回傳所欠美分所需的最少硬幣數量。
Main 已實作,但呼叫了未實作的函式,包括get_cents()、calculate_quarters()、calculate_dimes()、calculate_nickels()和calculate_pennies().
我有一個使用除法來查找美分的想法。我會將用戶輸入的美分除以 25,因為這是該季度中最大的。然后它將回傳一個正整數。此外,我會欠下美分 - 25。我會用這個數字除以 10 作為一角錢,以找出我需要使用多少一角錢并回傳一個正整數。我會繼續向下移動,直到達到便士。我不知道我將如何實作這一點,但我的思考程序是否在正確的地方?
#include <cs50.h>
#include <stdio.h>
int get_cents(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int cents);
int calculate_pennies(int cents);
int main(void)
{
// Ask how many cents the customer is owed
int cents = get_cents();
// Calculate the number of quarters to give the customer
int quarters = calculate_quarters(cents);
cents = cents - quarters * 25;
// Calculate the number of dimes to give the customer
int dimes = calculate_dimes(cents);
cents = cents - dimes * 10;
// Calculate the number of nickels to give the customer
int nickels = calculate_nickels(cents);
cents = cents - nickels * 5;
// Calculate the number of pennies to give the customer
int pennies = calculate_pennies(cents);
cents = cents - pennies * 1;
// Sum coins
int coins = quarters dimes nickels pennies;
// Print total number of coins to give the customer
printf("%i\n", coins);
}
int get_cents(void)
{
// TODO
return 0;
}
int calculate_quarters(int cents)
{
// TODO
return 0;
}
int calculate_dimes(int cents)
{
// TODO
return 0;
}
int calculate_nickels(int cents)
{
// TODO
return 0;
}
int calculate_pennies(int cents)
{
// TODO
return 0;
}
uj5u.com熱心網友回復:
我相信您是對的,但對于可能沒有徹底閱讀您的問題的其他人,我只想重申:
這個想法是每個函式應該回傳最多的硬幣(特定型別)以獲得余額,但不要超過。例如,calculate_quarters(76)應該回傳,3因為 3 個季度賺 75 美分,而下一個增量必須是 100 美分。
這是編碼部分:在 C 中,除以整數總是回傳一個整數,所以76 / 3不會回傳 25.3333... 因為 25.3333... 不是整數。相反,小數將被截斷,因此76 / 3計算結果為3. 請注意,這將始終向下舍入數字。
在您的情況下,此功能使編程更容易。cents您可以簡單地除以硬幣的價值,而不是明確四舍五入。以下是完整代碼的示例:
#include <stdio.h>
int get_cents(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int cents);
int calculate_pennies(int cents);
int main(void) {
// Ask how many cents the customer is owed
int cents = get_cents();
// Calculate the number of quarters to give the customer
int quarters = calculate_quarters(cents);
cents = cents - quarters * 25;
// Calculate the number of dimes to give the customer
int dimes = calculate_dimes(cents);
cents = cents - dimes * 10;
// Calculate the number of nickels to give the customer
int nickels = calculate_nickels(cents);
cents = cents - nickels * 5;
// Calculate the number of pennies to give the customer
int pennies = calculate_pennies(cents);
cents = cents - pennies * 1;
// Sum coins
int coins = quarters dimes nickels pennies;
// Print total number of coins to give the customer
printf("%i\n", coins);
}
int get_cents(void) {
int cents = 0;
scanf("%d", ¢s);
return cents;
}
int calculate_quarters(int cents) { return cents / 25; }
int calculate_dimes(int cents) { return cents / 10; }
int calculate_nickels(int cents) { return cents / 5; }
// NOTE: "cents / 1" is the same as "cents", so no need for division here
int calculate_pennies(int cents) { return cents; }
PS:為了方便,我也寫了get_cents()。該功能與此答案無關。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/516691.html
標籤:Ccs50
上一篇:當我洗掉“elseif”中的條件時,為什么我的程式回傳“分段錯誤(核心轉儲)”?
下一篇:結構值無法輸入文本
