Advertisement
Spocoman

04. Clever Lily

Aug 27th, 2024 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class CleverLily {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int age = Integer.parseInt(scanner.nextLine());
  7.         double machinePrice = Double.parseDouble(scanner.nextLine());
  8.         double toyPrice = Double.parseDouble(scanner.nextLine());
  9.  
  10.         double sum = 0;
  11.         int toy = 0, birthday = 10;
  12.  
  13.         for (int i = 1; i <= age; i++) {
  14.             if (i % 2 == 0) {
  15.                 sum += birthday - 1;
  16.                 birthday += 10;
  17.             } else {
  18.                 toy++;
  19.             }
  20.         }
  21.  
  22.         sum += toy * toyPrice;
  23.  
  24.         if (sum >= machinePrice) {
  25.             System.out.printf("Yes! %.2f\n", sum - machinePrice);
  26.         } else {
  27.             System.out.printf("No! %.2f\n", machinePrice - sum);
  28.         }
  29.     }
  30. }
  31.  
  32. ИЛИ:
  33.  
  34. import java.util.Scanner;
  35.  
  36. public class CleverLily {
  37.     public static void main(String[] args) {
  38.         Scanner scanner = new Scanner(System.in);
  39.         int age = Integer.parseInt(scanner.nextLine());
  40.         double machinePrice = Double.parseDouble(scanner.nextLine());
  41.         double toyPrice = Double.parseDouble(scanner.nextLine());
  42.  
  43.         double sum = 0;
  44.         int toy = 0, birthday = 10;
  45.  
  46.         for (int i = 1; i <= age; i++) {
  47.             if (i % 2 == 0) {
  48.                 sum += birthday - 1;
  49.                 birthday += 10;
  50.             } else {
  51.                 toy++;
  52.             }
  53.         }
  54.  
  55.         sum += toy * toyPrice;
  56.  
  57.         System.out.printf(sum >= machinePrice ?"Yes! %.2f\n" : "No! %.2f\n", Math.abs(sum - machinePrice));
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement