这道题我感觉真不怎么好做,但是别人说是水题,我能蒟蒻吧。
让我来考虑各种情况,我会被自己绕晕的。
什么有5的,没5的,开头是5的,结尾是5的,中间有5的,中间连续有很多5的……抓狂
感觉自己刷题,就是去读神犇的代码的,还是自己太垃圾。
写的短的博客,一般都调用了c++里面的东西,像我这种不懂C++的人,谁能知道我的痛。
看到思路明了的人的博客:
如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000 + 10;
char a[maxn];
int b[maxn];
int main()
{
while(~scanf("%s", a)){
memset(b, 0, sizeof(b));
int cnt = 0, ans = 0;
int len = strlen(a);
for(int i = 0; i < len&&a[i] == '5'; i++) a[i] = '#';//开始是5的情况555556
for(int i = 0; i < len; i++){
if(a[i] == '5'){
b[cnt++] = ans;
ans = 0;
for(int j = i+1; j < len && a[j] == '5'; j++)//中间有一堆5, 1555556
a[j] = '#';
}
else if(a[i] >= '0' && a[i] <= '9'){
ans = 10*ans + a[i] - '0';
if(i == len-1) b[cnt++] = ans;//结尾不是5的情况
}
}
sort(b, b+cnt);
for(int i = 0; i < cnt-1; i++) printf("%d ", b[i]);
printf("%d
", b[cnt-1]);
memset(a, 0, sizeof(a));
}
return 0;
}