标签:
思路分析:按位取数,存入数组,最后排序输出; 有许多特殊情况需要考虑,包括‘5‘出现的位置及相应的处理
注意事项:一定要清空数组
题解:
#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] = ‘#‘; 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++) a[j] = ‘#‘; } else if(a[i] >= ‘0‘ && a[i] <= ‘9‘){ ans = 10*ans + a[i] - ‘0‘; if(i == len-1) b[cnt++] = ans; } } sort(b, b+cnt); for(int i = 0; i < cnt-1; i++) printf("%d ", b[i]); printf("%d\n", b[cnt-1]); memset(a, 0, sizeof(a)); } return 0; }
标签:
原文地址:http://www.cnblogs.com/ACFLOOD/p/4242695.html