Advertisement
coasterka

CountBeers

May 27th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class CountBeers {
  6.  
  7.     public static void main(String[] args) {
  8.        
  9. /*      Input:
  10.         14 stacks
  11.         5 beers
  12.         9 stacks
  13.         22 beers
  14.         9 beers
  15.         End
  16.        
  17.         Output:
  18.         24 stacks + 16 beers
  19.        
  20.         hint: 20 beers = 1 stack */
  21.        
  22.         Scanner scan = new Scanner(System.in);
  23.         List<String> linesList = new ArrayList<String>();
  24.         String inputLine = scan.nextLine();
  25.         String end = "End";
  26.         String stacks = "stacks";
  27.         String beers = "beers";
  28.         int stacksCount = 0;
  29.         int beersCount = 0;
  30.        
  31.         while(!inputLine.equals(end)){
  32.             linesList.add(inputLine);
  33.             inputLine = scan.nextLine();
  34.             if (inputLine.equals(end)) {
  35.                 break;
  36.             }
  37.         }
  38.        
  39.         for (String line : linesList) {
  40.             String[] lineContent = new String[2];
  41.             lineContent = line.split(" ");
  42.            
  43.             if (lineContent[1].equals(beers)) {
  44.                 beersCount += Integer.parseInt(lineContent[0]);
  45.                 if (beersCount >= 20) {
  46.                     stacksCount++;
  47.                     beersCount-=20;
  48.                 }
  49.             }
  50.             if (lineContent[1].equals(stacks)) {
  51.                 stacksCount += Integer.parseInt(lineContent[0]);
  52.                 }
  53.             }
  54.             System.out.print(stacksCount + " stacks + ");
  55.             System.out.println(beersCount + " beers");             
  56.         }
  57.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement