首页 > ACM题库 > HDU-杭电 > HDU 1409 Is It a Number[解题报告] C++
2013
12-09

HDU 1409 Is It a Number[解题报告] C++

Is It a Number

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

5
0123
123.456
0.456E-5
123 456
0.456EF

样例输出:

YES
YES
YES
NO
NO

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1409

思路:就是把所有的不符点条件都排除掉,如果都符合,就Yes,否则,No;

#include<cstdio>
 #include<cstring>
 
 int main(){
     int _case;
     scanf("%d",&_case);
     getchar();
     while(_case--){
         char str[110];
         gets(str);
         int cnt1,cnt2,p,len=strlen(str);
         cnt1=cnt2=p=0;
         bool flag=true;
         for(int i=0;i<len;i++){
             if((str[i]<'0'||str[i]>'9')&&str[i]!='.'&&str[i]!='E'&&str[i]!='-'){
                 flag=false;break;
             }else if(str[i]=='E'){
                 cnt1++;
                 if(i==0||(i+1)==len||cnt1>1){
                     flag=false;break;
                 }
             }else if(str[i]=='-'){
                 cnt2++;
                 if(i==0||str[i-1]!='E'||(i+1)==len||cnt2>1){
                     flag=false;break;
                 }
             }else if(str[i]=='.'){
                 p++;
                 if((i+1)==len||i==0||p>1||cnt1>0){
                     flag=false;break;
                 }
             }
         }
         flag?puts("YES"):puts("NO");
     }
     return 0;
 }

 

解题报告转自:http://www.cnblogs.com/wally/archive/2013/04/30/3052057.html


  1. #include <stdio.h>
    int main(void)
    {
    int arr[] = {10,20,30,40,50,60};
    int *p=arr;
    printf("%d,%d,",*p++,*++p);
    printf("%d,%d,%d",*p,*p++,*++p);
    return 0;
    }

    为什么是 20,20,50,40,50. 我觉得的应该是 20,20,40,40,50 . 谁能解释下?

  2. /*
    * =====================================================================================
    *
    * Filename: 1366.cc
    *
    * Description:
    *
    * Version: 1.0
    * Created: 2014年01月06日 14时52分14秒
    * Revision: none
    * Compiler: gcc
    *
    * Author: Wenxian Ni (Hello World~), [email protected]
    * Organization: AMS/ICT
    *
    * =====================================================================================
    */

    #include
    #include

    using namespace std;

    int main()
    {
    stack st;
    int n,i,j;
    int test;
    int a[100001];
    int b[100001];
    while(cin>>n)
    {
    for(i=1;i>a[i];
    for(i=1;i>b[i];
    //st.clear();
    while(!st.empty())
    st.pop();
    i = 1;
    j = 1;

    while(in)
    break;
    }
    while(!st.empty()&&st.top()==b[j])
    {
    st.pop();
    j++;
    }
    }
    if(st.empty())
    cout<<"YES"<<endl;
    else
    cout<<"NO"<<endl;
    }
    return 0;
    }