队列-利用队列进行数字排序

【题目】

题目链接

【分析】

利用数据的绝对大小而不是相对大小进行排序,时间复杂度可能很高。普通的哈希方法可能因为数据稀疏导致效率低下,分位操作解决了这个问题!

具体实现过程中用stringstream规避了重复输出数据的麻烦 

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <sstream>
using namespace std;

int get_digit_number(int x, int i)
{
    int tmp = pow(10, i);
    x /= tmp;
    return x % 10;
}

int how_many_digits(int x)
{
    int ret = 1;
    while ((x /= 10) != 0)
        ++ret;
    return ret;
}

vector<int> q[5][10];

int main()
{
    int n;
    int max_digits = 0;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        int t; cin >> t;
        max_digits = max(max_digits, how_many_digits(t));
        q[0][t % 10].push_back(t);
    }

    ostringstream sout;
    for (int i = 1; i <= max_digits; ++i) {
        printf("Step%d.\n", i);
        for (int j = 0; j < 10; ++j) {
            printf("Queue%d:", j);
            for (int e : q[i-1][j]) {
                printf("%d ", e);
                if (i == max_digits)
                    sout << e << " ";
                else
                    q[i][get_digit_number(e, i)].push_back(e);
            }
            printf("\n");
        }
    }
    cout << sout.str() << endl;
    system("pause");
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值