[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;
}

 

转载于:https://www.cnblogs.com/newera/p/9285641.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值