HDU 1106 排序

注意细节。

const int N=1010;
char s[N];
int n;

int main()
{
    while(~scanf("%s",s))
    {
        n=strlen(s);

        vector<int> res;
        int num=0;
        bool have=false;
        for(int i=0;i<n;i++)
        {
            if(s[i] == '5' && have)
            {
                res.pb(num);
                num=0;
                have=false;
            }
            else if(s[i] != '5')
            {
                num=num*10+s[i]-'0';
                have=true;
            }
        }

        if(s[n-1] != '5') res.pb(num);//不要忘了

        sort(res.begin(),res.end());
        for(int i=0;i<res.size();i++)
            if(i) cout<<' '<<res[i];
            else cout<<res[i];
        cout<<endl;
    }
    //system("pause");
    return 0;
}

利用stringstream进行分割(还是库函数方便,不容易出错)。

string s;
int n;

int main()
{
    while(cin>>s)
    {
        n=s.size();

        for(int i=0;i<s.size();i++)
            if(s[i] == '5')
                s[i]=' ';

        vector<int> res;
        stringstream ss;
        ss<<s;
        int x;
        while(ss>>x)
            res.pb(x);

        sort(res.begin(),res.end());
        for(int i=0;i<res.size();i++)
            if(i) cout<<' '<<res[i];
            else cout<<res[i];
        cout<<endl;
    }
    //system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/fxh0707/p/14619484.html