C语言中的贪心算法

C语言中的贪心算法,c,algorithm,greedy,C,Algorithm,Greedy,我刚开始学习C语言。我写了这个C代码来实现贪婪算法 我不知道我在这段代码上犯了什么错误,这段代码看起来不错,但它并没有像我预期的那样工作。有人能帮我修复这个代码吗 int main(void) { float amount = 0; int cents = 0; int count = 0; int amount_left = 0; amount = .30; cents = (int)round(amount * 100); pri

我刚开始学习C语言。我写了这个C代码来实现贪婪算法 我不知道我在这段代码上犯了什么错误,这段代码看起来不错,但它并没有像我预期的那样工作。有人能帮我修复这个代码吗

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);
}

关于代码中的问题的注释:

您在第一个循环中使用了美分,此时将有剩余的金额,在第一个循环中,如果需要多次迭代,结果将不正确。 根据建议,最好将剩余金额-10>=0更改为剩余金额>=10。 最后的printf声明很可能由文本提供,用于打印通过提供的金额获得的硬币计数。 代码:

注:

在C中的整数运算中,当17/5=3和17%5=2时,不需要循环操作17/5=3。 使用此选项,您可以对值为N的硬币使用amount/N硬币计数为0,例如:amount=9,N=10,9/10=0(整数除法),剩余的金额为amount%N。 最后一种情况下,1硬币的剩余金额始终为0。
关于代码中的问题的注释:

您在第一个循环中使用了美分,此时将有剩余的金额,在第一个循环中,如果需要多次迭代,结果将不正确。 根据建议,最好将剩余金额-10>=0更改为剩余金额>=10。 最后的printf声明很可能由文本提供,用于打印通过提供的金额获得的硬币计数。 代码:

注:

在C中的整数运算中,当17/5=3和17%5=2时,不需要循环操作17/5=3。 使用此选项,您可以对值为N的硬币使用amount/N硬币计数为0,例如:amount=9,N=10,9/10=0(整数除法),剩余的金额为amount%N。 最后一种情况下,1硬币的剩余金额始终为0。
我同意NetVipeC的回答

我想添加一个可能超出您的任务范围的注释,但可能有助于您在将来创建更好的代码:

您的代码会受到影响。为了消除这种情况,我将创建一个函数,并使用不同的参数多次调用该函数。这个过程叫做。编写更复杂的程序需要代码重用。代码:

// a user-defined function that counts the number of coins with a specific value used 
int count_number_of_coins(int amount_left, int coin_value) {
    int count = 0;
    while(amount_left >= coin_value) {
        count++;
        amount_left -= coin_value;
    }
    return count;
}

int main(void) {
    float amount = 0;
    int cents = 0;
    int count = 0;
    int amount_left = 0;
    int coin_values[] = {25, 10, 5, 1}; // an array of ints that hold the values of the coins in cents.
    int i;

    amount = .30;

    cents = (int)round(amount * 100);

    printf("%d", cents);

    amount_left = cents;

    for(i=0; i<4; i++) {
        int current_count = count_number_of_coins(amount_left, coin_values[i]);
        amount_left -= current_count*coin_values[i];
        count += current_count;
    }

    printf("You get %d coins\n", count);
}

我同意NetVipeC的回答

我想添加一个可能超出您的任务范围的注释,但可能有助于您在将来创建更好的代码:

您的代码会受到影响。为了消除这种情况,我将创建一个函数,并使用不同的参数多次调用该函数。这个过程叫做。编写更复杂的程序需要代码重用。代码:

// a user-defined function that counts the number of coins with a specific value used 
int count_number_of_coins(int amount_left, int coin_value) {
    int count = 0;
    while(amount_left >= coin_value) {
        count++;
        amount_left -= coin_value;
    }
    return count;
}

int main(void) {
    float amount = 0;
    int cents = 0;
    int count = 0;
    int amount_left = 0;
    int coin_values[] = {25, 10, 5, 1}; // an array of ints that hold the values of the coins in cents.
    int i;

    amount = .30;

    cents = (int)round(amount * 100);

    printf("%d", cents);

    amount_left = cents;

    for(i=0; i<4; i++) {
        int current_count = count_number_of_coins(amount_left, coin_values[i]);
        amount_left -= current_count*coin_values[i];
        count += current_count;
    }

    printf("You get %d coins\n", count);
}

