【题目】
【分析】
利用数据的绝对大小而不是相对大小进行排序,时间复杂度可能很高。普通的哈希方法可能因为数据稀疏导致效率低下,分位操作解决了这个问题!
具体实现过程中用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;
}