1 条题解

  • 0
    @ 2024-7-13 21:04:16

    小凯的排序课

    本题为签到题,考察简单 for 循环和 if-else if-else 语句。此外,可以使用映射关系的数据结构,来减少代码量,提高编码效率

    C++ 实现

    #include <bits/stdc++.h>
    
    using namespace std;
    unordered_map<string, string> dict = {
        {"quick sort"    , "O(nlogn) O(logn) No" },
        {"merge sort"    , "O(nlogn) O(n) Yes"   },
        {"heap sort"     , "O(nlogn) O(1) No"    },
        {"shell sort"    , "O(n^{1.3~2}) O(1) No"},
        {"counting sort" , "O(n+m) O(n+m) Yes"   },
        {"insertion sort", "O(n^2) O(1) Yes"     },
        {"bubble sort"   , "O(n^2) O(1) Yes"     },
        {"selection sort", "O(n^2) O(1) No"      },
    };
    void solve() {
        string s;
        getline(cin, s);
        cout << dict[s] << endl;
    }
    
    signed main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int t;
        cin >> t;
        cin.ignore();    // 注意要 ignore,否则第一行的行尾换行符会影响 getline
        while (t--)
            solve();
        return 0;
    }
    

    Python 实现

    mp = {
        "quick sort": "O(nlogn) O(logn) No",
        "merge sort": "O(nlogn) O(n) Yes",
        "heap sort": "O(nlogn) O(1) No",
        "shell sort": "O(n^{1.3~2}) O(1) No",
        "counting sort": "O(n+m) O(n+m) Yes",
        "insertion sort": "O(n^2) O(1) Yes",
        "bubble sort": "O(n^2) O(1) Yes",
        "selection sort": "O(n^2) O(1) No",
    }
    for _ in range(int(input())):
        print(mp[input()])
    

    Java 实现

    import java.util.*;
    
    public class Main {
        private static final Scanner scanner = new Scanner(System.in);
        private static final HashMap dict = new HashMap() {{
            put("quick sort"    , "O(nlogn) O(logn) No" );
            put("merge sort"    , "O(nlogn) O(n) Yes"   );
            put("heap sort"     , "O(nlogn) O(1) No"    );
            put("shell sort"    , "O(n^{1.3~2}) O(1) No");
            put("counting sort" , "O(n+m) O(n+m) Yes"   );
            put("insertion sort", "O(n^2) O(1) Yes"     );
            put("bubble sort"   , "O(n^2) O(1) Yes"     );
            put("selection sort", "O(n^2) O(1) No"      );
        }};
    
        private static void solve() {
            System.out.println(dict.get(scanner.nextLine()));
        }
    
        public static void main(String[] args) {
            int t = scanner.nextInt();
            for (scanner.skip("\n"); t != 0; t--)   // skip同理C++的ignore
                solve();
            scanner.close();
        }
    }
    

    信息

    ID
    1009
    时间
    1000ms
    内存
    256MiB
    难度
    5
    标签
    递交数
    89
    已通过
    33
    上传者