hdu1106 排序(堆排序)


http://acm.hdu.edu.cn/showproblem.php?pid=1106

题意:中文题。


思路:很不错的水题。本来想水下堆,结果被格式卡了两次WA= =

需注意结尾不是5的,中间有连续5的。中间的0不要跳过,直接乘上就好。


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <iostream>

using namespace std;

typedef long long ll;
const int N = 1005;
const int INF = 0x3f3f3f3f;

int num[N];

void HeapAdjust(int mother, int len)
{
    int tmp = num[mother];
    for(int i = 2*mother; i <= len; i*=2)
    {
        if(i<len && num[i]<num[i+1]) i++;
        if(num[i]<=tmp) break;//若儿子没有母亲大,则调整完毕
        num[mother] = num[i];
        mother = i;
    }
    num[mother] = tmp;
}

void HeapSort(int len)
{
    for(int i = len/2; i > 0; i--)
    {
        HeapAdjust(i, len);
    }
    for(int i = len; i > 1; i--)
    {
        swap(num[1], num[i]);
        HeapAdjust(1, i-1);
    }
}

int main()
{
   // freopen("in.txt", "r", stdin);
    int n;
    char s[N];
    while(~scanf("%s", s))
    {
        int len = strlen(s);
        int dight = 0;
        n = 0;
        int flag = 0;//1标志数字的开始,0表示结束
        for(int i = 0; i < len; i++)
        {
            if(!flag && (s[i]=='5')) continue;
            else if(s[i] == '5')//把这个数字存起来
            {
                num[++n] = dight;
                dight = 0;
                flag = 0;
            }
            else
            {
                dight = dight*10+(s[i]-'0');
                flag = 1;
            }
        }
        if(dight!=0) num[++n] = dight;
        HeapSort(n);
        for(int i = 1; i < n; i++)
            printf("%d ", num[i]);
        printf("%d\n", num[n]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值