Identifiers
Time Limit: 1000MS Memory limit: 65536K
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2163
题目描写叙述
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
昊哥最终逃了他的形势与政策课程,过来打辅助了。
上场比赛累的一塌糊涂啊。这次最终释放出来了O(∩_∩)O~,最重要不用翻译英语了,好开心~。~
尽管这次并没有做出来非常多,但慢慢来,会好起来的!
这道题是第一个做出来的,非常水,就是推断输入的字符串是否合法。
简单的来说输入的字符串仅仅能有字母(大写或小写)和下划线,否则都不合法。
#include <iostream> #include <string> #include <stdio.h> #include <string.h> using namespace std; bool judge_zm(char c) { if((c>=‘a‘ && c<=‘z‘) || (c>=‘A‘ && c<=‘Z‘) || c==‘_‘) return true; return false; } int main() { bool flag; int i,n,len; char str[101]; cin>>n; cin.getline(str,101,‘\n‘); while(n--) { cin.getline(str,101,‘\n‘); len=strlen(str); flag=0; for(i=0;i<len;++i) { if(!judge_zm(str[i])) { flag=1; break; } } if(flag) cout<<"No"<<endl; else cout<<"Yes"<<endl; } return 0; }
[2011山东省第二届ACM大学生程序设计竞赛]——Identifiers