hdu 5059 判断数字表示方式以及范围合法(int型之内)

题意:
      给你一个串和两个整数a,b,问你这个串表示的数字是否合法,并且在a,b之间,
和法的要求是无论是正数还是负数都没有前导0,并且注意 -0 是不合法的。


思路:
      写了将近两个小时了,还是wa,就是不停的模拟模拟模拟,最后都感觉自己不知道题意了,-0不合法是最后ac了才测出来的,在网上看到了一个比较好的方法,里面涉及到两个新的函数,之前没用过,先解释下函数及其功能


#include<stdlib.h>
//atoi(str) 把字符串转化成10进制int<字符串里只能有0-9>
//itoa(a ,str ,b)把a转换成b进制存在str中  a b 都是整数

//记得用C++提交


//atoi(str) 把字符串转化成10进制int
//itoa(a ,str ,b)把a转换成b进制存在str中 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main ()
{
   char str1[110] ,str2[110];
   int a ,b;
   while(gets(str1))
   {
      scanf("%d %d" ,&a ,&b);
      getchar();
      int c = atoi(str1);
      itoa(c ,str2 ,10);
      if(strcmp(str1 ,str2) || c < a || c > b)
      printf("NO
");
      else printf("YES
");
   }
   return 0;
}


原文地址:https://www.cnblogs.com/csnd/p/12062723.html