zoukankan      html  css  js  c++  java
  • 2001. Counting Sheep

     
    After a long night of coding, Charles Pearson Peterson is having trouble sleeping. This is not only because he is still thinking about the problem he is working on but also due to drinking too much java during the wee hours. This happens frequently, so Charles has developed a routine to count sheep. Not the animal, but the word. Specifically, he thinks of a list of words, many of which are close in spelling to "sheep", and then counts how many actually are the word "sheep". Charles is always careful to be case-sensitive in his matching, so "Sheep" is not a match. You are to write a program that helps Charles count "sheep".

    Input

    Input will consist of multiple problem instances. The first line will consist of a single positive integer n ≤ 20, which is the number of problem instances. The input for each problem instance will be on two lines. The first line will consist of a positive integer m ≤ 10 and the second line will consist of m words, separated by a single space and each containing no more than 10 characters.

    Output

    For each problem instance, you are to produce one line of output in the format:

    Case i: This list contains n sheep.

    The value of i is the number of the problem instance (we assume we start numbering at 1) and n is the number of times the word "sheep" appears in the list of words for that problem instance. Two successive lines should be separated by a single blank line, but do not output any trailing blank line.

    Sample Input

    4
    5
    shep sheeps sheep ship Sheep
    7
    sheep sheep SHEEP sheep shepe shemp seep
    10
    sheep sheep sheep sheep sheep sheep sheep sheep sheep sheep
    4
    shape buffalo ram goat
    

    Sample Output

    Case 1: This list contains 1 sheep.
    
    Case 2: This list contains 3 sheep.
    
    Case 3: This list contains 10 sheep.
    
    Case 4: This list contains 0 sheep.
    



    Source: East Central North America 2000 Practice

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 char str[150];
     5 char sheep[6] = "sheep";
     6 
     7 int main () 
     8 { 
     9     int m,n;
    10     int numCount;
    11     scanf("%d",&m);
    12     int i=0;
    13     while(i<m)
    14     {
    15         numCount=0;
    16         scanf("%d",&n);
    17         getchar();
    18         gets(str);
    19         char *ptr = str;
    20         while(n--)
    21         {
    22             int j;
    23             if((memcmp(ptr,sheep,5) == 0)&&((*(ptr+5) == ' ')||(*(ptr+5) == '')))
    24             {
    25                 numCount++;
    26                 ptr+=6;
    27             }
    28             else
    29             {
    30                 for(j=0;j<11;j++)
    31                 {
    32                     if(*ptr++ != ' ')
    33                         continue;
    34                     break;
    35                 }
    36             }              
    37         }
    38         printf("Case %d: This list contains %d sheep.
    ",++i,numCount);
    39         if(i<m)
    40             printf("
    ");
    41         numCount = 0; 
    42     }
    43     return 0;
    44 }
  • 相关阅读:
    POJ1028 Web Navigation【堆栈+模拟】
    UVa10276 HDU1329 ZOJ1239 Hanoi Tower Troubles Again!【递推函数+打表】
    UVALive5369 UVa732 HDU1515 ZOJ1004 Anagrams by Stack【DFS+堆栈】
    HDU5776 sum【前缀和+模除】
    POJ1844 Sum【水题+数学题】
    AOJ0558 Cheese【BFS】
    POJ3009 Curling 2.0【DFS】
    HDU1163 Eddy's digital Roots(解法二)【快速模幂+九余数定理】
    HDU1210 Eddy's 洗牌问题【递推函数+模拟】
    Vijos P1571 笨笨的导弹攻击【最长上升子序列+DP】
  • 原文地址:https://www.cnblogs.com/yushuo1990/p/4340980.html
Copyright © 2011-2022 走看看