숫자 정렬을위한 대기열 사용 대기열

【이야기】

주제 링크

【분석】

상대 크기 대신 데이터의 절대 크기로 정렬하는 것은 시간이 매우 복잡 할 수 있습니다. 일반적인 해싱 방법은 희소 데이터로 인해 비효율적 일 수 있으며 양자화 작업은이 문제를 해결합니다!

특정 구현 프로세스에서 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;
}

 

추천

출처blog.csdn.net/w112348/article/details/109051462