Question

In: Computer Science

Java Supermegastaurants have overtaken megastaurants as the latest trend in dining. A supermegastaurant is characterized by...

Java

Supermegastaurants have overtaken megastaurants as the latest trend in dining. A supermegastaurant is characterized by enormous menus, four categories of food, and a rule that you must choose exactly one item from each category. You have won a gift certificate of M dollars to use at a megastaurant. In order to maximize your dining value, you wish to choose an item from each category such that the price is as close to M as possible.

Input Format

Input begins with five integers, A, B, C, D, and M, subject to the following constraints:

1 <= A, B, C, D <= 2000
1 <= M <= 2,147,000,000

Next is a line with A positive integers, each less or equal to than 2,147,000,000. These integers are the prices of items in the first category.

Next is a line with B positive integers, each less or equal to than 2,147,000,000. These integers are the prices of items in the second category.

Next is a line with C positive integers, each less or equal to than 2,147,000,000. These integers are the prices of items in the third category.

Next is a line with D positive integers, each less or equal to than 2,147,000,000. These integers are the prices of items in the fourth category.

Output Format

You are to output the total cost of the meal with a price closest to M.

Note: This cost can be equal to M, greater than M, or less than M. In the case of a tie costs, choose the cheaper of the two costs.

Sample Input

3 2 1 2 50

15 10 49

11 17

10

13 23

Sample Output

50

Explanation

Here you can choose items that exactly equal M:

10 + 17 + 10 + 13 = 50

1) An argument of the correctness of your code:

a) State a loop invariant.

b) Argue that the loop invariant is true before the loop starts executing.

c) Argue that the loop invariant remains true after each iteration of the loop.

d) Argue for the correctness of your algorithm based on the loop invariant.

Solutions

Expert Solution

Program:

//importing the library

import java.util.*;

//start of main class

public class Main

{
//start of main method
  
   public static void main(String[] args) {
  
       Scanner read=new Scanner(System.in);
  
   //declaration of variables
  
       int A,B,C,D,M;
  
       int max=0,min=0,e=0;

//reading A,B,C,D and M from user
  
       A=read.nextInt();
      
       B=read.nextInt();
      
       C=read.nextInt();
      
       D=read.nextInt();
      
       M=read.nextInt();
      
       //decalring arrays to store items
      
       int a[]=new int[A];
      
       int b[]=new int[B];
      
       int c[]=new int[C];
      
       int d[]=new int[D];
      
       //taking item numbers from user and storing them in arrays
      
       for(int i=0;i<A;i++){
      
       a[i]=read.nextInt();
      
      
       }
  
       for(int i=0;i<B;i++){
  
       b[i]=read.nextInt();
  
       }
  
       for(int i=0;i<C;i++){
  
       c[i]=read.nextInt();
  
       }
  
       for(int i=0;i<D;i++){
  
       d[i]=read.nextInt();
  
       }
      
       //verifying the items which are greter than M and less than M and equal to M
      
       for(int i=0;i<A;i++){
  
       for(int j=0;j<B;j++){
  
       for(int k=0;k<C;k++){
  
       for(int l=0;l<D;l++){
   //if items are equal to M then print M
       if(a[i]+b[j]+c[k]+d[l]==M){
  
       e=1;
  
       System.out.println(M);
  
       }
  
       else if((a[i]+b[j]+c[k]+d[l]<max) && (a[i]+b[j]+c[k]+d[l])<50 ){
  
       max=(a[i]+b[j]+c[k]+d[l]);
  
       }
  
       else if((a[i]+b[j]+c[k]+d[l]>min) && (a[i]+b[j]+c[k]+d[l])>50 ){
  
       min=(a[i]+b[j]+c[k]+d[l]);
  
       }
      
       }
  
       }
  
       }
  
       }
      
   //if items are not equal to M
   //then we will check the closest item numbers
   //that may be greater than or less then M
       if(e==0){
  
       if( M-min > Math.abs(M-max)){
  
       System.out.println(max);
  
       }
  
       else{
       System.out.println(min);   
       }
   }
   }
}


Output:


Related Solutions

The latest trend in health care is the merging of hospitals to improve efficiency and quality...
The latest trend in health care is the merging of hospitals to improve efficiency and quality of care for the patient. When institutions merge so do departments to eliminate redundant services. As the manger of the lab, design an efficient laboratory operation to provide services at both institutions.
What is the latest trend of the development of multinational cooperations? (at least 1500 words, charts...
What is the latest trend of the development of multinational cooperations? (at least 1500 words, charts and data are required, and case study is preferred).
Describe one U.S. healthcare trend and at least three implications the trend could have for financial...
Describe one U.S. healthcare trend and at least three implications the trend could have for financial planning and decision-making within a healthcare organization, such as a hospital or clinic. Please include why the trend will have those implications.
Your remodeled home will have an open floor plan with the dining area, living area, and...
Your remodeled home will have an open floor plan with the dining area, living area, and kitchen in one large room. You will have a fireplace along one of the walls, and you plan to mount your TV above the fireplace. Your sofa will be directly across from the fireplace/TV, in the middle of the room. You want to have an electrical outlet installed on the floor so that it will be under the sofa. This will allow you to...
An economic crisis is always characterized by inflation, but sometimes characterized by high growth rates in...
An economic crisis is always characterized by inflation, but sometimes characterized by high growth rates in output. True or False? A debt-deflation spiral can lead to a Phillips Curve. True or False? An oil price ------------------ is a sudden increase or decrease in the nominal or real price of oil. This is an example of a------------------- -side shock. (Fill in the blanks) Stagflation refers to low------------------------ and high inflation. The US experienced a period of stagflation in the -------------- 's.(Fill...
18. Multiple sclerosis is characterized by exacerbations and remissions: a) T18. Multiple sclerosis is characterized by...
18. Multiple sclerosis is characterized by exacerbations and remissions: a) T18. Multiple sclerosis is characterized by exacerbations and remissions: a) True b) False rue a) True b) False 19. AIDS is the virus that is first diagnosed and treated; HIV is the stage where there is active disease: a) True b) False 20. Medications usually control epileptic seizures if taken regularly: a)true b)false
What themes have characterized American foreign policy in the years since the conclusion of the cold...
What themes have characterized American foreign policy in the years since the conclusion of the cold war? Explain at least three foreign policy decisions or military intervention in your response.
Suppose you have a production technology that can be characterized by a learning curve. Every time...
Suppose you have a production technology that can be characterized by a learning curve. Every time you increase production by 1 unit, your costs decrease by $6. The first unit costs you $64 to produce. 1)You estimate you can win another project for two more units. Have your returns diminished? Why or why not? 2)If you receive a request for 4 units, what is your break-even price?
What theme or themes have characterized American foreign policy in the years since the conclusion of...
What theme or themes have characterized American foreign policy in the years since the conclusion of the Cold War? Explain at least three foreign policy decisions or military interventions in your response.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT