Advertisement
Spocoman

Tourist Shop

Sep 10th, 2024
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         double budget = Double.parseDouble(scanner.nextLine()),
  7.                 price, totalSum = 0;
  8.         int counter = 0;
  9.         String product;
  10.         while (!(product = scanner.nextLine()).equals("Stop")) {
  11.             price = Double.parseDouble(scanner.nextLine());
  12.             counter++;
  13.             if (counter % 3 == 0) {
  14.                 price /= 2;
  15.             }
  16.             totalSum += price;
  17.             if (budget < totalSum) {
  18.                 break;
  19.             }
  20.         }
  21.  
  22.         if (product.equals("Stop")) {
  23.             System.out.printf("You bought %d products for %.2f leva.\n", counter, totalSum);
  24.         } else {
  25.             System.out.printf("You don't have enough money!\nYou need %.2f leva!\n", totalSum - budget);
  26.         }
  27.     }
  28. }
  29.  
  30. ИЛИ:
  31.  
  32. import java.util.Scanner;
  33.  
  34. public class Main {
  35.     public static void main(String[] args) {
  36.         Scanner scanner = new Scanner(System.in);
  37.         double budget = Double.parseDouble(scanner.nextLine()),
  38.                 price, totalSum = 0;
  39.         String product;
  40.  
  41.         for (int i = 1; i < 2147483647; i++) {
  42.             if ((product = scanner.nextLine()).equals("Stop")) {
  43.                 System.out.printf("You bought %d products for %.2f leva.\n", --i, totalSum);
  44.                 return;
  45.             }
  46.             price = Double.parseDouble(scanner.nextLine());
  47.             if (i % 3 == 0) {
  48.                 price /= 2;
  49.             }
  50.             totalSum += price;
  51.             if (budget < totalSum) {
  52.                 System.out.printf("You don't have enough money!\nYou need %.2f leva!\n", totalSum - budget);
  53.                 return;
  54.             }
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement