首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我插入350 The时,输出应该是102.80。

当我插入350 The时,输出应该是102.80。
EN

Stack Overflow用户
提问于 2022-09-27 15:40:13
回答 3查看 66关注 0票数 -1

请参阅这张图以了解清楚的电块使用情况。

当我插入350 too的数字时,输出应该是102.80,但是我的输出出现RM 180.600006,小数位太长,我已经被放入%2f,但仍然显示为长小数点。在这里输出。用电量将分成几个区块。

代码语言:javascript
复制
#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;
    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-09-27 15:55:01

您的答案是错误的,因为每个累积kW的账单价格都会发生变化。换句话说,(Electric_Consumption*(33.40/100))计算的是每千瓦的33.40/100,而不是301到600千瓦之间的千瓦。您需要更改公式以添加上一个千瓦成本,然后从Electric_Consumption中减去这些千瓦。

小数太多的原因是您需要对格式字符串使用"%.2f"

票数 1
EN

Stack Overflow用户

发布于 2022-09-27 15:58:52

代码语言:javascript
复制
//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,那么他们需要支付

  • 21.80前200 kWh为21.8 * 200
  • 接下来的100 kWh是33.40 * 100
  • 51.60下一个300 kWh,即51.60 * 300
  • 54.60下一个300 kWh,即54.60 * 300
  • 其余的是57.1,也就是57.10 * 45

您的代码对所有kWh都使用了固定的价格,并且只使用金额来确定此固定价格。这是错的。

这可以通过递归解决:

代码语言:javascript
复制
 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;
}
票数 1
EN

Stack Overflow用户

发布于 2022-09-27 16:42:23

一种通过预先计算完整切片价格的解决方案。

代码语言:javascript
复制
    #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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73870286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档