📜  根据航空旅行的重量计算为行李支付的额外费用

📅  最后修改于: 2022-05-13 01:56:06.139000             🧑  作者: Mango

根据航空旅行的重量计算为行李支付的额外费用

给定一个大小为N的数组weight[] ,其中包含行李的重量。如果权重在W的阈值内,则不需要任何额外成本。但是在权重超过阈值后,他们需要根据下表支付额外的费用。任务是计算所需行李的额外费用。

Range of exceeding weight Cost for the excess weight
            0 – 50              100
          51 – 100              200
        101 – 150              300
        151 – 200              500
           > 200              1000

例子:

方法:该方法基于以下观察。如果行李的重量超过阈值W ,则根据给定的表格会产生额外费用。请按照以下步骤解决问题:

  • 从头开始迭代数组。
    • 检查当前行李的重量是否超过W
      • 如果有,则根据给定表格后的超重添加额外费用。
      • 否则继续迭代。
  • 返回总额外费用

下面是上述方法的实现,

C++
// C++ code to implement the above approach
#include 
using namespace std;
 
// Function to calculate the extra cost
int weighingMachine(int N, int weight[], int W)
{
    int amount = 0;
 
    // Loop to calculate the excess cost
    for (int i = 0; i < N; i++) {
        if (weight[i] - W > 0 && weight[i] - W <= 50)
            amount += 100;
        else if (weight[i] - W > 50 && weight[i] - W <= 100)
            amount += 200;
        else if (weight[i] - W > 100
                 && weight[i] - W <= 150)
            amount += 300;
        else if (weight[i] - W > 150
                 && weight[i] - W <= 200)
            amount += 500;
        else if (weight[i] - W > 200)
            amount += 1000;
    }
    return amount;
}
 
// Driver code
int main()
{
    int weight[] = { 120, 135, 280, 60, 300 };
    int N = 5;
    int W = 90;
 
    cout << weighingMachine(N, weight, W);
}
 
// This code is contributed by Samim Hossain Mondal.


Java
// Java code to implement the above approach
import java.util.*;
 
class GFG {
 
    // Function to calculate the extra cost
    static int weighingMachine(int N, int weight[], int W)
    {
        int amount = 0;
 
        // Loop to calculate the excess cost
        for (int i = 0; i < N; i++) {
            if (weight[i] - W > 0
                && weight[i] - W <= 50)
                amount += 100;
            else if (weight[i] - W > 50
                     && weight[i] - W <= 100)
                amount += 200;
            else if (weight[i] - W > 100
                     && weight[i] - W <= 150)
                amount += 300;
            else if (weight[i] - W > 150
                     && weight[i] - W <= 200)
                amount += 500;
            else if (weight[i] - W > 200)
                amount += 1000;
        }
        return amount;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int weight[] =
        { 120, 135, 280, 60, 300 };
        int N = 5;
        int W = 90;
 
        System.out.println(
          weighingMachine(N, weight, W));
    }
}


Python
# Python code to implement the above approach
 
# Function to calculate the extra cost
def weighingMachine(N, weight, W):
     
    amount = 0;
 
    # Loop to calculate the excess cost
    for i in range(0, N):
        if (weight[i] - W > 0 and weight[i] - W <= 50):
            amount = amount + 100
        elif (weight[i] - W > 50 and weight[i] - W <= 100):
            amount = amount + 200
        elif (weight[i] - W > 100
                 and weight[i] - W <= 150):
            amount = amount + 300
        elif (weight[i] - W > 150
                 and weight[i] - W <= 200):
            amount = amount + 500;
        elif (weight[i] - W > 200):
            amount = amount + 1000
             
    return amount
 
# Driver code
weight = [ 120, 135, 280, 60, 300 ]
N = 5
W = 90
 
print(weighingMachine(N, weight, W))
 
# This code is contributed by Samim Hossain Mondal.


C#
// C# code to implement the above approach
using System;
 
public class GFG {
 
  // Function to calculate the extra cost
  static int weighingMachine(int N, int []weight, int W)
  {
    int amount = 0;
 
    // Loop to calculate the excess cost
    for (int i = 0; i < N; i++) {
      if (weight[i] - W > 0
          && weight[i] - W <= 50)
        amount += 100;
      else if (weight[i] - W > 50
               && weight[i] - W <= 100)
        amount += 200;
      else if (weight[i] - W > 100
               && weight[i] - W <= 150)
        amount += 300;
      else if (weight[i] - W > 150
               && weight[i] - W <= 200)
        amount += 500;
      else if (weight[i] - W > 200)
        amount += 1000;
    }
    return amount;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int []weight =
    { 120, 135, 280, 60, 300 };
    int N = 5;
    int W = 90;
 
    Console.WriteLine(
      weighingMachine(N, weight, W));
  }
}
 
// This code is contributed by 29AjayKumar


Javascript



输出
1700

时间复杂度: O(N)
辅助空间: O(1)