Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Deals on Cookies ---------------- It is lunch time again and I am starving. I wo

ID: 3907787 • Letter: D

Question



Deals on Cookies ---------------- It is lunch time again and I am starving. I would like to buy some cookies from a small Cafe, named Cafe CS201, owned and operated by CSC201. Cafe CS201 offen offers discounts for buying multiple cookies. I wonder which of the discounts provides the best values. For this problem, please write a program named CookieDeals.java to help me find the best deal.
Requirements Implement the method as specified below:
public static int[] deal(int[][] deals, int maxCookie)
int[][] deals include all available deals. For example, if deal={{2,5},{6,15}}, it means there are two deals: 2 cookies for 5 dollars and 6 cookies for 15 dollars.
maxCookie specifies the max number of cookie I want to buy. I can buy less than the maxCookie, but never buy more than that number. This method returns a two-element int array with the first number is the number of cookies in the best deal and the second number is the price for that deal.
Input The input contains multiple scenarios. Each scenario starts with a line containing two numbers N ( 1<= N <=10) and M( 1<= M <=20), N means there are N deals available, M means the max amount of cookies I want to buy (I may buy less if I get the best deal). Following N lines, each line has two numbers: x and y which represent an offer to buy x cookies for $y. The input will terminated by a line containing the characters 0 0.
Output For each scenarios, print Purchase x cookies for $y for the best offer that matches requirements (the best deal but less or equal to the number of cookies I want to buy). If there are multiple best offers, print the one which buys more tickets (but still be less or equals to the number of cookies I want to buy). If there is no suitable offer (may require to buy more cookies than I want to), print No good deals.
Sample input data Sample Output ------------------------------------------------------------------ 5 6 Purchase 6 cookies for $6 12 10 Purchase 3 cookies for $5 9 8 Purchase 1 cookies for $3 6 6 Purchase 2 cookies for $4 3 3 No good deals 1 2 3 5 1 3 3 5 4 7 3 2 3 5 1 3 4 7 3 2 3 6 1 2 2 4 1 3 4 10 0 0
? AA case1.txt?:case2.txt? 12 5 2 1 2 3 6 8 40 0

Explanation / Answer

CookieDeals.java

import java.util.Scanner;

//CookieDeals class begins

public class CookieDeals

{  

       //main() begins

       public static void main(String[] args)

       {

             //Scanner object for input

             Scanner scan = new Scanner(System.in);

             int n, m;

             int[][] deals;

             //Infinite do while loop runs until both n and m are 0

             do

             {

                    //Input n amd m (number of deals and maximum number of cookies)

                    n = scan.nextInt();

                    m = scan.nextInt();

                    //Break loop if both n and m are 0

                    if(n == 0 && m == 0)

                           break;

                    //deals of size n * 2

                    deals = new int[n][2];

                    //Input deals details (number of cookies in that deal and price)

                    for(int i = 0; i < n; i++)

                           for(int j = 0; j < 2; j++)

                                 deals[i][j] = scan.nextInt();

                    //Call deal with deals and m as parameters and save the returned array in arr[]

                    int[] arr = deal(deals, m);

                    //If arr[0] is 0 and arr[1] is 1, print "No good deals"

                    if(arr[0] == 0 && arr[1] == 1)

                           System.out.println("No good deals");

                    //Otherwise print the deal

                    else

                           System.out.println("Purchase " + arr[0] + " cookies for $" + arr[1]);

             }while(true);

       }

      

       //Function to return the best suitable deal, it returns {0, 1} is no deal matches

       public static int[] deal(int[][] deals, int maxCookie)

       {

             //A temporary array to save the best deal at a given time

             int[] bestDeal = {0, 1};

             //Loop to traverse through each deal

             for(int i = 0; i < deals.length; i++)

                    //Proceed only if number of cookies in the deal is less than or equal to maximum cookies allowed

                    if(deals[i][0] <= maxCookie)

                           //Proceed only if the number of cookies in a dollar of deal[i] is greater than the number of cookies in a dollar of bestDeal[i]

                           //Number of cookies in a dollar = Number of cookies / Price in dollars

                           if((float)deals[i][0] / deals[i][1] >= (float)bestDeal[0] / bestDeal[1])

                           {

                                 //If this deal has more number of cookies than bestDeal, then update it

                                 if(deals[i][0] > bestDeal[0])

                                 {

                                        bestDeal[0] = deals[i][0];

                                        bestDeal[1] = deals[i][1];

                                 }

                           }

             //Return the array having best deal

             return bestDeal;

       }

}

Outputs of the sample test cases given in the question are also provided.

Input 1:

5 6
12 10
9 8
6 6
3 3
1 2
3 5
1 3
3 5
4 7
3 2
3 5
1 3
4 7
3 2
3 6
1 2
2 4
1 3
4 10
0 0

Output 1:

Purchase 6 cookies for $6
Purchase 3 cookies for $5
Purchase 1 cookies for $3
Purchase 2 cookies for $4
No good deals

Input 2:

2 5
1 2
6 8
0 0

Output 2:

Purchase 1 cookies for $2

Input 3:

2 5
1 2
6 8
1 2
3 2
0 0

Output 3:

Purchase 1 cookies for $2
No good deals

Input 4:

3 4
1 2
2 3
2 4
0 0

Output 4:

Purchase 2 cookies for $3

Input 5:

Last few lines may differ from those given in the question as the image is not clear.

2 5
1 2
6 8
1 2
3 2
5 6
12 10
9 8
6 6
3 3
1 2
3 5
1 3
3 5
4 7
3 2
9 5
1 3
4 7
0 0

Output 5:

Purchase 1 cookies for $2
No good deals
Purchase 6 cookies for $6
Purchase 3 cookies for $5
Purchase 1 cookies for $3

Kindly give a thumbs up, if found useful. Comment for queries. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote