山东省赛Identifiers 之getchar()的使用


Identifiers

Time Limit: 1000MS Memory limit: 65536K

题目描述

 Identifier is an important concept in the C programming language. Identifiers provide names for several language elements, such as functions, variables, labels, etc.

An identifier is a sequence of characters. A valid identifier can contain only upper and lower case alphabetic characters, underscore and digits, and must begin with an alphabetic character or an underscore. Given a list of chararcter sequences, write a program to check if they are valid identifiers.

输入

 The first line of the input contains one integer, N, indicating the number of strings in the input. N lines follow, each of which contains at least one and no more than 100 characters. (only upper and lower case alphabetic characters, digits, underscore (" "), hyphen ("-"), period ("."), comma (","), colon (":"), semicolon (";"), exclamation mark ("!"), question mark ("?"), single and double quotation marks, parentheses, white space and square brackets may appear in the character sequences.)

输出

For each of the N lines, output "Yes" (without quote marks) if the character sequence contained in that line make a valid identifier; output "No" (without quote marks) otherwise.

示例输入

7
ValidIdentifier
valid_identifier
valid_identifier
0 invalid identifier
1234567
invalid identifier
adefhklmruvwxyz12356790 -.,:;!?'"()[]ABCDGIJLMQRSTVWXYZ

示例输出

Yes
Yes
Yes
No
No
No
No
#include 
#include
#include
using namespace std;
char a[101];
int i;
int main()
{
    int n,i;
   /// cin>>n;
    ///getchar();
    scanf("%d",&n);
    getchar();
    while(n--)
    {
        gets(a);
        int len=strlen(a);
        if((a[0]>='a'&&a[0]<='z')||(a[0]>='A'&&a[0]<='Z')||a[0]=='_')
        {
            for(i=1;i='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='Z')||a[i]=='_'||(a[i]>='0'&&a[i]<='9')))
                    break;
            }
            if(i>=len)
                cout<<"Yes"<
 
   
但是如果没有getchar(),应该这样输入,如下图:
 
   
总结:卧槽,终于懂了getchar()
因为你输入7后,有了一个回车,而getchar()会直接取走那个无用的回车键这个字符,相当于清除缓冲区
 
   
 
  

你可能感兴趣的:(山东省赛Identifiers 之getchar()的使用)