Total cost

A family consists of members. You are given the task to book flight tickets for these 

x members.
You are given the following information about the airline in which you have to book the tickets:

  • P: It denotes the cost of one ticket of the flight.
  • S: It denotes the number of total available seats in the flight.
  • T: If the numbers of available seats are less than or equal to T, then the cost of the flight ticket increases to H.
  • H: It denotes the new hiked cost.

Determine the total cost to book the tickets for all the family members.

Note: The tickets are booked one by one for all the family members.

Example:

Input:  p = 6000 , s = 10,  t= 5, h = 6500, x=  7
Output: 43000

Approach

C++

#include <bits/stdc++.h>
using namespace std;

long long totalCost(long long plong long s
long long tlong long hlong long x)
{

    long long cost = 0cnt = 0;
    while (s > t)
    {
        cost += p;
        s--;
        cnt++;
        if (cnt == x)
            break;
    }
    if (cnt < x)
        cost += (x - cnt) * h;
    return cost;
}
int main()
{
    long long p = 6000s = 10t = 5h = 6500x = 7;

    cout << totalCost(psthx<< "\n";

    return 0;
}


No comments:

Post a Comment