zoukankan      html  css  js  c++  java
  • 杭电2024 C语言合法标识符

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

      开始真的对这题是一点头绪都没有,简直了。然后事实证明是我想多了,这题主要是把概念给弄清楚,其它的就是代码的简单实现了。有一个地方要注意,就是string类在cin的时候不会给你读取空格的,所以这就需要你用getline(cin,string),记得在cin>>n之后cin.get[],把那个回车符号给读取掉。

      C语言合法标识符的命名规则:1、首元素只能是下划线或是字母,2、除首元素外的元素只能是数字、下划线和字母),先判断输入的字符串首元素是否符合规则,接着再判断其他元素。要注意的是因为输入的是字符串,则在判断元素是否是数字时是与字符数字判断!!!

      附上ac代码:

      

    #include <iostream>
    #include<math.h>
    #include <iomanip>
    #include<cstdio>
    #include<string>
    #include<map>
    #include<vector>
    #include<list>
    #include<algorithm>
    #include<stdlib.h>
    #include<iterator>
    using namespace std;
    
    
    int main()
    {
        string input;
        int n;
        cin>>n;
        cin.get();//舍去回车符号
        while(n--)
        {
            int flag=0;
            int flag1=0;
            getline(cin,input);//必须是一行全部读入
            if(input[0]=='_'||(input[0]>='a'&&input[0]<='z')||(input[0]>='A'&&input[0]<='Z'))
            {
                flag=1;
            }
    
           int len=input.size();
           for(int i=1;i<len;i++)
           {
               if((input[i]>='0'&&input[i]<='9')||input[i]=='_'||(input[i]>='a'&&input[i]<='z')||(input[i]>='A'&&input[i]<='Z'))
            {
                flag1=1;
            }else
            {
                flag1=0;
                break;
            }
           }
    
           if(flag1==1&&flag==1)
           {
               cout<<"yes"<<endl;
           }
           else
           {
               cout<<"no"<<endl;
           }
        }
    
        return 0;
    }
  • 相关阅读:
    第三百六十九天 how can I 坚持
    第三百六十八天 how can I 坚持
    第三百六十四、五、六、七天 how can I 坚持
    POJ 1663:Number Steps
    POJ 2676:Sudoku 数独
    POJ 2488:A Knight's Journey 深搜入门之走马观花
    POJ 3050:Hopscotch
    51nod 1419:最小公倍数挑战
    POJ 1011:Sticks 经典搜索
    POJ 2362:Square 觉得这才算深度搜索
  • 原文地址:https://www.cnblogs.com/William-xh/p/6832717.html
Copyright © 2011-2022 走看看