[P1580] yyy loves Easter_Egg I

Link:

P1580 传送门

Solution:

拿来练练字符串的读入

1、$gets()$相当于$c++$中的$getline()$,但返回值为指针!(无数据时为NULL)

(都读入换行符,并将其舍弃)

2、$sscanf(起始指针,.....,......)$可以实现从另一个字符串读入

3、$strcmp$和$memcmp$都是相同时返回值为0

4、$count(begin,end,value)$能实现各个容器中的个数查找

5、$strstr(s1,s2)$能查找$s1$中第一个$s2$的位置,没有子串$s2$时返回$NULL$!

Tips:

1、字符串中的查找函数未找到时返回的是$NULL$

对$NULL$调用会RE,使用前一定要判断

2、这道题数据出的不太好,结束符是一个空格……(不是回车或EOF)

Code:

#include <bits/stdc++.h>

using namespace std;
const int MAXN=1005;
int cur=1;
char text[MAXN],fst[MAXN],n1[MAXN],n2[MAXN];

int main()
{
    gets(text);
    sscanf(strstr(text,"@"),"@yyy loves %s",fst);
    while(++cur)
    {
        gets(text);
        if(strlen(text)==1) break;
        
        sscanf(text,"yyy loves %s",n1);
        if(!strcmp(n1,fst))
            return printf("Successful @yyy loves %s attempt",fst),0;
        //调用指针前要判断
        if(strstr(text,"@")!=NULL) sscanf(strstr(text,"@"),"@yyy loves %s",n2);
        if(count(text,text+strlen(text),'@')!=1 || strcmp(n2,fst))
        {//注意对count函数的使用
            printf("Unsuccessful @yyy loves %s attempt\n",fst);
            printf("%d\nyyy loves %s",cur,n1);return 0;
        }
    }
    printf("Unsuccessful @yyy loves %s attempt\n",fst);
    printf("%d\nGood Queue Shape",cur-1);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/newera/p/9285641.html
yyy
今日推荐