욕심 많은 프로그램에 대한 내 코드는 4.2를 제외하고 지금까지 모든 숫자에서 잘 작동합니다. 사람이 오류cs50 pset1 욕심이 드는 비정상적인 버그
:) greedy.c exists
:) greedy.c compiles
:) input of 0.41 yields output of 4
:) input of 0.01 yields output of 1
:) input of 0.15 yields output of 2
:) input of 1.6 yields output of 7
:) input of 23 yields output of 92
**:(input of 4.2 yields output of 18
\ expected output, but not "22\n"**
:) rejects a negative input like -.1
:) rejects a non-numeric input of "foo"
:) rejects a non-numeric input of ""
#include <stdio.h>
#include <cs50.h>
int main(void)
{
float x;
do
{
printf("how much change is owed(in dollars)?:\n");
x = GetFloat();
}
while (x < 0);
x = x*100;
int i = 0;
while (x >= 25)
{
x = (x-25);
i++;
}
while (x >= 10)
{
x = (x-10);
i++;
}
while (x >= 5)
{
x = (x-5);
i++;
}
while (x >= 1)
{
x = (x-1);
i++;
}
printf("%d\n",i);
}
[부동 소수점 연산이 깨졌습니까?] (http://stackoverflow.com/questions/588004/is-floating-point-math-broken)를 참조하십시오. int 값으로 작업하는 것이 더 좋습니다. 이 문제를 나타내는 SO에 대한 많은 "변경"질문이 있습니다. 지금까지 "비정상적인"것이 아닌 단계는 당신이 통과하는 단계입니다. –
정확한 값이 필요하면 부동 소수점을 사용하지 마십시오. – Olaf