Ticker

6/recent/ticker-posts

Code-Vita Questions: Television Sets with Solution in C, C++ and JAVA

Television Sets

 Problem Description

Dr. Vishnu is opening a new world-class hospital in a small town designed to be the first preference of the patients in the city. Hospital has N number of rooms of two types - with TV and without TV, with daily rates of R1 and R2 respectively.


However, from his experience Dr. Vishnu knows that the number of patients is not constant throughout the year, instead, it follows a pattern. The number of patients on any given day of the year is given by the following formula –


(6-M)^2 + |D-15| where

 

M is the number of the month (1 for Jan, 2 for Feb...12 for Dec) and

 

D is the date (1,2...31).

 

All patients prefer without TV rooms as they are cheaper, but will opt for TV rooms only if without TV rooms are not available. Hospital has a revenue target for the first year of operation. Given this target and the values of N, R1, and R2 you need to identify the number of TVs the hospital should buy so that it meets the revenue target. Assume the hospital opens on 1st Jan and year is a non-leap year.

 

Constraints

Hospital opens on 1st Jan in an ordinary year

 

5 <= Number of rooms <= 100

 

500 <= Room Rates <= 5000

 

0 <= Target revenue < 90000000

 

Input Format

First-line provides an integer N that denotes the number of rooms in the hospital

 

Second-line provides two space-delimited integers that denote the rates of rooms with TV (R1) and without TV (R2) respectively

 

The third line provides the revenue target

 

Output

Minimum number of TVs the hospital needs to buy to meet its revenue target. If it cannot achieve its target, print the total number of rooms in the hospital.

 

Timeout

1

 

 

Test Case

Example 1

 

Input

 

20

 

1500 1000

 

7000000

 

Output

 

14

 

Explanation

 

Using the formula, the number of patients on 1st Jan will be 39, on 2nd Jan will be 38, and so on. Considering there are only twenty rooms and rates of both types of rooms are 1500 and 1000 respectively, we will need 14 TV sets to get a revenue of 7119500. With 13 TV sets, Total revenue will be less than 7000000

 

Example 2

 

Input

 

10

 

1000 1500

 

10000000

 

Output

 

10

 

Explanation

 

In the above example, the target will not be achieved, even by equipping all the rooms with TV. Hence, the answer is 10 i.e. the total number of rooms in the hospital.


Solution :


Code in C:


#include <stdio.h>
int main()
{
    int mon[] = {312831303130313130313031};
    int NR1R2T;
    scanf("%d", &N);
    scanf("%d %d", &R1, &R2);
    scanf("%d", &T);
    //Let total no. of TV required be x
    int x = -1;                  //initializing x
    for (int i = 1i <= Ni++) // foor loop to calculate number of TV rooms required to achieve target revenue
    {
        x = i;            // Let total no. of TV required be x = i
        int nx = N - x;   // Total no. of rooms without TV will be = Total No. of rooms - No. of rooms with TV
        int total = 0;    // Variable total is used to store total revenue per year
        int d = 0m = 1; // d and m for evaluating date and month respectively
        while (1)         //infinite while loop
        {
            if (d == 31 && m == 12)   // if date and month is the last day of of the year
                break;                // then break the infinite while loop
            else if (d >= mon[m - 1]) // condition for changing month and date if date is the last day of month
            {
                d = 1;     //reseting date to 1
                m = m + 1; // increasing the month by 1
            }
            else
            {
                d = d + 1; //else increase the date normally by 1
            }
            int p = (6 - m) * (6 - m) + abs(d - 15); //formula provided in question to find patient on a d date of m month
            // let patient in x room be xp, where x is no. of rooms with TV
            // and let patient in nx room be nxp, where nx is no. of rooms without TV
            int nxpxp;
            // It is given in the question that patients prefer room without TV. Firstly we have to fill rooms without TV

            if (p <= nx) //if no. of patient is less than or equals to no. of rooms without TV
            {
                nxp = p; //then all patients goes to non-TV room
                xp = 0;  //and no one goes to TV room
            }
            else //else (means, no of patients is more than no. of non-TV rooms)
            {
                nxp = nx;        //all non-TV rooms will be full
                if (p - nx <= x) // if no. of remaining patients are less than or equals to no. of TV rooms
                    xp = p - nx; //then all remaining patients goes to TV rooms
                else             // else (means, no. of remaining patients are more than no. of TV rooms)
                    xp = x;      // then only x number of patients will be provided with TV rooms, where x is Total no. of TV rooms
            }
            total += xp * R1 + nxp * R2; // Total revenue of one day will be xp*R1 + nxp*R2
        }
        if (total >= T) //if total revenue is greater than or equals to the target revenue
            break;      //break the for loop
    }
    printf("%d"x);
    return 0;
}



Code in C++:


