在“ C”中的贪婪算法被称为“ C”。 [英] Greedy Algorithm in "C"
本文介绍了在“ C”中的贪婪算法被称为“ C”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚开始学习 C语言
。我写了这段C代码来实现贪婪算法
,我不知道我在这段代码中犯了什么错误,该代码看起来不错,但无法正常工作。有人可以帮我修复此代码吗?
I'm just start learning C language
. I wrote this C code to implement Greedy algorithm
I don't know what mistake I've made with this code, that code seems fine but its not working as I expected. Can anybody help me to fix this code?
int main(void) {
float amount = 0;
int cents = 0;
int count = 0;
int amount_left = 0;
amount = .30;
cents = (int)round(amount * 100);
printf("%d", cents);
amount_left = cents;
while (cents - 25 >= 0) {
count = count + 1;
amount_left = cents - 25;
}
while (amount_left - 10 >= 0) {
count = count + 1;
amount_left = amount_left - 10;
}
while (amount_left - 5 >= 0) {
count = count + 1;
amount_left = amount_left - 5;
}
while (amount_left - 1 >= 0) {
count = count + 1;
amount_left = amount_left - 1;
}
printf("You get %d coins\n", count);
}
推荐答案
关于代码:
- 您在第一个循环中使用
cents
时, code> amount_left ,在第一个循环中,如果需要多次迭代,则结果将是不正确的。 - 建议更好将
amount_left-10> = 0
更改为amount_left> = 10
。 - 最后一个
printf
语句最有可能(按文字显示)用于打印通过提供的金额获得的硬币数量。
- You are using in the first loop
cents
when there would beamount_left
, in the case of the first loop if it require more that one iteration, the result would be incorrect. - As recommended is better to change
amount_left - 10 >= 0
byamount_left >= 10
. - The final
printf
statement most probably (by the text) is for printing the count of coin gained by the amount provided.
代码:
#include <stdio.h>
#include <math.h>
int main(void) {
float amount = 0;
int cents = 0;
int count = 0;
int amount_left = 0;
amount = .30;
cents = (int)round(amount * 100);
printf("%d\n", cents);
amount_left = cents;
while (amount_left >= 25) {
count++;
amount_left -= 25;
}
while (amount_left >= 10) {
count++;
amount_left -= 10;
}
while (amount_left >= 5) {
count++;
amount_left -= 5;
}
while (amount_left >= 1) {
count = count + 1;
amount_left -= 1;
}
printf("You get %d coins\n", count);
}
使用公式: initial_amount
= 硬币价值
* 已使用硬币
+ amount_left
Using the formula: initial_amount
= coin value
* coin used
+ amount_left
这可以用C写成:
-
initial_amount
/硬币价值
=已使用硬币
-
初始金额
%硬币价值
=amount_left
initial_amount
/coin value
=coin used
initial_amount
%coin value
=amount_left
更优化的解决方案:
#include <stdio.h>
#include <math.h>
int main(void) {
float amount = 0;
int cents = 0;
int count = 0;
int amount_left = 0;
amount = .30;
cents = (int)round(amount * 100);
printf("%d\n", cents);
amount_left = cents; // beginning with 30 cents
count += amount_left / 25; // 30 / 25 = 1, one 25 cent coin
amount_left %= 25; // 30 % 25 = 5, left with 5 cents
count += amount_left / 10; // 5 / 10 = 0 no coin used
amount_left %= 10; // 5 % 10 = 5 left the same 5 cents
count += amount_left / 5; // 5 / 5 = 1 one 5 cent coin
amount_left %= 5; // 5 % 5 = 0 left with 0 cents
count += amount_left; // not needed 1 cent coins.
printf("You get %d coins\n", count);
}
注意:
- 不需要
while循环
操作17/5 = 3
用整数运算在C
和17%5 = 2
中。 - 使用此功能对于价值
N
的硬币,数量/ N
硬币计数(可以为0,例如:amount = 9
和N = 10
,9/10 = 0
),剩余的金额为金额%N
。 - 最后一种情况(对于1的硬币)始终为
amount = 0
。
- There is no need to
while loop
operation17 / 5 = 3
in integers arithmetic inC
and17 % 5 = 2
. - Using this you use for a coin of value
N
,amount / N
coins count (could be 0, eg:amount = 9
andN = 10
,9/10 = 0
in integer division) and the amount left isamount % N
. - The last case (for coin of 1) always left
amount = 0
.
这篇关于在“ C”中的贪婪算法被称为“ C”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文