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 42 43 44 45 46 47 48 49 50 51 52 | /**************************************************************** * Description: 2022_CSP_S_2 * Author: Alex Li * Date: 2023-08-18 05:15:45 * LastEditTime: 2023-08-18 06:04:49 ****************************************************************/ #include <iostream> using namespace std; const int MAXN = 105; int n, m, k, val[MAXN]; int temp[MAXN], cnt[MAXN]; void init() { cin >> n >> k; for (int i = 0; i < n; i++) cin >> val[i]; int maximum = val[0]; for (int i = 1; i < n; i++) if (val[i] > maximum) maximum = val[i]; m = 1; while (maximum >= k) { maximum /= k; m++; } } void solve() { int base = 1; for (int i = 0; i < m; i++) { for (int j = 0; j < k; j++) cnt[j] = 0; for (int j = 0; j < n; j++) cnt[val[j] / base % k]++; for (int j = 1; j < k; j++) cnt[j] += cnt[j - 1]; for (int j = n - 1; j >= 0; j--) { temp[cnt[val[j] / base % k] - 1] = val[j]; cnt[val[j] / base % k]--; } for (int j = 0; j < n; j++) val[j] = temp[j]; base *= k; } } int main() { init(); solve(); for (int i = 0; i < n; i++) cout << val[i] <<' ' ; cout << endl; return 0; } |
假设输入的 n 为不大于 100 的正整数,k 为不小于 2 且不大于 100 的正整数,val[i]在 int 表示范围内,完成下面的判断题和单选题:
0 of 6 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 6 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
1、这是一个不稳定的排序算法。( )
2、该算法的空间复杂度仅与 n 有关。( )
3、该算法的时间复杂度为O(m(n+k))。( )
4、当输入为“5 3 98 26 91 37 46”时,程序第一次执行到第 41 行,val[]数组的内容依次为( )。
5、若 val[i]的最大值为 100,k 取( )时算法运算次数最少。
6、当输入的 k 比 val[i]的最大值还大时,该算法退化为( )算法。