Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company.EXAM_07_01_18;
- import java.util.Scanner;
- public class Juice2 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int r = Integer.parseInt(scanner.nextLine());
- int s = Integer.parseInt(scanner.nextLine());
- int c = Integer.parseInt(scanner.nextLine());
- int juiceLimit = Integer.parseInt(scanner.nextLine());
- double juiceCurrent = 0;
- double raspVol = 4.5;
- double strawVol = 7.5;
- double cherryVol = 15;
- double maxJuice = -1;
- int maxRasp = 0;
- int maxStraw = 0;
- int maxCherry = 0;
- String result = "";
- for (int rasp = 0; rasp <= r; rasp++) {
- for (int straw = 0; straw <= s; straw++) {
- for (int cherry = 0; cherry <= c; cherry++) {
- juiceCurrent = cherry * cherryVol + straw * strawVol + rasp * raspVol;
- if (juiceCurrent <= juiceLimit) {
- if (juiceCurrent > maxJuice) {
- maxRasp = rasp;
- maxStraw = straw;
- maxCherry = cherry;
- maxJuice = juiceCurrent;
- }
- }
- }
- }
- }
- for (int rasp = 0; rasp <= r; rasp++) {
- for (int straw = 0; straw <= s; straw++) {
- for (int cherry = 0; cherry <= c; cherry++) {
- juiceCurrent = cherry * cherryVol + straw * strawVol + rasp * raspVol;
- if (juiceCurrent == maxJuice) {
- if (cherry > maxCherry ||
- cherry == maxCherry && straw > maxStraw ||
- cherry == maxCherry && straw == maxStraw && rasp > maxRasp) {
- result = String.format("%d Raspberries, %d Strawberries, %d Cherries. Juice: %.0f ml.",
- rasp, straw, cherry, juiceCurrent);
- }
- }else {
- result = String.format("%d Raspberries, %d Strawberries, %d Cherries. Juice: %.0f ml."
- , maxRasp, maxStraw, maxCherry, maxJuice);
- }
- }
- }
- }
- System.out.println(result);
- }
- }
Add Comment
Please, Sign In to add comment