Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class CountBeers {
- public static void main(String[] args) {
- /* Input:
- 14 stacks
- 5 beers
- 9 stacks
- 22 beers
- 9 beers
- End
- Output:
- 24 stacks + 16 beers
- hint: 20 beers = 1 stack */
- Scanner scan = new Scanner(System.in);
- List<String> linesList = new ArrayList<String>();
- String inputLine = scan.nextLine();
- String end = "End";
- String stacks = "stacks";
- String beers = "beers";
- int stacksCount = 0;
- int beersCount = 0;
- while(!inputLine.equals(end)){
- linesList.add(inputLine);
- inputLine = scan.nextLine();
- if (inputLine.equals(end)) {
- break;
- }
- }
- for (String line : linesList) {
- String[] lineContent = new String[2];
- lineContent = line.split(" ");
- if (lineContent[1].equals(beers)) {
- beersCount += Integer.parseInt(lineContent[0]);
- if (beersCount >= 20) {
- stacksCount++;
- beersCount-=20;
- }
- }
- if (lineContent[1].equals(stacks)) {
- stacksCount += Integer.parseInt(lineContent[0]);
- }
- }
- System.out.print(stacksCount + " stacks + ");
- System.out.println(beersCount + " beers");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement