# 算法设计：如何计算电费？详细实现

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

## 本文概述

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

100至200个单位的电量– 15 * 100 = 1500
200至250个单位的电量– 20 * 50 = 1000

## 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``