【HUOJ】排序(queue 优先队列)

排序

题意:输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整数就是由若干个‘0’组成的,这时这个整数就是0)。

你的任务是:对这些分割得到的整数,依从小到大的顺序排序输出。

思路:我先用string a输入,然后每一个ai判断是不是5,如果不是5就放进一个队列里,如果是5就进行一波判断

另外,我WA了一个地方是输入520这种数,我会输出0 20,然后改了一波23333~

先放上我的代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
int main()
{

    string a;
    queue<char>q;
    while(cin>>a)
    {
        int x;
        priority_queue <int,vector<int>,greater<int> >p;
        //queue<int>p;
        int l=a.length();
        for(int i=0; i<l; i++)
        {
            if(a[i]!='5')
            {
                q.push(a[i]);
            }
            if(a[i]=='5'||i==l-1)
            {
                x=0;
                int flag=0;
                if(q.empty()) continue;
                while(!q.empty())
                {
                    if(q.front()=='0'&&flag==0)
                    {
                        q.pop();
                    }
                    else
                    {
                        flag=1;
                        x=x*10+(q.front()-'0');
                        q.pop();
                    }
                }
                if(flag==0)
                    p.push(0);
                else
                    p.push(x);
            }
        }
        cout<<p.top();
        p.pop();

        while(!p.empty())
        {
            cout<<" "<<p.top();
            p.pop();
        }

        cout<<endl;
    }
    return 0;
}

做题用时:31分钟

整理博客:7分钟

猜你喜欢

转载自www.cnblogs.com/Kohinur/p/9069134.html