HDU1409-Is It a Number

100 篇文章 0 订阅

Is It a Number

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


Problem Description
In some programming languages(like C/C++), we define numbers like this:

number -> digits optional_fraction optional_exponent
optional_fraction -> [empty] | .digits
optional_exponent -> [empty] | E digits | E - digits
digits -> digit | digits digit
digit -> [0-9]

Given a string, you have to tell whether it is a legal number.
 

Input
The first line of input contains an integer T(1<=T<=100) which indicate the number of test cases. Then T test cases follow. Each test case contains one line. Each line contains a string whose length will not exceed 100.

Note: there are no spaces and tabs at the begin and the end of the string.
 

Output
For each test case, you have to tell whether the string is a legal number. If it is a legal number, output "YES" in a single line, otherwise, output "NO" in a single line.
 

Sample Input
  
  
5 0123 123.456 0.456E-5 123 456 0.456EF
 

Sample Output
  
  
YES YES YES NO NO
 

Author
Ignatius.L
 

Source
 

题意:给出一串数字,问这串数字是否符合规则

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <climits>

using namespace std;

int main()
{
    char ch[1000];
    int t;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        gets(ch);
        int len=strlen(ch);
        int flag=1,flag1=0,i;
        for(i=0;i<len; i++)
        {
            if(ch[i]=='.')
            {
                if(i!=0) {flag1=1;i++;break;}
                else flag=0;
            }
            if(ch[i]=='E')
            {
                if(i!=0) {flag1=2;i++;break;}
                else flag=0;
            }
            if(ch[i]>='0'&&ch[i]<='9') continue;
            else flag=0;
        }
        if(!flag)
        {
            printf("NO\n");continue;
        }
        if(flag1==1)
        {
            int flag2=0;
            for(; i<len; i++)
            {
                if(ch[i]=='E')
                {
                    if(flag2) {flag1=2;i++;break;}
                    else {flag=0;break;}
                }
                flag2++;
                if(ch[i]>='0'&&ch[i]<='9') continue;
                else flag=0;
            }
            if(!flag2) flag=0;
        }
        if(!flag) {printf("NO\n");continue;}
        if(flag1==2)
        {
            int flag2=0,flag3=0;
            for(; i<len; i++)
            {
                if(ch[i]=='-')
                {
                    if(flag2) flag=0;
                    flag2=1;continue;
                }
                flag3++;
                if(ch[i]>='0'&&ch[i]<='9') continue;
                else flag=0;
            }
            if(!flag3) flag=0;
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值