bobby_k

Untitled

Mar 23rd, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package com.company.EXAM_07_01_18;
  2. import java.util.Scanner;
  3.  
  4. public class Juice2 {
  5.  
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         int r = Integer.parseInt(scanner.nextLine());
  10.         int s = Integer.parseInt(scanner.nextLine());
  11.         int c = Integer.parseInt(scanner.nextLine());
  12.         int juiceLimit = Integer.parseInt(scanner.nextLine());
  13.         double juiceCurrent = 0;
  14.         double raspVol = 4.5;
  15.         double strawVol = 7.5;
  16.         double cherryVol = 15;
  17.         double maxJuice = -1;
  18.         int maxRasp = 0;
  19.         int maxStraw = 0;
  20.         int maxCherry = 0;
  21.         String result = "";
  22.  
  23.         for (int rasp = 0; rasp <= r; rasp++) {
  24.             for (int straw = 0; straw <= s; straw++) {
  25.                 for (int cherry = 0; cherry <= c; cherry++) {
  26.                     juiceCurrent = cherry * cherryVol + straw * strawVol + rasp * raspVol;
  27.                     if (juiceCurrent <= juiceLimit) {
  28.                         if (juiceCurrent > maxJuice) {
  29.                             maxRasp = rasp;
  30.                             maxStraw = straw;
  31.                             maxCherry = cherry;
  32.                             maxJuice = juiceCurrent;
  33.                         }
  34.                     }
  35.                 }
  36.             }
  37.         }
  38.  
  39.         for (int rasp = 0; rasp <= r; rasp++) {
  40.             for (int straw = 0; straw <= s; straw++) {
  41.                 for (int cherry = 0; cherry <= c; cherry++) {
  42.                     juiceCurrent = cherry * cherryVol + straw * strawVol + rasp * raspVol;
  43.                     if (juiceCurrent == maxJuice) {
  44.                         if (cherry > maxCherry ||
  45.                                 cherry == maxCherry && straw > maxStraw ||
  46.                                 cherry == maxCherry && straw == maxStraw && rasp > maxRasp) {
  47.                             result = String.format("%d Raspberries, %d Strawberries, %d Cherries. Juice: %.0f ml.",
  48.                                     rasp, straw, cherry, juiceCurrent);
  49.                         }
  50.                     }else {
  51.                             result = String.format("%d Raspberries, %d Strawberries, %d Cherries. Juice: %.0f ml."
  52.                                     , maxRasp, maxStraw, maxCherry, maxJuice);
  53.  
  54.                         }
  55.                     }
  56.                 }
  57.             }
  58.  
  59.         System.out.println(result);
  60.     }
  61. }
Add Comment
Please, Sign In to add comment