HDU 2024 C语言合法标识符(笑)

http://acm.hdu.edu.cn/showproblem.php?pid=2024

是的,这题很简单微笑,但是1A也不是那么容易微笑,为什么把这么简单的题记录在博客呢,因为提醒自己要严谨,注意细节。

分析:

这题就是合法的命名规则。规定开头不能用数字,可以用大小写字母和下划线,下划线相当于字母一样,所以________也是命名合法的。必须把整个字符串遍历完,遇到奇怪的字符除了字母和下划线数字的就要打出no,no和yes也是小写的微笑

所以就是:

关键细节
没有什么坑的数据
关键词也不用考虑 就是小心点就行了

#include<cstdio>
#include<cstring>
#include<queue>
#include <iostream>
#include<algorithm>

using namespace std;

char str[55];
int main(){
   int T;
   cin>>T;
   int cnt;
   getchar();
   while(T--){
    int ans;
    gets(str);
    int len = strlen(str);
    int flag = 0;



    for(int i= 0;i<len;i++){

        if(str[0]>='0'&&str[0]<='9'){
            printf("no
");
            flag=1;
            break;
            }
        else if((str[i]<='z'&&str[i]>='a')||(str[i]<='Z'&&str[i]>='A')||str[i]=='_'||(str[i]>='0'&&str[i]<='9')){
               continue;
                }
                else{
                    printf("no
");
                    flag = 1;
                    break;

                }

    }
        if(!flag)
          printf("yes
");
   }


   return 0;
}


原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256721.html