【发布时间】: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