Problem 1 – Hungry Garfield
hristo_skr avatar hristo_skr 2 Точки

/Programming Basics Exam 2016/ Проблем с давана на изпит задача

Здравейте, отскоро се упражнявам/уча JAva basics и решвам една задачка давана на изпит през м.февруари 2016г. Задачата е следната  :

Problem 1 – Hungry Garfield

You know that Garfield is a very lazy cartoon cat that is always hungry and, just like you and me, likes Italian food very much. His favorite dishes are pizza, lasagna and sandwiches. John is a talented cartoonist and sometimes he goes on a business trip here and there and Garfield has to stay home alone and take care of himself. Every time John is away for a long time he gives Garfield some money to make sure he is well fed. Garfield is a very smart cat, he can talk, he can buy pizza himself, but he doesn’t care about calculations, because cats don’t care about anything else but food and a good nap. He asks you to make a simple program that calculates whether the money John gave him would be enough to eat all the pizzas, lasagnas and sandwiches he wants.

You are given the sum of money (in dollars) that John gives the prices of pizza, lasagna and sandwiches in leva. Then you will be given the quantity of pizza, lasagna and sandwiches he would like to eat in order to be well fed. You have to convert all the money to dollars. If the money he wants to spend is equal or less than the money John gave him, he will be satisfied, otherwise he will be hungry and angry at John.

Давам следното решение в :

package Probem01;

import java.util.Scanner;

public class HungryGarfield {
    public static void main(String[] args) {
        Scanner input = new Scanner(;
        Double a = input.nextDouble();
        Double b = input.nextDouble();
        Double c = input.nextDouble();
        Double d = input.nextDouble();
        Double e = input.nextDouble();
        int f = input.nextInt();
        int g = input.nextInt();
        int j = input.nextInt();
        double z = (c / b) * f + (d / b) * g + (e / b) * j;
        double k = a - z;
        double l = z - a;
        if (k >= 0) {
            System.out.printf("Garfield is well fed, John is awesome. Money left: $%.2f.", k);
        } else {
            System.out.printf("Garfield is hungry. John is a badass. Money needed: $%.2f.", l);


и ми дава 70/100. Въпросът ми е къде ми е грашката или какво  съм изпуснал? Благодаря.

Programming Basics
MartinBG avatar MartinBG 2129 Точки

Както писаха и колегите по-горе, задачата на Java се решава с BigDecimal - грозно и ненужно сложно решение, но това е начинът да мине със 100/100 в Judge.



hristo_skr avatar hristo_skr 2 Точки

Благодаря  за отговора само, че BigDecimal не съм  упражнявал още и трябва даго разгледам подробно. Мерси!


KaiFouch avatar KaiFouch 0 Точки

Wow. è stato bello conoscere un tale talentuoso gatto di nome Garfield. E puoi anche visitare per ottenere giochi da casinò a basso prezzo. Sì, il calcolo non è così facile nemmeno gli umani a volte possono fare. E penso che la programmazione che hai realizzato per lui sia favolosa.

27/03/2020 15:32:30