hdu1106 排序 堆排序

来源:互联网 发布:打印机无法网络打印 编辑:程序博客网 时间:2024/06/05 21:12

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 2000using 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;}

0 0
原创粉丝点击