题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=525
解题思路:
用到了字符串截取函数strtok
本来想用sscanf的正则表达式的,但是不会写。。。百度了一下,知道了大概,貌似不能对数字用。估计能把(没仔细找~~)
代码如下:
#include <iostream>
#include <cstring>
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
const int N = 5010;
char str[N];
string ans[N];
bool cmp(string a, string b)
{
int lena = a.size(), lenb = b.size();
if(lena != lenb)
return lena < lenb;
else
{
for(int i = 0; i < lena; ++i)
if(a[i] != b[i])
return a[i] < b[i];
}
}
int main()
{
int len, i, j, k, num;
while(scanf("%s", str) != EOF)
{
num = 0;
char *ptr=strtok(str,"5");
while(ptr!=NULL)
{
string temp;
len = strlen(ptr);
for(i = 0; ptr[i] == '0'; ++i);
if(i == len)
ans[num++] = "0";
else
{
for(j = i; j < len; ++j)
temp.push_back(ptr[j]);
ans[num++] = temp;
}
ptr=strtok(NULL,"5");
}
sort(ans, ans + num, cmp);
for(int i = 0; i < num; ++i)
cout<<ans[i]<<" ";
cout<<endl;
}
return 0;
}