HDOJ1106总是WA,想不出什么测试数据了
haoj 1106:
http://acm.hdu.edu.cn/showproblem.php?pid=1106
题目大意是将一个数字字符串,把'5'看成空格,将整个数字字符串分割成很多数字,然后按序输出。
能想到的数据都测试过了,但是还是WA,求大神帮忙看看呗:
//字符串转化为整数编程实现
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int LEN = 1005;
//标记输入字符串是否有效
enum STATUS{VALID = 0,INVALID};
int string_status = VALID;
long long StrToIntCore(const char* str,bool minus);
int StrToInt(const char *str){
string_status = INVALID;
long long num = 0;
if (str != NULL && str[0] != '\0')
{
bool minus = false;
if ('+' == str[0])
{
++str;
}
else if ('-' == str[0])
{
++str;
minus = true;
}
if (*str != '\0')
{
num = StrToIntCore(str,minus);
}
}
return (int)num;
}
int main(){
char str[LEN];
char num_strs[LEN][10];
int num[LEN];
while(gets(str)){
int loop = 0,cnt = 0,str_length = strlen(str);
for (int i = 0;i < str_length;++i)
{
if (str[i] != '5')
{
num_strs[loop][cnt] = str[i];
++cnt;
}
else{
num_strs[loop][cnt] = '\0';
++loop;
cnt = 0;
}
}
num_strs[loop++][cnt] = '\0';
cnt = 0;
for (int i = 0;i < loop;++i)
{
int temp = StrToInt(num_strs[i]);
if(!string_status){
num[cnt] = temp;
++cnt;
}
}
sort(num,num + cnt);
for(int i = 0;i < cnt;++i){
if(i != cnt - 1)
cout << num[i] << " ";
else
cout << num[i] << endl;
}
}
return 0;
}
long long StrToIntCore(const char* str,bool minus){
long long num = 0;
while(*str != '\0'){
if (*str >= '0' && *str <= '9')
{
num = num *10 + *str - '0';
//判断是否溢出
if ((!minus && num > 0x7FFFFFFF)
|| (minus && num < (signed int)0x80000000))
{
num = 0;
break;
}
++str;
}
else{
num = 0;
break;
}
}
if (*str == '\0')
{
string_status = VALID;
}
return minus ? -num : num;
}