#include <iostream>
using namespace std;
main()
{
    int mon[] = {312831303130313130313031};
    int NR1R2T;
    cin >> N;
    cin >> R1 >> R2;
    cin >> T;
    //Let total no. of TV required be x
    int x = -1;                  //initializing x
    for (int i = 1i <= Ni++) // foor loop to calculate number of TV rooms required to achieve target revenue
    {
        x = i;            // Let total no. of TV required be x = i
        int nx = N - x;   // Total no. of rooms without TV will be = Total No. of rooms - No. of rooms with TV
        int total = 0;    // Variable total is used to store total revenue per year
        int d = 0m = 1; // d and m for evaluating date and month respectively
        while (true)      //infinite while loop
        {
            if (d == 31 && m == 12)   // if date and month is the last day of of the year
                break;                // then break the infinite while loop
            else if (d >= mon[m - 1]) // condition for changing month and date if date is the last day of month
            {
                d = 1;     //reseting date to 1
                m = m + 1; // increasing the month by 1
            }
            else
            {
                d = d + 1; //else increase the date normally by 1
            }
            int p = (6 - m) * (6 - m) + abs(d - 15); //formula provided in question to find patient on a d date of m month
            // let patient in x room be xp, where x is no. of rooms with TV
            // and let patient in nx room be nxp, where nx is no. of rooms without TV
            int nxpxp;
            // It is given in the question that patients prefer room without TV. Firstly we have to fill rooms without TV

            if (p <= nx) //if no. of patient is less than or equals to no. of rooms without TV
            {
                nxp = p; //then all patients goes to non-TV room
                xp = 0;  //and no one goes to TV room
            }
            else //else (means, no of patients is more than no. of non-TV rooms)
            {
                nxp = nx;        //all non-TV rooms will be full
                if (p - nx <= x) // if no. of remaining patients are less than or equals to no. of TV rooms
                    xp = p - nx; //then all remaining patients goes to TV rooms
                else             // else (means, no. of remaining patients are more than no. of TV rooms)
                    xp = x;      // then only x number of patients will be provided with TV rooms, where x is Total no. of TV rooms
            }
            total += xp * R1 + nxp * R2; // Total revenue of one day will be xp*R1 + nxp*R2
        }
        if (total >= T) //if total revenue is greater than or equals to the target revenue
            break;      //break the for loop
    }
    cout << x;
}



Code in JAVA:


import java.util.Scanner;

public class TvSet {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int mon[] = { 312831303130313130313031 };
        int N = in.nextInt();
        int R1 = in.nextInt();
        int R2 = in.nextInt();
        int T = in.nextInt();
        in.close();
        // Let total no. of TV required be x
        int x = -1// initializing x
        for (int i = 1i <= Ni++) // foor loop to calculate number of TV rooms required to achieve target revenue
        {
            x = i// Let total no. of TV required be x = i
            int nx = N - x// Total no. of rooms without TV will be = Total No. of rooms - No. of rooms
                            // with TV
            int total = 0// Variable total is used to store total revenue per year
            int d = 0m = 1// d and m for evaluating date and month respectively
            while (true// infinite while loop
            {
                if (d == 31 && m == 12// if date and month is the last day of of the year
                    break// then break the infinite while loop
                else if (d >= mon[m - 1]) // condition for changing month and date if date is the last day of month
                {
                    d = 1// reseting date to 1
                    m = m + 1// increasing the month by 1
                } else {
                    d = d + 1// else increase the date normally by 1
                }
                int p = (6 - m) * (6 - m) + Math.abs(d - 15); // formula provided in question to find patient on a d
                                                              // date of m month
                // let patient in x room be xp, where x is no. of rooms with TV
                // and let patient in nx room be nxp, where nx is no. of rooms without TV
                int nxpxp;
                // It is given in the question that patients prefer room without TV. Firstly we
                // have to fill rooms without TV
                if (p <= nx// if no. of patient is less than or equals to no. of rooms without TV
                {
                    nxp = p;// then all patients goes to non-TV room
                    xp = 0;// and no one goes to TV room
                } else // else (means, no of patients is more than no. of non-TV rooms)
                {
                    nxp = nx// all non-TV rooms will be full
                    if (p - nx <= x// if no. of remaining patients are less than or equals to no. of TV rooms
                        xp = p - nx// then all remaining patients goes to TV rooms
                    else // else (means, no. of remaining patients are more than no. of TV rooms)
                        xp = x// then only x number of patients will be provided with TV rooms, where x is
                                // Total no. of TV rooms
                }
                total += xp * R1 + nxp * R2// Total revenue of one day will be xp*R1 + nxp*R2
            }
            if (total >= T// if total revenue is greater than or equals to the target revenue
                break// break the for loop
        }
        System.out.println(x);
    }
}



Don't forget to share this post.


Post a Comment

0 Comments