打印计数而不是剩余的金额?如果您在美分>=25时写入,会更清晰,不是吗?Fortran II需要您显示的那种比较,总是比较为零;Fortran IV——又名Fortran 66——超越了这一点。还有一个有用的运算符writed/和另一个writed%可以使这一点更好。@PatriceGahide谢谢!但是我仍然没有得到输出。在第一个while循环中,如果美分大于25,会发生什么?这个循环在退出之前会重复多少次?如果你看不出为什么第一个while循环会有bug,那么使用你发布的代码并使用调试器逐步完成它是非常值得的。你很快就会意识到问题出在哪里。打印计数而不是剩余的数量?如果你在美分>=25…的时候写,会更清楚,不是吗?Fortran II需要您显示的那种比较,总是比较为零;Fortran IV——又名Fortran 66——超越了这一点。还有一个有用的运算符writed/和另一个writed%可以使这一点更好。@PatriceGahide谢谢!但是我仍然没有得到输出。在第一个while循环中,如果美分大于25,会发生什么?这个循环在退出之前会重复多少次?如果你看不出为什么第一个while循环会有bug,那么使用你发布的代码并使用调试器逐步完成它是非常值得的。你会很快意识到问题所在。谢谢大家!实际上,我是C语言的初学者,我不明白优化后的解决方案是如何工作的!如果你不介意的话,你能用代码之间的注释解释一下吗?@user3342450:the/是一个整数除法。例如,30/25返回1。%运算符称为模或模,并返回除法的余数。例如,30%25返回5。那么,x=x+y;可缩写为x+=y;与x=x%y时相同;可以缩写为x%=y;。使用调试器逐步完成此代码以获得更多信息。谢谢大家!实际上,我是C语言的初学者,我不明白优化后的解决方案是如何工作的!如果你不介意的话,你能用代码之间的注释解释一下吗?@user3342450:the/是一个整数除法。例如,30/25返回1。%运算符称为模或模,并返回除法的余数。例如,30%25返回5。那么,x=x+y;可缩写为x+=y;与x=x%y时相同;可以缩写为x%=
Y使用调试器逐步完成此代码以获得更多信息。谢谢!我会尽快学习所有的功能,因为这段代码看起来比我的代码好。没问题。我已经编辑了答案,有一个更简单的实现。谢谢!我会尽快学习所有的功能,因为这段代码看起来比我的代码好。没问题。我对答案进行了编辑,使其具有更简单的实现。
// a user-defined function that counts the number of coins with a specific value used 
int count_number_of_coins(int amount_left, int coin_value) {
    int count = 0;
    while(amount_left >= coin_value) {
        count++;
        amount_left -= coin_value;
    }
    return count;
}

int main(void) {
    float amount = 0;
    int cents = 0;
    int count = 0;
    int amount_left = 0;
    int coin_values[] = {25, 10, 5, 1}; // an array of ints that hold the values of the coins in cents.
    int i;

    amount = .30;

    cents = (int)round(amount * 100);

    printf("%d", cents);

    amount_left = cents;

    for(i=0; i<4; i++) {
        int current_count = count_number_of_coins(amount_left, coin_values[i]);
        amount_left -= current_count*coin_values[i];
        count += current_count;
    }

    printf("You get %d coins\n", count);
}
int main(void) {
    float amount = 0;
    int cents = 0;
    int count = 0;
    int amount_left = 0;
    int coin_values[] = {25, 10, 5, 1}; // an array of ints that hold the values of the coins in cents.
    int i;

    amount = .30;

    cents = (int)round(amount * 100);

    printf("%d", cents);

    amount_left = cents;

    for(i=0; i<4; i++) {
        while(amount_left >= coin_values[i]) {
            count++;
            amount_left -= coin_values[i];
        }
    }

    printf("You get %d coins\n", count);
}