{"id":20279547,"url":"https://github.com/xukeawsl/pat-solutions-cpp","last_synced_at":"2026-06-09T10:31:09.879Z","repository":{"id":242344088,"uuid":"786873878","full_name":"xukeawsl/PAT-Solutions-Cpp","owner":"xukeawsl","description":"The PAT (Advanced Level) Practice Solutions","archived":false,"fork":false,"pushed_at":"2024-06-01T05:24:35.000Z","size":104,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-04T02:41:24.487Z","etag":null,"topics":["cpp","solutions"],"latest_commit_sha":null,"homepage":"https://pintia.cn","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xukeawsl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-15T13:18:28.000Z","updated_at":"2024-06-07T13:59:20.000Z","dependencies_parsed_at":"2024-06-10T20:03:11.569Z","dependency_job_id":null,"html_url":"https://github.com/xukeawsl/PAT-Solutions-Cpp","commit_stats":null,"previous_names":["xukeawsl/pat-solutions-cpp"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/xukeawsl/PAT-Solutions-Cpp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xukeawsl%2FPAT-Solutions-Cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xukeawsl%2FPAT-Solutions-Cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xukeawsl%2FPAT-Solutions-Cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xukeawsl%2FPAT-Solutions-Cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xukeawsl","download_url":"https://codeload.github.com/xukeawsl/PAT-Solutions-Cpp/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xukeawsl%2FPAT-Solutions-Cpp/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34103355,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-09T02:00:06.510Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","solutions"],"created_at":"2024-11-14T13:31:23.769Z","updated_at":"2026-06-09T10:31:09.843Z","avatar_url":"https://github.com/xukeawsl.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# PAT 甲级题解\n\n记录 PAT 甲级题目的 C++ 解答及其注意点, 并分享一些 C++ 刷题常用语法\n\n试题链接：\n\n1. [PAT官方](https://pintia.cn/problem-sets/994805342720868352/exam/problems/type/7)\n\n2. [AcWing](https://www.acwing.com/problem/search/1/?csrfmiddlewaretoken=BZzs1DHQHamtbsPlyW88aT2Bt7nCuCta08kfejLs7EMgLEx7UozjHDTjCOiw3u82\u0026show_algorithm_tags=0\u0026search_content=PAT)\n\n# C++ 刷题常用语法\n\n## 离散化（数组去重）\n\n```cpp\n#include \u003cvector\u003e\n#include \u003calgorithm\u003e\n\nusing namespace std;\n\nvector\u003cint\u003e arr = {1, 3, 2, 2, 4, 5};\n\nsort(arr.begin(), arr.end());\t// O(nlogn)\n\nauto last = unique(arr.begin(), arr.end());  // 将相邻元素去重, last 为去重后的尾迭代器\narr.erase(last, arr.end());\t// 删除 [last, arr.end()) 这部分重复出现的元素\n\n// 以上步骤可以简化为如下, 时间复杂度是 O(n) 的\n// arr.erase(unique(arr.begin(), arr.end()), arr.end());\n```\n\n## 数组求和\n\n```cpp\n#include \u003cvector\u003e\n#include \u003cnumeric\u003e\n\nusing namespace std;\n\nvector\u003cint\u003e arr = {1, 3, 2, 2, 4, 5};\n\nint sum = accumulate(arr.begin(), arr.end(), 0);\n```\n\n## 求数组最值\n\n```cpp\n#include \u003cvector\u003e\n#include \u003calgorithm\u003e\n\nusing namespace std;\n\nvector\u003cint\u003e arr = {1, 3, 2, 2, 4, 5};\n\nint maxval = *max_element(arr.begin(), arr.end());  // 返回最大值元素对应的迭代器\nint minval = *min_element(arr.begin(), arr.end());  // 返回最小值元素对应的迭代器\n\n// 返回最小值和最大值对应的迭代器(pair)\nauto [minit, maxit] = minmax_element(arr.begin(), arr.end());\n```\n\n## 构建前缀和数组\n\n```cpp\n#include \u003cvector\u003e\n#include \u003cnumeric\u003e\n\nusing namespace std;\n\nvector\u003cint\u003e arr = {1, 3, 2, 2, 4, 5};\n\nvector\u003cint\u003e presum(arr.size() + 1);\t// 需要分配足够的空间\npartial_sum(arr.begin(), arr.end(), presum.begin() + 1);\n```\n\n## 读取输入行（包括空格）\n\n```cpp\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\n\nusing namespace std;\n\nstring input;\ngetline(cin, input);\t// 如果之前有非字符串类型读取, 需要用 getchar() 读走回车符\n```\n\n## 根据空格分隔多个字符串\n\n```cpp\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\n#include \u003csstream\u003e\n#include \u003cvector\u003e\n\nusing namespace std;\n\nstring input = \"hello world c++\";\n    \nistringstream in(input);\n\nvector\u003cstring\u003e words;\nstring word;\nwhile (in \u003e\u003e word) {\n    words.emplace_back(word);\n}\n```\n\n## 优先级队列自定义比较规则\n\n```cpp\n#include \u003ciostream\u003e\n#include \u003cvector\u003e\n#include \u003cqueue\u003e\n\nusing namespace std;\n\npriority_queue\u003cint\u003e pq_max;    // 默认是最大堆\n\npriority_queue\u003cint, vector\u003cint\u003e, greater\u003cint\u003e\u003e pq_min; // 最小堆\n\nusing PII = pair\u003cint, int\u003e;\n\n// 自定义比较规则, first 大的在上, first 相同时, second 小的在上\n// priority_queue 的比较函数含义同 sort 相反, p1.first \u003c p2.first 表示\n// first 更大的在上, 而 sort 中表示 first 更小的在前\nauto cmp = [](const PII\u0026 p1, const PII\u0026 p2) {\n    if (p1.first == p2.first) {\n        return p1.second \u003e p2.second;\n    }\n    return p1.first \u003c p2.first;\n};\n\npriority_queue\u003cPII, vector\u003cPII\u003e, decltype(cmp)\u003e pq_self{cmp};\n\npq_self.emplace(2, 1);\npq_self.emplace(2, 3);\npq_self.emplace(1, 3);\n\nwhile (!pq_self.empty()) {\n    auto [a, b] = pq_self.top(); pq_self.pop();\n    cout \u003c\u003c a \u003c\u003c '-' \u003c\u003c b \u003c\u003c endl;\n}\n```\n\n## 数组翻转\n\n```cpp\n#include \u003cvector\u003e\n#include \u003calgorithm\u003e\n\nusing namespace std;\n\nvector\u003cint\u003e arr = {1, 3, 2, 2, 4, 5};\n\nreverse(arr.begin(), arr.end());\n```\n\n## 求下一个排列\n\n```cpp\n#include \u003cstring\u003e\n#include \u003calgorithm\u003e\n\nusing namespace std;\n\nstring s = \"123\";\n\n// 下一个更大的排列是 132\n// 如果不存在下一个更大的排列则返回 false, 且 s 转换为最小的排列\nbool exist = next_permutation(s.begin(), s.end());\n```\n\n## 初始化一个下标数组\n\n```cpp\n#include \u003cvector\u003e\n#include \u003cnumeric\u003e\n\nusing namespace std;\n\nvector\u003cint\u003e index(5);\n\n// 从 0 开始填充, {0, 1, 2, 3, 4}\niota(index.begin(), index.end(), 0);\n```\n\n## 字符串数字互转\n\n```cpp\n#include \u003cstring\u003e\n\nusing namespace std;\n\nstring s = \"666\";\n\nint digit = stoi(s);    // 字符串转数字\n//long digit = stol(s);\n//long long digit = stoll(s);\n\ns = to_string(digit + 1);   // 数字转字符串\n```\n\n## 十进制转二进制字符串\n\n```cpp\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\n#include \u003cbitset\u003e\n\nusing namespace std;\n\nbitset\u003c32\u003e b(-234);\t// 指定 32 位整数\n\ncout \u003c\u003c b.to_string() \u003c\u003c endl;\t// 对于负数, 得到的是补码的字符串表示\n\ncout \u003c\u003c b.count() \u003c\u003c endl; // 得到二进制表示下 1 的个数\n```\n\n## 最大公约数/最小公倍数\n\n```cpp\n#include \u003calgorithm\u003e\n\nusing namespace std;\n\nint a = 3, b = 6;\n\nint g = __gcd(a, b);\t// algorithm 提供了 __gcd 但是没有提供 lcm\n\n// 自己实现 lcm\nint lcm(int a, int b) {\n    return a * b / __gcd(a, b);\n}\n\n\n// c++17 标准 numeric 提供了 gcd 和 lcm 函数\n// 不过它们适用于 common_type，对于 __int128_t 这种类型不适用\n// 可以使用上面的 __gcd 来实现\n#inlucde \u003cnumeric\u003e\n\nint a = 3, b = 4;\n\nint g = gcd(a, b);\nint l = lcm(a, b);\n```\n\n## 红黑树容器使用\n\n### 迭代器\n\n```cpp\n// 注意点 1, 关于指向末尾的迭代器\n// set 和 map 的尾迭代器 end 存放了容器中的元素个数\n// 因此解引用 end 迭代器是没问题的, 对 end 执行自减操作\n// 可以得到容器尾部的元素\nauto it = prev(st.end());\n\n// 注意点 2，关于指向起始的迭代器\n// set 和 map 的起始迭代器当容器为空时和 end 相同\n// 对 begin 执行自减操作还是得到的 begin 而不是 end\n// 因此需要注意不要造成死循环了\n```\n\n### 自定义比较规则\n\n```cpp\n// 以 pair 为例, 假设规定以 first 从大到小排列, first 相同时\n// 以 second 从小到大排列\n\nusing PII = pair\u003cint, int\u003e;\n\nauto cmp = [](const PII\u0026 p1, const PII\u0026 p2) {\n    if (p1.first == p2.first) {\n        return p1.second \u003c p2.second;\n    }\n    return p2.first \u003c p1.first;\n};\n\nset\u003cPII, decltype(cmp)\u003e st(cmp);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxukeawsl%2Fpat-solutions-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxukeawsl%2Fpat-solutions-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxukeawsl%2Fpat-solutions-cpp/lists"}