• HDU2024 C语言合法标识符


    C语言合法标识符

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 110554    Accepted Submission(s): 41760

    Problem Description

    输入一个字符串,判断其是否是C的合法标识符。

    Input

    输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。

    Output

    对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出"yes",否则,输出“no”。

    Sample Input

    3 12ajf fi8x_a ff ai_2

    Sample Output

    no yes no

     

    这几天快要被字典树、背包、两天一次的比赛给虐死了,我简直是不要太菜了,次次排老末,所以刷刷水题找找自信。。。。

    这题确实很简单,但是两种做法的细节要注意一下。

    分别是getline()的用法和gets()的用法,还要注意输入测试次数n后要写getchar(),不然n会被当作测试数据来判断。

    代码一:

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    int main(){
        int n;
        int len;
        int sum;
        string ch;
        cin >> n;
        getchar();
        while (n--){
            getline(cin,ch);
            len = ch.length();
            sum = 0;
            if (ch[0] >= '0'&&ch[0] <= '9') cout << "no" << endl;
            else{
                for (int j = 0; j < len; j++){
                    if ((ch[j] >= '0'&&ch[j] <= '9') || (ch[j] >= 'a'&&ch[j] <= 'z') || (ch[j] >= 'A'&&ch[j] <= 'Z') || (ch[j] == '_'))
                        sum++;
                }
                if (sum == len) cout << "yes" << endl;
                else cout << "no" << endl;
            }
        }
        return 0;
    }

     

    代码二:

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    int main(){
        int n;
        int len;
        int sum;
        char ch[60];
        cin >> n;
        getchar();
        while (n--){
            gets(ch);
            len = strlen(ch);
            sum = 0;
            if (ch[0] >= '0'&&ch[0] <= '9') cout << "no" << endl;
            else{
                for (int j = 0; j < len; j++){
                    if ((ch[j] >= '0'&&ch[j] <= '9') || (ch[j] >= 'a'&&ch[j] <= 'z') || (ch[j] >= 'A'&&ch[j] <= 'Z') || (ch[j] == '_'))
                        sum++;
                }
                if (sum == len) cout << "yes" << endl;
                else cout << "no" << endl;
            }
        }
        return 0;
    }
    天晴了,起飞吧
  • 相关阅读:
    洛谷 P6622
    洛谷 P6619
    LOJ 3188
    CF::Gym 102174G
    eJOI2017~2019
    洛谷 P6313
    洛谷 P6305
    JSOI2020 酱油记
    洛谷 P6234
    CodeForces 1334F
  • 原文地址:https://www.cnblogs.com/jianqiao123/p/11234757.html
Copyright © 2020-2023  润新知