为什么我的程序适用于某些测试而不适用于其他测试?

时间:2017-09-25 18:32:31

标签: c cs50

这是我测试的输出:

With Cte As
(
    Select  *,
            Row_Number() Over (Partition By Event_Type Order By Time Desc) As Row_Number,
            Lead(Value) Over (Partition By Event_Type Order By Time Desc) As Prev
    From    YourTable
)
Select  Event_Type, Value - Prev As Value
From    Cte
Where   Prev Is Not Null
And     Row_Number = 1

这是代码:

:) greedy exists

:) greedy compiles

:( input of 0.41 yields output of 4
    expected "4\n", not "3\n"

:( input of 0.01 yields output of 1
    expected "1\n", not "0\n"

:) 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

:) rejects a negative input like -.1

:) rejects a non-numeric input of "foo"

:) rejects a non-numeric input of ""

1 个答案:

答案 0 :(得分:0)

正如Havenard已经写过的那样,问题是change被存储为float。 对于这样的程序,change必须存储为整数值。

使用int代替float代码:

#include <stdio.h>
#include <cs50.h>

void count_coins (void);

int coin = 0;
int change = 0;

int main (void) {
    do {
        change = 41; // insert a get integer function here
    } while (change < 0);

    count_coins();

    printf("%i\n", coin);
}

void count_coins (void) {
    while (change >= 25) {
        coin++;
        change -= 25;
    }

    while (change >= 10) {
        coin++;
        change -= 10;
    }

    while(change >= 5) {
        coin++;
        change -= 5;
    }

    while (change >= 1) {
        coin++;
        change -= 1;
    }
}