DS内排—2-路归并排序
时间限制: 1 Sec 内存限制: 128 MB
题目描述
输入一组字符串,用2-路归并排序按字典顺序进行降序排序。
输入
测试次数t
每组测试数据:数据个数n,后跟n个字符串,字符串不含空格。
输出
对每组测试数据,输出2-路归并排序的每一趟排序结果。每组测试数据的输出之间有1空行。
样例输入
1 2 3 | 2 6 shenzhen beijing guangzhou futian nanshan baoan 10 apple pear peach grape cherry dew fig haw lemon marc |
样例输出
1 2 3 4 5 6 7 8 | shenzhen beijing guangzhou futian nanshan baoan shenzhen guangzhou futian beijing nanshan baoan shenzhen nanshan guangzhou futian beijing baoan pear apple peach grape dew cherry haw fig marc lemon pear peach grape apple haw fig dew cherry marc lemon pear peach haw grape fig dew cherry apple marc lemon pear peach marc lemon haw grape fig dew cherry apple |
提示
解决方案
写得太差了都快不好意思发了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <iostream> #include <string> #include <vector> int main() { int ctrl; std::cin >> ctrl; while (ctrl--) { int size; std::cin >> size; std::vector<std::string> vector(static_cast<size_t>(size)); for (int i = 0; i < size; ++i) { std::cin >> vector[i]; } int base = 2; while (base <= size * 2) { for (int i1 = 0; i1 < vector.size(); i1 += base) { for (int i2 = 0; i2 < base - 1; ++i2) { for (int i3 = 0; i3 < base - i2 - 1; ++i3) { if (i1 + i3 + 1 >= vector.size()) { continue; } if (vector[i1 + i3] < vector[i1 + i3 + 1]) { std::swap(vector[i1 + i3], vector[i1 + i3 + 1]); } } } } std::cout << vector.front(); for (int i = 1; i < vector.size(); ++i) { std::cout << ' ' << vector[i]; } std::cout << std::endl; base *= 2; } std::cout << std::endl; } return 0; } |