7-1 整数格式

7-1 整数格式

分数 100

全屏浏览题目

切换布局

作者 scs

单位 北京邮电大学

给定一个字符串,请分析该字符串格式,检查它是否符合10进制整数的表示格式。

特别提醒:
1、字符串如果有前导0,如0123,则该字符串不符合整数格式;
2、+0,-0是不符合整数格式的,但+1和-1都是符合的;
3、科学记数法是不符合的。

输入格式:

第一行为一个整数t(0

输出格式:

共t行,依次对应输入的测试用例,如果符合则输出yes,否则输出no。

输入样例:

8
1
-1
200
999
-999
0123
+0
-0

输出样例:

yes
yes
yes
yes
yes
no
no
no

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

#include
#define START 0
#define ERROR -1
#define ACCEPT 3
#define q1 1
#define q2 2
int main()
{
	int state,t,i;
	char str;
	scanf("%d",&t);

	for(i=1;i<=t;i++){
		state=START;
		while((str=getchar())!='\n'){
			switch(state){
				case START:if(str=='+'||str=='-') state=q1;
				           else if(str=='0') state=q2;
						   else if(str>='1'&&str<='9') state=ACCEPT;
						   else state=ERROR;break;
				case q1:if(str>='1'&&str<='9') state=ACCEPT;
				        else state=ERROR;break;
				case ACCEPT:if(str>='0'&&str<='9') state=ACCEPT;
						    else state=ERROR;break;
				default:state=ERROR;break;			   			
			}
		}
		if(state==ACCEPT||state==q2) printf("yes\n");
		else printf("no\n");
	}

	return 0;
}

你可能感兴趣的:(c语言)