CS50 pset1 的奇怪总和值 - 贪心挑战
Weird sum value for CS50 pset1- Greedy challenge
我已经在 PSET1 的 CS50 中完成了贪婪挑战的代码
不过,硬币的总数returns相当多。当我进一步查看时,我的镍币数量似乎很大(4381344 个硬币换 0.48 美分)。
叫我傻瓜,但我已经为此烦恼了一段时间。谁能指出原因?
#include <cs50.h>
#include <stdio.h>
#include <math.h>
// You want to:
// get user input of a float
// check float
// Loop: minus money with biggest coins unit until negative
// then loop and loop
// until 1
// then count the number of loop (by assign a variable to count)
int main(void)
{
float money_give;
int quarters= 25, qc, dimes = 10, dc, nickels= 5, nc,pennies= 1, pc;
do
{
printf("O hai! How much change is owed?\n");
money_give = get_float();
}
while (money_give <= 0.0);
money_give = money_give * 100;
int money = (int)round(money_give);
while (money >= quarters)
{
qc = money / quarters;
money = money % quarters;
}
while (money >= dimes)
{
dc = money / dimes;
money = money % dimes;
}
while (money >= nickels)
{
nc = money / nickels;
money = money % nickels;
}
while (money >= pennies)
{
pc = money / pennies;
money = money % pennies;
}
printf("%i\n",pc+ nc+ qc +dc);
printf("%i\n",nc);
printf("%i\n",qc);
printf("%i\n",dc);
printf("%i\n",pc);
}
哦,输出是:
O hai! How much change is owed?
0.48
4381350
4381344
1
2
3
您忘记初始化变量。变化:
int quarters= 25, qc, dimes = 10, dc, nickels= 5, nc,pennies= 1, pc;
进入:
int quarters= 25, qc = 0, dimes = 10, dc = 0, nickels= 5, nc = 0, pennies= 1, pc = 0;
发生的事情是因为 nc
没有被初始化,然后它开始等于存储它的内存中已经存在的任何垃圾。
然后来到这部分的时候,并没有得到新的值,因为money
小于nickels
:
while (money >= nickels)
{
nc = money / nickels;
money = money % nickels;
}
因此它从来没有被分配一个新值而不是它开始时的垃圾。
从中得到的教训:始终初始化变量。
我已经在 PSET1 的 CS50 中完成了贪婪挑战的代码
不过,硬币的总数returns相当多。当我进一步查看时,我的镍币数量似乎很大(4381344 个硬币换 0.48 美分)。
叫我傻瓜,但我已经为此烦恼了一段时间。谁能指出原因?
#include <cs50.h>
#include <stdio.h>
#include <math.h>
// You want to:
// get user input of a float
// check float
// Loop: minus money with biggest coins unit until negative
// then loop and loop
// until 1
// then count the number of loop (by assign a variable to count)
int main(void)
{
float money_give;
int quarters= 25, qc, dimes = 10, dc, nickels= 5, nc,pennies= 1, pc;
do
{
printf("O hai! How much change is owed?\n");
money_give = get_float();
}
while (money_give <= 0.0);
money_give = money_give * 100;
int money = (int)round(money_give);
while (money >= quarters)
{
qc = money / quarters;
money = money % quarters;
}
while (money >= dimes)
{
dc = money / dimes;
money = money % dimes;
}
while (money >= nickels)
{
nc = money / nickels;
money = money % nickels;
}
while (money >= pennies)
{
pc = money / pennies;
money = money % pennies;
}
printf("%i\n",pc+ nc+ qc +dc);
printf("%i\n",nc);
printf("%i\n",qc);
printf("%i\n",dc);
printf("%i\n",pc);
}
哦,输出是:
O hai! How much change is owed?
0.48
4381350
4381344
1
2
3
您忘记初始化变量。变化:
int quarters= 25, qc, dimes = 10, dc, nickels= 5, nc,pennies= 1, pc;
进入:
int quarters= 25, qc = 0, dimes = 10, dc = 0, nickels= 5, nc = 0, pennies= 1, pc = 0;
发生的事情是因为 nc
没有被初始化,然后它开始等于存储它的内存中已经存在的任何垃圾。
然后来到这部分的时候,并没有得到新的值,因为money
小于nickels
:
while (money >= nickels)
{
nc = money / nickels;
money = money % nickels;
}
因此它从来没有被分配一个新值而不是它开始时的垃圾。
从中得到的教训:始终初始化变量。