HDU1106题解报告

#include<iostream>//处理5在开头的情况
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<vector>
#include<sstream>
using namespace std;
typedef unsigned long long llu;
const int maxn = 1000 + 500;
llu arr[maxn];
vector<string> v;
//处理数字
llu Tonum(string& str)
{
    llu sum = 0;//数字值
    llu index = 1;//个位数
    llu len = str.length();
    for(int i=len-1;i>=0;--i)
    {
        sum += (str[i] - '0') * index;
        index *= 10;
    }
    return sum; 
}
int main()
{
    ios::sync_with_stdio(false);
    string line;
    while(cin>>line)
    {
        v.clear();
        memset(arr,0,sizeof(arr));
        stringstream os(line);
        char ch;
        string str;
        while(os>>ch)
        {
            if(ch=='5')
            {
                if(!str.empty())//不为空串时加入
                    v.push_back(str);
                str.clear();
                continue;
            }
            str += ch;
        }
        //str有可能非空
        if(!str.empty())
            v.push_back(str);
        for(int i=0;i!=v.size();++i)
        {
            arr[i] = Tonum(v[i]);
        }
        sort(arr,arr+v.size());
        cout<<arr[0];
        for(int i=1;i!=v.size();++i)
            cout<<" "<<arr[i];  
        cout<<endl;
    }
}

  写的很复杂(附一份简单代码)555俺怎么这么菜

  

#include<iostream>
#include<cstdio>
#include<sstream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
    string line;
    while(cin>>line)
    {
        int len = line.length();
        for(int i=0;i!=len;++i)
            if(line[i]=='5')
                line[i] = ' ';
        stringstream tmp(line);
        vector<int> v;
        int value;
        while(tmp>>value)
            v.push_back(value);
        sort(v.begin(),v.end());
        cout<<v[0];
        for(int i=1;i!=v.size();++i)
            cout<<" "<<v[i];
        cout<<endl;
    }
}

 

posted on 2019-08-31 15:28  chengyulala  阅读(144)  评论(0编辑  收藏  举报

导航