开始程序麻烦

时间:2013-12-26 19:18:53

标签: c algorithm

此程序旨在获取用户输入(金额)并以硬币输出输入。该程序试图使用尽可能少的硬币。该计划大部分都有效。例如,当我输入2.30的值时,我获得的仅为9个季度。我需要帮助解决这个编码问题。

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

int main()
{

    int coins, quarter, money3, money2, pennies, nickels, dimes, quarters;

    int penny, nickel, dime, money1;

    float money;

    quarter = 25;
    dime = 10;
    nickel = 5;
    penny = 1;

    printf("How much money would you like in change?\n");
    scanf("%f", & money);

    coins = (money * 100) + .5;

    quarters = coins/quarter;
    money1 = coins % quarter;        
    dimes = money1/dime;
    money2 = coins % dime;
    nickels = money2/nickel;
    money3 = coins % nickel;
    pennies = money3/penny;

    printf("The number of quarters is %d\n", quarters);
    printf("The number of dimes is %d\n", dimes);
    printf("The number of nickels are %d\n", nickels);
    printf("The number of pennies is %d\n", pennies);
    printf("%d\n", money1);
    printf("%d\n", coins);
}

4 个答案:

答案 0 :(得分:1)

请勿使用coins的原始值来计算money2money3。相反,money2可以使用money1来计算,money3可以使用money2来计算。你应该改变

money2 = coins % dime;
nickels = money2/nickel;
money3 = coins % nickel;  

money2 = money1 % dime;
nickels = money2/nickel;
money3 = money2 % nickel;  

现在2.30正在提供输出:

The number of quarters is 9
The number of dimes is 0
The number of nickels are 1
The number of pennines is 0
5
230      

答案 1 :(得分:1)

假设您想要将输入值分成可能的最高值硬币,那么您可以按照值的降序顺序通过硬币,然后减去金额。这消除了对某些变量的需求。

这个怎么样:

#include <stdio.h>
/* you don't actually need math.h for this code */    

int main()
{

  int coins, pennies, nickels, dimes, quarters;
  int quarter, nickel, dime;
  float money;

  quarter = 25;
  dime = 10;
  nickel = 5;

  printf("How much money would you like in change?\n");
  scanf("%f", & money);

  coins = (money * 100) + .5;

  quarters = coins/quarter;
  coins -= quarters * quarter;
  dimes = coins/dime;
  coins -= dimes * dime;
  nickels = coins/nickel;
  coins -= nickels * nickel;
  pennies = coins;

  printf("The number of quarters is %d\n", quarters);
  printf("The number of dimes is %d\n", dimes);
  printf("The number of nickels are %d\n", nickels);
  printf("The number of pennies is %d\n", pennies);

  return 0;
}

答案 2 :(得分:1)

您应该使用调试器逐步执行该程序,并检查在它们运行时分配给变量的值,或者添加一些printf来输出变量的值,因为它们被分配/更改。你遇到的问题基本上是一个简单的拼写错误。我建议您再尝试解决此问题,如果您不想自己解决,请参阅以下ideone现场演示:http://ideone.com/BsIgg0

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

int main()

{
    int coins, quarter, money3, money2, pennies, nickels, dimes, quarters;
    int penny, nickel, dime, money1;
    float money;

    quarter = 25;
    dime = 10;
    nickel = 5;
    penny = 1;

    printf("How much money would you like in change?\n");
    scanf("%f", & money);

    coins = (money * 100) + .5;

    printf("coins = %d\n", coins);

    quarters = coins/quarter;
    money1 = coins % quarter;
    printf("money1 = %d\n", money1);        
    dimes = money1/dime;
    money2 = coins % dime;
    printf("money2 = %d\n", money2);
    nickels = money2/nickel;
    money3 = coins % nickel;
    printf("money3 = %d\n", money3);
    pennies = money3/penny;

    printf("The number of quarters is %d\n", quarters);
    printf("The number of dimes is %d\n", dimes);
    printf("The number of nickels are %d\n", nickels);
    printf("The number of pennies is %d\n", pennies);
    printf("%d\n", money1);
    printf("%d\n", coins);
}

答案 3 :(得分:0)

quarters = coins/quarter;
money1 = coins % quarter;
dimes = money1/dime;
money2 = money1 % dime;
nickels = money2/nickel;
money3 = money2 % nickel;
pennies = money3/penny;