【问题标题】:C Math assignmentC 数学作业
【发布时间】:2017-01-27 08:56:54
【问题描述】:

我必须编写一个程序来计算支付某物所需的硬币数量。 IE。

total cost = $1.35
$1.00 = 1, remaining $0.35
$0.25 = 1, remaining $0.10
$0.10 = 1, remaining $0.0
$0.05 = 0, remaining $0.0
$0.01 = 0, remaining $0.0`

我的问题是如何使用模数进行这些数学运算?作为作业的一部分,我必须使用模数和整数除法进行计算,但我看不出如何使用模数对这段代码起作用。我开始的代码如下

#include<stdio.h>
#include<math.h>
int main(){
    float topay, change;
    float gst = 1.13;                               //gst amount
    int loonies, quatres, nickles, dimes, pennies;     //coin variables
    printf ("Please enter the amount to be paid: $");
    scanf ("%f", &topay);                           //Input for amount to be paid
    printf ("GST: %.2f\n", gst);
    printf ("Balance owing: $%.2f\n", topay=topay*gst); 
    loonies = topay;            //Math for amount of loonies
    change = topay - loonies;   //Math for balance owing
    quatres = change/0.25;      //Math for how many quarters needed
    printf ("Loonies required: %d, balance owing $%.2f\n", loonies, change);
    printf ("Quarters required: %d, balance owing $%.2f\n", quatres, change = change-(quatres*0.25));

    return 0;
}

【问题讨论】:

  • 使用 int 表示美分的数量,而不是使用 float 来表示美元。

标签: c math variable-assignment


【解决方案1】:

模数告诉您除法运算后的余数。所以:

$1.35 模数 $1.00 产生 35 美分

因此,您需要在某处存储 35 美分,但还要从余额中减去:

$1.35 - $0.35 = $1.00

现在您使用整数除法来查看 1.00 美元中有多少美元硬币。答案是一。

好的,现在余额为 0.35 美元。下一枚硬币是 25 美分硬币,0.25 美元。

因此,0.35 美元的模数 0.25 美元等于 0.10 美元。把十美分存起来。

从余额中减去 0.10 美元得到 0.25 美元。 0.25 美元有多少个季度?回答一个。

好的,现在余额是 0.10 美元。

然后在你身边。

这可以在一个简单的循环中完成,其中包含一个包含硬币面额的数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多