首页 > 技术文章 > HDU 1106 排序 (排序+处理字符串)

dwtfukgv 2016-06-02 23:56 原文

题意:略。

析:按照题目说的,把字符串分割,然后把字符串转成十进制,存起来,可以用数组,我用的向量,

排序一下就OK了。注意的是,要考虑多个5相邻的时候,刚开始没考虑WA了一次。

代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <map>
#include <cctype>

using namespace std;
const int maxn = 1000 + 5;
char s[maxn];

int main(){
    while(~scanf("%s", s)){
        vector<int> v;
        int n = strlen(s);
        int t = 0;
        bool ok = false;
        for(int i = 0; i < n; ++i)
            if('5' == s[i] && ok){  v.push_back(t); t = 0; ok = false; }
            else if('5' != s[i]) { t = t * 10 + s[i] - '0';  ok = true; }
        
        if(s[n-1] != '5')  v.push_back(t);
        sort(v.begin(), v.end());
        printf("%d", v[0]);
        for(int i = 1; i < v.size(); ++i)
            printf(" %d", v[i]);
        printf("\n");
    }
    return 0;
}

 

推荐阅读