HNOJ Is it a Number?

Is it a Number?
Time Limit: 1000ms, Special Time Limit:2500ms,Memory Limit:65366KB
Problem description
This is it! You've finally graduated and started working.Looking forward to some really cool tasks now. While you're skipping around in the eagerness of getting started, you're told what your first task is - Input Validation! You should check whether the typed input is an integer number.Time to get going! Given a sequence of characters, check whether they describe an integer number. Whitespace is allowed both before and after the number, but the rest of the input must consist of a single, non-negative integer number. Only digits will be accepted as the relevant part of the input (+ is not allowed, for instance).
Input
The first line of input contains a single number T, the number of test cases to follow. Then follow a single line for each test case; the input to be validated.
Output
For each test case, output a line containing the value of the number if the input is a valid integer number, or invalid input (all lowercase) if the input is not.
Sample Input
4
23 456
-36
0045
44.3
Sample Output
invalid input
invalid input
45
invalid input
Judge Tips
0 < T <= 500 Each test case will consist of at least 1 and at most 50 characters (excluding the line break). A test case can contain any character with an ASCII value between 32 and 126(inclusive). There should be no leading zeros in the output.
Problem Source

IDIOPEN 2011

 

 

题目大意:这道水题水的好郁闷啊,如果数字两旁有空格的格式是有效的~~呜呜

思路:切掉两边的空格然后当做正常的字符串来处理,特殊处理长度为一的字符

至于搞掉前导0,要注意用图像的思想啊,多画图,呗下标搞晕了~~~

 

program:

 

 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
int test;
scanf("%d",&test);
getchar();
while(test--)
{  
   
    char ch[1000];
    
    gets(ch);
    if(strlen(ch)>50)break;
    if(strlen(ch)==1)
       {
         if(!( (ch[0]-'0'>=0)  && (ch[0]-'0'<=9) ))
           {
             printf("invalid input\n");
             continue;
           }
         else
           {
             printf("%c\n",ch[0]);
             continue;
           }
                      
       }
    int flag=0;
    int k=0,g;
    for(;k<strlen(ch);k++)
        if(ch[k]!=' ')
          break;
    //cout<<k<<endl;
    if(k==strlen(ch))
        {
          printf("invalid input\n");
          continue;          
        }
    g=strlen(ch)-1;
    for(;g>=0;g--)
        if(ch[g]!=' ')
           break;
    //cout<<g<<endl;
    if(g==0)
        {
          printf("invalid input\n");
          continue;          
        }
   
    for(int i=k;i<=g;i++)
    {
       
       if(!( (ch[i]-'0'>=0)  && (ch[i]-'0'<=9) ))
         {
           printf("invalid input\n");
           flag=1;
           break;
           //goto END;   
         }    
    }
    
    if(flag==1)continue;
    int i=k;
    for(;i<=g;i++)
         if(ch[i]!='0')
             break;
    //cout<<i<<endl;
    if(i-1==g)
       {
          printf("0\n");      //如果是因为全部等于0而跳出的话,i是比len>1的,
          continue;          //如果是因为碰到数字跳出,指的就是数字的下标
       }
    else
        for(;i<=g;i++)
            printf("%c",ch[i]);
    printf("\n");
    //END :
                
}
//system("pause");
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值