南阳 dinner

来源:互联网 发布:马哥python 编辑:程序博客网 时间:2024/04/28 06:46

Dinner

时间限制:100 ms  |  内存限制:65535 KB
难度:1
描述
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.
输入
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 chopsticks2 bowl letter
样例输出
fork chopsticksbowl
提示

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


此题到是没什么太大的难度,需要注意一下输出格式,最后一个输出的单词后面直接接着回车,而不是接着一个空格之后接回车!

  因为不知什么时候是最后一个,所以不能前面”*** “,剩下最后一个”***\n“的形式输出,

      而应第一个输出时后面没有空格,后面再输出时前加空格,当退出循环后输出回车的形式输出;

本题大意如下:

                    小A去仓库区餐具,可是仓库中还有别的货物,我们需要做的就是将标有餐具英文的箱子找出,输出到屏幕上,输入数据有多组,每组数据首先输入一个n,代表有n个箱子,随后输入n个单词,查处餐具后按  *** *** **\n的形式输出。

 代码如下:  



#include<iostream>using namespace std;int main(){    string s[1000],r,t[5]={"bowl","knife","fork","chopsticks"};     int n,k;    while( cin>>n)   //多组输入n;    {k=0;          //k用来判断空格输出;        for(int i=0;i<n;i++)        {            cin>>s[i];            if(s[i]==t[0]||s[i]==t[1]||s[i]==t[2]||s[i]==t[3])  //判断输入的字符串是否为餐具;            {    if(!k) cout<<s[i];    //只有第一个输入没有空格,此时k为0;                 else                    cout<<" "<<s[i];  //除第一个单词外,输出前均需有空格;                     k++;    //只有有单词为餐具时k值才增加,            }        }        cout<<endl;   //结束时需要加回车;    }    return 0;}


0 0
原创粉丝点击