当我插入350 too的数字时,输出应该是102.80,但是我的输出出现RM 180.600006,小数位太长,我已经被放入%2f,但仍然显示为长小数点。在这里输出。用电量将分成几个区块。
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#include <stdio.h>
//For the first 200 kWh (1-200 kWh) per month = RM21.80
//For the next 100 kWh (201-300 kWh) per month = RM33.40
//For the next 300 kWh (301-600 kWh) per month = RM51.60
//For the next 300 kWh (601-900 kWh) per month = RM54.60
//For the next kWh (901 kWh onwards) per month = RM57.10
int main()
{
float Electric_Consumption, Bill;
printf("Please insert the Current Electric Consumption: ");
scanf("%f", &Electric_Consumption);
if (Electric_Consumption >=1 && Electric_Consumption <= 200)
{
Bill = (Electric_Consumption*(21.80/100));
printf("Your bill need to pay is RM %f", Bill);
}
else if(Electric_Consumption >=201 && Electric_Consumption <= 300)
{
Bill = (Electric_Consumption*(33.40/100));
printf("Your bill need to pay is RM %f", Bill);
}
else if(Electric_Consumption >=301 && Electric_Consumption <= 600)
{
Bill = (Electric_Consumption*(51.60/100));
printf("Your bill need to pay is RM %f", Bill);
}
else if(Electric_Consumption >=601 && Electric_Consumption <= 900)
{
Bill = (Electric_Consumption*(54.60/100));
printf("Your bill need to pay is RM %f", Bill);
}
else if(Electric_Consumption >=900)
{
Bill = (Electric_Consumption*(57.10/100));
printf("Your bill need to pay is RM %f", Bill);
}
else
{ printf("You inputed wrong electric consumption!");
}
return 0;
}发布于 2022-09-27 15:55:01
您的答案是错误的,因为每个累积kW的账单价格都会发生变化。换句话说,(Electric_Consumption*(33.40/100))计算的是每千瓦的33.40/100,而不是301到600千瓦之间的千瓦。您需要更改公式以添加上一个千瓦成本,然后从Electric_Consumption中减去这些千瓦。
小数太多的原因是您需要对格式字符串使用"%.2f"。
发布于 2022-09-27 15:58:52
这
//For the first 200 kWh (1-200 kWh) per month = RM21.80
//For the next 100 kWh (201-300 kWh) per month = RM33.40
//For the next 300 kWh (301-600 kWh) per month = RM51.60
//For the next 300 kWh (601-900 kWh) per month = RM54.60
//For the next kWh (901 kWh onwards) per month = RM57.10意思是,如果有人使用945 kWh,那么他们需要支付
您的代码对所有kWh都使用了固定的价格,并且只使用金额来确定此固定价格。这是错的。
这可以通过递归解决:
double calculate_price(double kWh) {
if (kWh > 900) return (kWh-900)*57.10 + calculate_price(kWh-900);
else if (kWh > 600) return (kWh-600)*54.60 + calculate_price(kWh-600);
else if .... same for the rest....
else // kWh <= 200
return 21.80*kWh;
}发布于 2022-09-27 16:42:23
一种通过预先计算完整切片价格的解决方案。
#include <stdio.h>
//For the first 200 kWh (1-200 kWh) per month = RM 21.80
//For the next 100 kWh (201-300 kWh) per month = RM 33.40
//For the next 300 kWh (301-600 kWh) per month = RM 51.60
//For the next 300 kWh (601-900 kWh) per month = RM 54.60
//For the next kWh (901 kWh onwards) per month = RM 57.10
int main(void)
{
float Electric_Consumption;
printf("Please insert the Current Electric Consumption: ");
scanf("%f", &Electric_Consumption);
float price_full_slice[] = {21.8*2,
21.8*2 + 33.4,
21.8*2 + 33.4 + 51.6*3,
21.8*2 + 33.4 + 51.6*3, 54.6*3};
float price = 0;
if(Electric_Consumption>900)
price = price_full_slice[3] + (Electric_Consumption-900) * 57.1/100;
if(Electric_Consumption>600 && Electric_Consumption<=900)
price = price_full_slice[2] + (Electric_Consumption-600) * 54.6/100;
if(Electric_Consumption>300 && Electric_Consumption<=600)
price = price_full_slice[1] + (Electric_Consumption-300) * 51.6/100;
if(Electric_Consumption>200 && Electric_Consumption<=300)
price = price_full_slice[0] + (Electric_Consumption-200) * 33.4/100;
if(Electric_Consumption<=200)
price = Electric_Consumption * 21.8/100;
printf("%.2f", price);
return 0;
}https://stackoverflow.com/questions/73870286
复制相似问题