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();
        }
    }
}

打个小广告

欢迎加入我的知识星球「基你太美」,我会在星球中分享 AucFrame 框架、大厂面经、AndroidUtilCode 更详尽的说明…一切我所了解的知识,你可以通过支付进入我的星球「基你太美」进行体验,加入后优先观看星球中精华的部分,如果觉得星球的内容对自身没有收益,你可以自行申请退款退出星球,也没必要加我好友;如果你已确定要留在我的星球,可以通过扫描如下二维码(备注:基你太美+你的星球昵称)加我个人微信,方便我后续拉你进群(PS:进得越早价格越便宜)。

我的二维码