编辑:在我的IDE中重新编写代码之后,今天是第八次,我的菜鸟犯了个错误,给我的输入一个错误的数据类型,该错误类型已得到修复,但我的输出仍然不正确。
关于我的目标的详细信息:进行更改时,您希望将分配给每个客户的硬币的数量减到最少。 好吧,假设出纳员欠客户一些零钱,并且出纳员的抽屉里有25美分硬币,10美分硬币,5美分镍币和1美分硬币。要解决的问题是确定要交付给客户的硬币以及每种硬币的数量。
预期结果:
欠款:0.41
4
实际结果:
欠款:0.41
3
#include <math.h>
#include <cs50.h>
#include <stdio.h>
int main (void)
{
float dollars;
int changeowed = 0;
do
{
dollars = get_float ("Change owed: ");
}
while (dollars < 0);
float cents = round(dollars * 100);
while (cents >= 25)
{
cents = cents - 25;
changeowed++;
}
while (cents > 10)
{
cents = cents - 10;
changeowed++;
}
while (cents > 5)
{
cents = cents - 5;
changeowed++;
}
while (cents > 1)
{
cents = cents - 1;
changeowed++;
}
printf("%i \n", changeowed);
}
答案 0 :(得分:0)
这里是问题:有4个循环,一个循环四分之一,一个角钱,一个镍币,一个便士币。第一个循环条件正确:
while (cents > 10)
while (cents > 5)
while (cents > 1)
其他三个不正确:
>=
这些都需要更改为使用>
代替Enter an Number
12
The factors are
1 2 3 4 6 12
。
答案 1 :(得分:0)
您可以轻松完成任何名义上的事情。使用整数类型。
int nominals[] = {100, 25, 10, 5, 1, 0};
void getNominals(double money, int *result)
{
unsigned ncents = money * 100.0;
int *nm = nominals;
while(*nm && ncents)
{
*result++ = ncents / *nm;
ncents %= *nm++;
}
}
int main(void)
{
int result[sizeof(nominals) / sizeof(nominals[0])] = {0};
getNominals(4.36, result);
for(size_t index = 0; nominals[index]; index++)
{
printf("%d = %d\n", nominals[index], result[index]);
}
}