Dinner

题目描述

Little A is one member of ACM team. He had just won the gold in World Final. To celebrate, he decided to invite all to have one meal. As bowl, knife and other tableware is not enough in the kitchen, Little A goes to take backup tableware in warehouse. There are many boxes in warehouse, one box contains only one thing, and each box is marked by the name of things inside it. For example, if “basketball” is written on the box, which means the box contains only basketball. With these marks, Little A wants to find out the tableware easily. So, the problem for you is to help him, find out all the tableware from all boxes in the warehouse.

(The tableware only contains: bowl, knife, fork and chopsticks.)

输入要求

There are many test cases. Each case contains one line, and one integer N at the first, N indicates that there are N boxes in the warehouse. Then N strings follow, each string is one name written on the box.

输出要求

For each test of the input, output all the name of tableware.

假如输入

3 basketball fork chopsticks
2 bowl letter

应当输出

fork chopsticks
bowl

提示

The tableware only contains: bowl, knife, fork and chopsticks.

Java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.io.BufferedInputStream;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));
        Set<String> set = new HashSet<String>();
        set.add("bowl");
        set.add("knife");
        set.add("fork");
        set.add("chopsticks");
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int i = 0;
            for (; i < n; ++i) {
                String tmp = scanner.next();
                if (set.contains(tmp)) {
                    System.out.print(tmp);
                    ++i;
                    break;
                }
            }
            for (; i < n; ++i) {
                String tmp = scanner.next();
                if (set.contains(tmp)) {
                    System.out.print(" " + tmp);
                }
            }
            System.out.println();
        }
    }
}

打个小广告

欢迎加入我的小专栏「基你太美」一起学习。