A - Dinner (NBUT 1217)
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.
Input
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.
Output
For each test of the input, output all the name of tableware.
Sample Input
3 basketball fork chopsticks
2 bowl letter
Sample Output
fork chopsticks
bowl
Hint
The tableware only contains: bowl, knife, fork and chopsticks.
题目翻译:
小A是ACM团队的一员。他刚刚赢得了世界决赛的金牌。为了庆祝,他决定邀请所有人吃一顿饭。由于厨房里的碗、刀等餐具不够用,小A去仓库取备餐具。仓库里有很多箱子,一个箱子里只有一件东西,每个箱子都用里面东西的名字来标记。例如,如果盒子上写着“篮球”,这意味着盒子里只有篮球。有了这些记号,小A想轻而易举地找到餐具。所以,你的问题是帮他,从仓库里所有的箱子里找出所有的餐具。
输入
有许多测试用例。每个case包含一行,第一个是整数N, N表示仓库中有N个箱子。然后是N个字符串,每个字符串都是写在盒子上的一个名字。
输出
对于每次测试的输入,输出所有餐具的名称。
样例输入
3 篮球叉筷子
2 碗的信
样例输出
叉子筷子
碗
提示
餐具只包含:碗、刀、叉、筷。
思路:
直接暴力就可以,但是得注意输出空格的格式,每两个餐具之间有一个空格,但是最后一个输出的餐具后面没有空格输出,还有不用考虑餐具相同的情况,如果有两个 bowl 的话直接输出两个就完事。
AC代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#define M 1000
using namespace std;
string s;
int main()
{
int n;
while(~scanf("%d",&n))
{
int flag=0;
for(int i=0; i<n; i++)
{
cin>>s;
if(flag==0)
{
if((s=="bowl" || s=="knife" || s=="fork" || s=="chopsticks"))
{
cout<<s;
flag=1;//第一个不输出空格
}
}
else
{
if((s=="bowl" || s=="knife" || s=="fork" || s=="chopsticks"))
{
cout<<" "<<s;
}
}
}
cout<<'\n';
}
return 0;
}