方法一:根据题意直接写,容易理解
#include#includeusing namespace std;int main(){char a[300]; cin.getline(a,300); int len=strlen(a); if(a[0]>='0'&&a[0]<='9') { cout<<"no"; return 0; } for(int i=0;i='0'&&a[i]<='9')||(a[i]>=65&&a[i]<=90)||(a[i]>=97&&a[i]<=122)||a[i]=='_')) { ;}else{cout<<"no";return 0;}}cout<<"yes"; return 0;}
方法二:和方法一相似
#include#includeusing namespace std;int main(){char a[300]; cin.getline(a,300); int len=strlen(a); if(a[0]>='0'&&a[0]<='9') { cout<<"no"; return 0; } for(int i=0;i='0'&&a[i]<='9')||(a[i]>=65&&a[i]<=90)||(a[i]>=97&&a[i]<=122)||a[i]=='_')) { cout<<"no"; return 0;}}cout<<"yes"; return 0;}
方法三:不将字母转化成asscii中对应的数字
#include#includeusing namespace std;int main(){char a[300]; cin.getline(a,300); int len=strlen(a); if(a[0]>='0'&&a[0]<='9') { cout<<"no"; return 0; } for(int i=0;i='0'&&a[i]<='9')||(a[i]>='A'&&a[i]<='Z')||(a[i]>='a'&&a[i]<='z')||a[i]=='_')) { ;}else{cout<<"no";return 0;}}cout<<"yes"; return 0;}