算法设计:如何计算电费?详细实现

2021年3月27日18:08:55 发表评论 743 次浏览

本文概述

以整数U表示所耗电量的千瓦时单位,我们的任务是透过以下收费计算电费:

1到100个单位- 10卢比/单位
100到200个单位- 15卢比/单位
200到300个单位- 20卢比/单位
超过300个单位- 25卢比/单位

例子:

输入:U = 250
输出:3500
说明:前100个单位的电量– 10 * 100 = 1000
100至200个单位的电量– 15 * 100 = 1500
200至250个单位的电量– 20 * 50 = 1000
总电量账单= 1000 + 1500 + 1000 = 3500

输入:U = 95
输出:950
说明:前100个单位的电量-10 * 95 = 950
总电费= 950

方法:这个想法是要确定它所属的收费条, 然后根据上述费用计算账单。下面是步骤说明:

检查消耗的单位小于等于100, 如果是, 则总电费为:

总电费 = units * 10

否则, 请检查消耗的单位是否小于200, 如果是, 则总电费为:

总电费 = (100 * 10) + (units - 100) * 15

否则, 请检查消耗的单位是否小于300, 如果是, 则总电费将为:

总电费 = (100*10)+ (100*15)+ (units-200) * 20

否则, 请检查设备消耗的电量是否大于300, 如果是, 则总电费为:

总电费 = (100*10)+ (100*15)+ (100*20)+ (units-300) * 25

下面是上述方法的实现:

C ++

// C++ implementation to calculate the 
// electricity bill 
#include<bits/stdc++.h>
using namespace std;
  
// Function to calculate the 
// electricity bill 
int calculateBill( int units) 
{ 
  
     // Condition to find the charges 
     // bar in which the units consumed 
     // is fall 
     if (units <= 100) 
     { 
         return units * 10; 
     } 
     else if (units <= 200)
     { 
         return (100 * 10) + 
                (units - 100) * 15; 
     } 
     else if (units <= 300)
     { 
         return (100 * 10) + 
                (100 * 15) + 
                (units - 200) * 20; 
     } 
     else if (units > 300)
     { 
         return (100 * 10) + 
                (100 * 15) + 
                (100 * 20) + 
                (units - 300) * 25; 
     } 
     return 0; 
} 
  
// Driver Code 
int main() 
{ 
     int units = 250; 
     cout << calculateBill(units); 
} 
  
// This code is contributed by spp____

Java

// Java implementation to calculate the
// electricity bill
  
import java.util.*;
  
class ComputeElectricityBill {
  
     // Function to calculate the
     // electricity bill
     public static int calculateBill( int units)
     {
  
         // Condition to find the charges
         // bar in which the units consumed
         // is fall
         if (units <= 100 ) {
             return units * 10 ;
         }
         else if (units <= 200 ) {
             return ( 100 * 10 )
                 + (units - 100 )
                       * 15 ;
         }
         else if (units <= 300 ) {
             return ( 100 * 10 )
                 + ( 100 * 15 )
                 + (units - 200 )
                       * 20 ;
         }
         else if (units > 300 ) {
             return ( 100 * 10 )
                 + ( 100 * 15 )
                 + ( 100 * 20 )
                 + (units - 300 )
                       * 25 ;
         }
         return 0 ;
     }
  
     // Driver Code
     public static void main(String args[])
     {
         int units = 250 ;
  
         System.out.println(
             calculateBill(units));
     }
}

Python3

# Python3 implementation to calculate the 
# electricity bill 
  
# Function to calculate the 
# electricity bill 
def calculateBill(units):
  
     # Condition to find the charges 
     # bar in which the units consumed 
     # is fall 
     if (units < = 100 ):
       
         return units * 10 ; 
      
     elif (units < = 200 ):
      
         return (( 100 * 10 ) + 
                 (units - 100 ) * 15 ); 
      
     elif (units < = 300 ):
       
         return (( 100 * 10 ) + 
                 ( 100 * 15 ) + 
                 (units - 200 ) * 20 ); 
      
     elif (units > 300 ):
      
         return (( 100 * 10 ) + 
                 ( 100 * 15 ) + 
                 ( 100 * 20 ) + 
                 (units - 300 ) * 25 ); 
      
     return 0 ; 
  
# Driver Code 
units = 250 ; 
print (calculateBill(units)); 
  
# This code is contributed by Code_Mech

C#

// C# implementation to calculate the 
// electricity bill 
using System;
  
class ComputeElectricityBill{ 
  
// Function to calculate the 
// electricity bill 
public static int calculateBill( int units) 
{ 
  
     // Condition to find the charges 
     // bar in which the units consumed 
     // is fall 
     if (units <= 100)
     { 
         return units * 10; 
     } 
     else if (units <= 200)
     { 
         return (100 * 10) + 
                (units - 100) * 15; 
     } 
     else if (units <= 300) 
     { 
         return (100 * 10) +
                (100 * 15) + 
                (units - 200) * 20; 
     } 
     else if (units > 300) 
     { 
         return (100 * 10) + 
                (100 * 15) + 
                (100 * 20) + 
                (units - 300) * 25; 
     } 
     return 0; 
} 
  
// Driver Code 
public static void Main(String []args) 
{ 
     int units = 250; 
      
     Console.WriteLine(calculateBill(units)); 
} 
} 
  
// This code is contributed by spp____

输出如下:

3500

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: