Help him (Bestcoder Round #12) hdu 5059__ Open Your Mind

Source: Internet
Author: User
Tags sprintf
Help him
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1829 accepted submission (s): 137


Problem Description
As you know the want to hack someone's program, you must submit your test data. However Sometimes you are submit invalid data, so we need a data checker to check your data. Now small W has prepared a problem for BC, but his is too busy to write the data checker. Please help him to write a data check which judges whether the ' input is ' an integer ranged from A to B (inclusive). Note:a string represents a valid integer when it follows below rules. 1. When it represents a non-negative an integer, it contains only digits without leading. 2. When it represents a negative an integer, it contains exact one negative sign ('-') followed by digits without leading OS and there are no characters before '-'. 3. Otherwise it is not a valid integer. Input
Multi test Cases (about), every case occupies two lines, the "a" contain a string which represents the input St Ring, then second line contains a and b separated by space. Process to the end of file. Length of string is no more than 100. The string may contain any characters other than ' \ n ', ' \ R '. -1000000000≤a≤b≤1000000000 Output
For each case output ' YES ' (without quote) when the ' string is ' an integer ranged from a to B, otherwise output ' NO ' (Withou T quote). Sample Input
10-100 1a0-100 Sample Output
YES NO


The following:

Determine if the string you are converting to numbers is legal, and the number size is within [a,b] range.


Analysis:

At first I wrote a lot of things to judge all kinds of illegal situations, and there may be escape characters, so write to the end or WA ... It turned out to be a very simple idea: simply convert the string to numbers first, use the Itoa function or the sprintf function, because the conversion will stop if an illegal character appears (for example: -10a54, the middle A is illegal, So this string is going to be-10 when converted to a number. And then convert the number to a string, and finally compare the string to the string it gives, and in [a,b], output yes if yes, or No.


conclusion: Ideas are important!!!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
    int a , b;
    Char m[110], c[110];

    while (gets (m))
    {
        scanf ("%d%d", &a,&b);
        GetChar ();
        int x = atoi (m);
        sprintf (c, "%d", x);
        Itoa (x,c,10);
        if (strcmp (m,c) = = 0 && x >=a && x <= B) puts ("YES");
        Else puts ("NO");
    return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.