Deals on Cookies ---------------- It is lunch time again and I am starving. I wo
ID: 3907835 • 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
(Use the method stated below
public static int[] deal(int[][] deals, int maxCookie) int[][]
and also need to read the text files using Scanner)
Explanation / Answer
import java.util.Scanner;
public class CookieDeals {
static int N,M;
public static int[] deal(int[][] deals, int maxCookie)
{
int best=0,prev=0; //to find which deal is best
double small=0.0,temp=0.0;
temp=100.00;
prev=0;
for(int i=0;i<N-1;i++)
{
if(deals[i][0]<=maxCookie) //to check deal have less cookies than what person needs
{
small=deals[i][1]/deals[i][0]; //calculating new value of small
if(temp>small)
{
best=i;
temp=small;
prev=i;
}
else if(temp==small) //if deals have equal price
{
if(deals[prev][0]>=deals[i][0]) //check which deal has high no of cookies
best=prev;
else
{
best=i;
prev=i;
}
}
else if(temp<small)
{ //if price of small is greater than previous is the best deal
best=prev;
}
else
best=N;
}
}
return deals[best];
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
N=sc.nextInt(); //NUMBER OF DEALS AVAILABLE
M=sc.nextInt(); //MAXIMUM NUMBER OF COOKIES TO BUY
int[][] a= new int[N+1][2]; //DEALS
for(int i=0;i<N+1;i++)
{
for(int j=0;j<2;j++)
{
a[i][j]=sc.nextInt();
}
}
int[] ans=deal(a,M);
if(a[ans[0]][0]!=0)
System.out.println("Purchase "+a[ans[0]][0]+" cookies for $"+a[ans[0]][1]);
else
System.out.println("No good deals");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.