为什么提示用户后我的代码未返回所需的输出

我正在制作一个简单的算法,该算法仅使用25c,10c,5c和1c硬币,返回键入$值时需要找回的最小硬币数目。

例如欠款额:0.31 硬币:3

请参见下面的代码,它会正确提示用户,但在输入时不会返回任何数字。

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

int main(void)

{
    //Declaring the variables
   float change_owed;
   int quarter,dime,nickel,penny;
    //Defining types of coins that can be given as change.
    quarter = 25;
    dime = 10;
    nickel = 5;
    penny = 1;

    do
    {
    //Defining function and storing in variable change
        change_owed = get_float("Change: ");
    }  
    //Repeat prompt while input is negative  
    while (change_owed <= 0);
    //rounding the numbers so we can divide
    int amount = round(change_owed * 100);

    ///Deciding if to use a Quarter
    //Defining Quarter counter
    int count_quarter=0;
    while (amount >= quarter)
    {   
        //Counting number of quarters used
        count_quarter++;
        //Decreasing amount owed by a quarter
        return amount - quarter;
    }
    //Deciding whether to use a dime
    //Defining dime counter
    int count_dime=0;
    while (amount >= dime)
    {
        //Counting number of dimes used
        count_dime++;
        //Decreasing amount owed by a dime
        return amount - dime;
    }
    int count_nickel=0;
    while (amount >= nickel)
    {
       //Deciding whether to use a nickel
       //counting number of nickels used 
        count_nickel++;
        //Decreasing amount by a nickel
        return amount - nickel;
    }

    //Deciding whether to use a penny
    //Defining penny counter
    int count_penny=0;
    while (amount >= penny)
    {
       //counting number of pennies used
        count_penny++;
      //Decreasing amount by a penny
      return amount - penny; 
    }

  int total_coins = (count_quarter + count_dime + count_nickel + count_penny);

    printf("%i",total_coins);

}
bv5gj 回答:为什么提示用户后我的代码未返回所需的输出

使用C(或我能想到的任何语言)的return语句从函数返回一个值。这意味着在return语句之后将不再执行该函数的代码,下一条指令将是该函数之后的下一条指令。如果从main返回,则调用exit函数,该函数除其他外终止进程并确保该进程的返回值可用。

在bash上,如果这样做:

./a.out
echo $?

您可以看到C程序的返回值。

您想要的是持续添加尽可能大的硬币(贪心算法),然后切换到下一个较小的硬币。

好方法,使用整数并以美分计算一切。

#include "stdio.h"

#define NUM_COIN_TYPES 4

int main(void)
{
        int coins[NUM_COIN_TYPES] = {25,10,5,1};
        float change_owed_float = 13.37; //insert your read from commandline code here
        int change_owed = (int)(change_owed_float * 100.f);
        int num_coins = 0;

        for(int ctr = 0; ctr < NUM_COIN_TYPES && change_owed > 0; ++ctr)
        {
                while(change_owed - coins[ctr] >= 0)
                {
                        change_owed -= coins[ctr];
                        ++num_coins;
                }
        }

        printf("Number of coins required: %d\nChange owed: %d\n",num_coins,change_owed);
        return 0; //could be void main and no return
}

可能会产生怪异的浮动方式:

#include "stdio.h"

#define NUM_COIN_TYPES 4

int main(void)
{
        float coins[NUM_COIN_TYPES] = {.25,.10,.05,.01};
        float change_owed = 13.37; //insert your read from commandline code here
        int num_coins = 0;

        for(int ctr = 0; ctr < NUM_COIN_TYPES && change_owed > 0.0; ++ctr)
        {
                while(change_owed - coins[ctr] >= -.009) //floats are weird
                {
                   change_owed -= coins[ctr];
                   ++num_coins;
                }
        }

        if(change_owed > -.01 && change_owed <= 0.0)
        {
                //sanity check
                printf("It works!\n");
                change_owed = 0;
        }

        printf("Number of coins required: %d\nChange owed: %f\n",change_owed);
        return 0; //could be void main and no return
}

编辑:避免浮点算术怪异的最简单方法可能是在将所有内容都乘以100后再使用整数。

HTH

本文链接:https://www.f2er.com/2612872.html

大家都在问