Counting Sheep

题目描述:

共有n ( n<= 20 )组数据,其中每组数据有一个正整数m( m <= 10),然后是 m 个单词。统计其中有多少个“sheep” 。单词大小写敏感,因而“Shepp”等不匹配。

题目解析:

按照题目要求,总结规则如下:
(1)计算每组数据包含多少个sheep,注意大小写敏感
(2)相邻的两组输出之间包含一个空行

代码如下

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    
    
    int n,m,c=1,count;
    string str;
    cin>>n;
    while(n--)
    {
    
    
        if(c>1)
            cout<<endl;    //相邻的两组输出之间包含一个空行
        cin>>m;
        count=0;
        for(int i=0;i<m;i++)
        {
    
    
            cin>>str;
            if(str=="sheep")   count++;
        }
        cout<<"Case"<<c++<<": This list contains "
        <<count<<" sheep."<<endl;
    }
    return 0;
}

输入输出样例

4
5
shep sheeps sheep ship Sheep
Case1: This list contains 1 sheep.

7
sheep sheep SHEEP sheep shepe shemp seep
Case2: This list contains 3 sheep.

10
sheep sheep sheep sheep sheep sheep sheep sheep sheep sheep
Case3: This list contains 10 sheep.

4
shape buffaio ram qoat
Case4: This list contains 0 sheep.

猜你喜欢

转载自blog.csdn.net/qq_51907130/article/details/112167478