Zhejiang University Edition "C Language Programming (3rd Edition)" Exercise 2-2

Exercise 2-2 Stepped electricity price (15 points)

In order to encourage residents to save electricity, a provincial power company implements a "tiered electricity price", and the electricity price of residential users who install one household and one meter is divided into two "ladders": the monthly electricity consumption is within 50 kWh (including 50 kWh) The electricity price is 0.53 yuan / kWh; if it exceeds 50 kWh, the excess electricity consumption will be increased by 0.05 yuan / kWh. Please write a program to calculate the electricity bill.

Input format:
Enter the monthly electricity consumption (unit: kWh) of a user given in one line.

Output format:
output the electricity fee (yuan) that the user should pay in one line, and the result retains two decimal places. The format is as follows: "cost = electricity payable value"; if the power consumption is less than 0, then output "Invalid Value!".

Input example 1:

10

Sample output 1:

cost = 5.30

Input example 2:

100

Sample output 2:

cost = 55.50

This is the junior high school math problem (if I remember correctly), but I feel that I have written a lot. You must have a simpler way. (Useless tears shed)

#include"stdio.h"
int main()
{
    int Power;
    float cost;
    scanf("%d", &Power);
    if(Power >= 0 && Power <= 50)
    {
        cost = 0.53*Power;
        printf("cost = %0.2f", cost);
    }
    else if(Power > 50)
    {
        cost = 0.53*50 + (Power - 50)*0.58;
        printf("cost = %0.2f", cost);
    }
    else
    {
        printf("Invalid Value!");
    }
    return 0;
}
Published 25 original articles · won 3 · views 240

Guess you like

Origin blog.csdn.net/oxygen_ls/article/details/105376799