hdu1106 排序 堆排序

Description

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

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

 

Input

输入包含多组测试用例,每组输入数据只有一行数字(数字之间没有空格),这行数字的长度不大于1000。   

输入数据保证:分割得到的非负整数不会大于100000000;输入数据不可能全由‘5’组成。 
 

Output

对于每个测试用例,输出分割得到的整数排序的结果,相邻的两个整数之间用一个空格分开,每组输出占一行。 
 

Sample Input

       
       
0051231232050775
 

Sample Output

       
       
0 77 12312320
 
纯属为了练习一下堆排序
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 2000
using namespace std;
__int64 heap[N];
char str[2000];
char str1[1010];
char Map[1010][1100];
int print_in(__int64 a)
{
    if(heap[0]==N)
        return -1;
    heap[++heap[0]]=a;
    int x=heap[0];
    int p=x/2;
    while(p>0&&heap[p]>heap[x])
    {
        swap(heap[p],heap[x]);
        x=p;
        p=p/2;
    }
    return 0;
}
int print_out()
{
    if(heap[0]==0)
        return -1;
    __int64 ret=heap[1];
    swap(heap[1],heap[heap[0]]);
    heap[0]--;
    int p=1,x;
    int lc,rc;
    while(1)
    {
        lc=p<<1;
        rc=p<<1|1;
        x=p;
        if(lc<=heap[0]&&heap[lc]<heap[x])
            x=lc;
        if(rc<=heap[0]&&heap[rc]<heap[x])
            x=rc;
        if(x==p)
            break;
        swap(heap[x],heap[p]);
        p=x;
    }
    return ret;
}
int main()
{
    __int64 a;
    while(~scanf("%s",str))
    {
        heap[0]=0;
        int j=0,k=0;
        int len=strlen(str);
        for(int i=0; i<len; i++)
        {
            if(str[i]=='5')
            {
                if(j!=0)
                {
                    str1[j]='\0';
                    strcpy(Map[k++],str1);
                    j=0;
                }
            }
            else if(str[i]!='5')
            {
                str1[j++]=str[i];
            }
        }
        if(str[len-1]!='5')
        {
            str1[j]='\0';
            strcpy(Map[k++],str1);
        }
        for(int i=0; i<k; i++)
        {
            a=0;
            for(int j=0; j<strlen(Map[i]); j++)
            {
                a=a*10+Map[i][j]-'0';
            }
            print_in(a);
        }
        while(heap[0]>0)
        {
            cout<<print_out();
            putchar(heap[0]!=0?' ':'\n');
        }
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值