标签:style blog http color io os ar for 数据
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5059
题目意思:就是输入一行不多于 100 的字符串(除了‘\n‘ 和 ‘\r‘ 的任意字符),问是否是合法的整数,如果是的话问是否在[a, b] 范围内,是则输出 YES,否则输出 NO
合法的整数:(1)非负整数:只有数字,没有前导0
(2)负数:负号后面只能跟着数字,负号前没有任何字符
首先这条题感觉不是出得太好,不过都是硬着头皮学人家做啦。反正一些很变态的数据可能还是过不了,但却AC的。
模拟题一般都是比较恶心的!!!
AC 代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> using namespace std; typedef __int64 LL; inline LL judge(string s, int sign, int st) { LL num = 0; for (int i = st; i < s.size(); i++) num = num * 10 + (s[i]-‘0‘); num *= sign; return num; } int main() { int len, a, b; string s; while (getline(cin, s)) { scanf("%d%d", &a, &b); getchar(); len = s.size(); int flag = 0; if (s[0] == ‘-‘) { for (int i = 1; i < len; i++) { flag = 0; if (s[i] < ‘0‘ || s[i] > ‘9‘) break; flag = 1; } } else { for (int i = 0; i < len; i++) { flag = 0; if (s[i] < ‘0‘ || s[i] > ‘9‘) break; flag = 1; } } // 特判0的几种情况 if (s[0] == ‘0‘ && len != 1) flag = 0; if (s[0] == ‘0‘ && len == 1) flag = 1; if (s[0] == ‘-‘ && s[1] == ‘0‘) flag = 0; if (!flag) { printf("NO\n"); continue; } // 处理到这里就已经是合法整数了 LL ans = 0; if (s[0] == ‘-‘) ans = judge(s, -1, 1); else ans = judge(s, 1, 0); if (len == 0 || len >= 12 || ans < a || ans > b) printf("NO\n"); else printf("YES\n"); } return 0; }
一直搞不明白更改成这样为什么是错的!!!空闲再想~~~
WA~~~WA ~~~~wa ~~~wa
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <string> 6 using namespace std; 7 8 typedef __int64 LL; 9 10 inline int check(string s, int st) 11 { 12 int f; 13 for (int i = st; i < s.size(); i++) 14 { 15 f = 0; 16 if (s[i] < ‘0‘ || s[i] > ‘9‘) 17 break; 18 f = 1; 19 } 20 return f; 21 } 22 23 inline LL judge(string s, int sign, int st) 24 { 25 LL num = 0; 26 for (int i = st; i < s.size(); i++) 27 num = num * 10 + (s[i]-‘0‘); 28 num *= sign; 29 return num; 30 } 31 32 int main() 33 { 34 int a, b; 35 string s; 36 while (getline(cin, s)) 37 { 38 scanf("%d%d", &a, &b); 39 getchar(); 40 int flag = 0; 41 if (s[0] == ‘-‘) 42 flag = check(s, 1); 43 else 44 flag = check(s, 0); 45 if (s[0] == ‘0‘ && s.size() != 1) 46 flag = 0; 47 if (s[0] == ‘0‘ && s.size() == 1) 48 flag = 1; 49 if (s[0] == ‘-‘ && s[1] == ‘0‘) 50 flag = 0; 51 if (!flag) 52 { 53 printf("NO\n"); 54 continue; 55 } 56 LL ans = 0; 57 if (s[0] == ‘-‘) 58 ans = judge(s, -1, 1); 59 else 60 ans = judge(s, 1, 0); 61 if (s.size() == 0 || s.size() >= 12 || ans < a || ans > b) 62 printf("NO\n"); 63 else 64 printf("YES\n"); 65 } 66 return 0; 67 }
BestCoder12 1002.Help him(hdu 5059) 解题报告
标签:style blog http color io os ar for 数据
原文地址:http://www.cnblogs.com/windysai/p/4008826.html