Power OJ 2846: 防不住AK的防AK

传送门

  • stl一下就出来了,stl基础不好在这里记录一下,string自带find函数,可以直接用来查找,在这里如果找不到的话会返回一个特别的值string::pos,然后就可以直接判断了
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#define lson l, mid, root << 1
#define rson mid + 1, r, root << 1 | 1
#define father  l , r , root
#define lowbit(x) ( x & ( - x ) )
#define mod 1000000007
using namespace std;
const int maxn = 5e5+10;
const int inf = 0x3f3f3f3f;
int main()
{
    
    
	string s;
	int t;
	cin>>t;
	while(t--){
    
    
        cin>>s;
        if(s.find("520")==string::npos){
    
    
            cout<<"Yes"<<endl;
        }else{
    
    
            cout<<"No"<<endl;
        }
	}
    return 0;
}

当然还可以用getchar(实际上用不到这么麻烦)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,t,x,y=0,z=0;
    char c='1',c1;
    scanf("%d",&n);
    c1=getchar();
    while(n--){
        t=0;
        x=0;
        while(c!=10){
            c=getchar();
            //printf("%c",c);
            if(x==0&&c=='5'){
                x=1;continue;
            }
            else if(x==0&&c!='5'){
                x=0;
                continue;
            }
            if(x==1&&c=='2'){
                x=2;continue;
 
            }
            else if(x==1&&c!='2'){
                x=0;
                if(c=='5')
                x=1;
                continue;
            }
            if(x==2&&c=='0')
            {
                t=1;
            }
            else if(x==2&&c!='0')
            x=0;
        }
        if(n==0)
        t==0?printf("Yes"):printf("No");
        else
        t==0?printf("Yes\n"):printf("No\n");
        c='1';
    }
    return 0;
}

还可以直接读入字符串

#include<stdio.h>
#include<string.h>
int main()
{
    
    
    long int t,i,j,n;
    int m;
    char a[100000]={
    
    0};
    scanf("%ld",&t);
    getchar();
    for(i=0;i<t;i++)
    {
    
    
        m=0;
        gets(a);
        n=strlen(a);
        for(j=0;j<n-2;j++)
        {
    
    
            if(a[j]=='5'&&a[j+1]=='2'&&a[j+2]=='0')
            {
    
    
                m=1;
            }
        }
        if(m)
        printf("No\n");
        else
        printf("Yes\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43715171/article/details/97814681
今日推荐