博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 5059
阅读量:5077 次
发布时间:2019-06-12

本文共 2509 字,大约阅读时间需要 8 分钟。

Problem Description
As you know, when you want to hack someone's program, you must submit your test data. However sometimes you will submit invalid data, so we need a data checker to check your data. Now small W has prepared a problem for BC, but he 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 integer, it contains only digits without leading zeros.
2. When it represents a negative integer, it contains exact one negative sign ('-') followed by digits without leading zeros and there are no characters before '-'.
3. Otherwise it is not a valid integer.
 
 
Input
Multi test cases (about 100), every case occupies two lines, the first line contain a string which represents the input string, 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" (without quote).
 
 
Sample Input
10
-100 100
1a0
-100 100
 
 
Sample Output
YES
NO
题意:第一行给出一个字符串 判断这个字符串是否合法 如果合法是否在区间【a,b】之内(包含a,b)   坑多。。。
#include
#include
#include
#include
#include
#include
using namespace std;const int N=1e2+10;const int INF=0x3f3f3f3f;const int MOD=1e9+7;typedef long long LL;int main (){ char s[N]; LL a, b, c; int len, flag, i, mark, legal; while (gets(s) != 0) { scanf("%lld %lld%*c", &a, &b); len = strlen(s); mark = 0;///标记负号 legal = 1;///是否合法 c = 0;///保存字符串的值 flag = 0;///0表示在范围内 1表示不在 if (s[0] == '-') mark = 1; for (i = 0; i < len; i++) { if ((s[i] == '-' && i == 0) || (s[i] >= '0' && s[i] <= '9')) continue; /**
<除了‘-’ 剩下的都必须是数字且不含有前导0的才是合法的串* legal="0;" break; } i="0;" if (mark) i++; (s[i]="=" '0') (len>
15) legal = 0;///串太长超出范围 if (legal)///只处理合法的串 { i = 0; if (mark) i++;///排除‘-’的影响 for ( ; i < len; i++) c = c*10+(s[i]-'0'); if (mark) c = -c;///有‘-’ 取相反数 if (c >= a && c <= b) flag = 1; } if (len == 1 && s[0] == '0' && 0 >= a && 0 <= b) flag = 1;///字符串只有一个‘0’如果在区间内也合法 if (s[0] == '-' && len == 1) flag = 0;///只有‘-’ 也是不合法的 if (len == 0) flag = 0;///空串是不合法的!!!!! if (flag == 0) printf("NO\n"); else printf("YES\n"); } return 0;}

 

转载于:https://www.cnblogs.com/PersistFaith/p/4932608.html

你可能感兴趣的文章
这个看起来有点简单!--------实验吧
查看>>
PHP count down
查看>>
JVM参数调优:Eclipse启动实践
查看>>
(旧笔记搬家)struts.xml中单独页面跳转的配置
查看>>
不定期周末福利:数据结构与算法学习书单
查看>>
strlen函数
查看>>
python的列表与shell的数组
查看>>
关于TFS2010使用常见问题
查看>>
软件工程团队作业3
查看>>
python标准库——queue模块 的queue类(单向队列)
查看>>
火狐、谷歌、IE关于document.body.scrollTop和document.documentElement.scrollTop 以及值为0的问题...
查看>>
深入理解JVM读书笔记--字节码执行引擎
查看>>
vue-搜索功能-实时监听搜索框的输入,N毫秒请求一次数据
查看>>
批处理 windows 服务的安装与卸载
查看>>
React文档翻译 (快速入门)
查看>>
nodejs fs路径
查看>>
动态规划算法之最大子段和
查看>>
linux c:关联变量的双for循环
查看>>
深入浅出理解zend framework(三)
查看>>
python语句----->if语句,while语句,for循环
查看>>