Join Regular Classroom : Visit ClassroomTech

Miscellaneous Problem | One Egg Company | Codewindow.in

The company named “One Egg” which supplies eggs to retailers. They have M classes of eggs. Each class can have N no. of eggs (N can be same or it can vary class to class). They accept an order via mail for X eggs. In response, they confirm if they can supply the eggs with a “Thank you” note as well as the no. of eggs or with a “Sorry” note and the no. of eggs they can supply. They also mention the breakdown of eggs by class they will supply. The ordered eggs are adjusted against the different classes with the most no. of eggs adjusted first then the balance is adjusted against the second highest and so on.

The company named “One Egg” is a bit superstitious as well . If the no. of eggs ordered is greater than or equal to the total no. of eggs in stock then they retain 1 egg and responds back with the “Sorry” Note : If the classes have same number of eggs then class entered first should be selected to adjust. with total number of eggs in stock minus 1 and breakdown of eggs by class.

Input Format:
First line It contains 2 space-separated integers describing the respective values of M & X, the no. of eggs ordered. The following M lines contain an integer each indicating the no. of eggs available in each class Where M : the number of classes of eggs.

Output Format:
First line Described if X is less than total no. of Eggs then Print ” Thank you, your order for X eggs is accepted” Else if X is greater than or equal to total no. of Eggs then print ” Sorry, we can only supply (total number of Eggs in stock -1) eggs” Then M lines with three columns: 1st column – no of eggs available in each class 2nd column – Eggs allocated against each class for that order 3rd column – Balance Eggs
against each class.

Constraints:
• 1 ≤ M ≤ 20
• N ≥ 1
• X ≥ 1

Example 1
Input:
5 150
50
15
80
10
5

Example 2
Input:
4 250
80
50
70
20

Output:
Sorry, we can only supply 219 eggs
8 0 80 0
5 0 50 0
7 0 70 0
2 0 19 1

Explanation:
Total order of 250 eggs was greater than the total no of eggs 80+50+70+20 = 220. Therefore the sorry message. 250 was first adjusted against Class with first highest no of eggs 80. Therefore Balance of 250-80 = 170 was adjusted against second highest class of 70. Therefore Balance of 170-70 = 100 was then adjusted against 50. Therefore Balance of 100-50 = 50 then adjusted against 20. Where Balance is greater than last class of egg all but one egg is left in that last class.

Solution: In C

#include <stdio.h>
int main() {
    int m,x,i,a[1000],sum=0,s;
    scanf("%d %d",&m,&x);
    for(i=0;i<m;i++)
    {
        scanf("%d",&a[i]);
        sum=sum+a[i];
    }
    if(sum>x)
        printf("Thank you, your order for %d eggs are accepted\n",x);
    else
    {
        printf("Sorry, we can only supply %d eggs\n",sum-1);
        x=sum-1;
    }
    for(i=0;i<m;i++)
    {
        if(x>=a[i])
        {
            printf("%d\t%d\t%d\n",a[i],a[i],0);
            x=x-a[i];
        }
        else if(x<a[i])
        {
            s=a[i]-x;
            printf("%d\t%d\t%d\n",a[i],x,s);
            x=0;
        }
        else if(x==0)
            printf("%d\t%d\t%d\n",a[i],0,a[i]);
    }
    return 0;
}

Also Checkout

Recent Posts
Categories
Pages