├── .gitignore ├── Common ├── BinaryTree.hpp ├── CQueue.hpp └── Lists.hpp ├── LICENSE ├── Problems ├── 2004-1-1.c ├── 2004-1-2.c ├── 2004-1-3.c ├── 2004-2-3.c ├── 2005-1-1.c ├── 2005-1-2.c ├── 2005-1-3.c ├── 2005-1-4.c ├── 2005-2-1.c ├── 2005-2-2.c ├── 2005-2-3.c ├── 2005-2-4.c ├── 2006-1-1.c ├── 2006-1-2.c ├── 2006-1-3.c ├── 2006-1-4.c ├── 2006-2-1.c ├── 2006-2-2.c ├── 2007-1-1.c ├── 2007-1-2.c ├── 2007-1-3.c ├── 2007-1-4.c └── 2007-1-5.c ├── README.md └── Test ├── 2004-1-1-test.cpp ├── 2004-1-2-test.cpp ├── 2004-1-3-test.cpp ├── 2004-2-3-test.cpp ├── 2005-1-1-test.cpp ├── 2005-1-2-test.cpp ├── 2005-1-3-test.cpp ├── 2005-1-4-test.cpp ├── 2005-2-1-test.cpp ├── 2005-2-2-test.cpp ├── 2005-2-3-test.cpp ├── 2005-2-4-test.cpp ├── 2006-1-1-test.cpp ├── 2006-1-2-test.cpp ├── 2006-1-3-test.cpp ├── 2006-1-4-test.cpp └── 2006-2-1-test.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | # vscode 35 | .vs 36 | .VSCodeCounter 37 | .vscode 38 | Debug -------------------------------------------------------------------------------- /Common/BinaryTree.hpp: -------------------------------------------------------------------------------- 1 | # pragma once 2 | 3 | # include 4 | # include 5 | # include 6 | # include 7 | # include 8 | # include 9 | 10 | namespace bin_tree_algo { 11 | 12 | constexpr int null_number = 114514; 13 | 14 | template 15 | struct tree_node { 16 | value_type value; 17 | tree_node *left; 18 | tree_node *right; 19 | tree_node(const value_type &_va, tree_node *le, tree_node *ri) : 20 | value(_va), left(le), right(ri) { } 21 | }; 22 | 23 | template 24 | tree_node* build_tree(const container &_init_list) { 25 | auto left_child = [](size_t current) { 26 | return (current + 1) * 2 - 1; 27 | }; 28 | auto right_child = [](size_t current) { 29 | return (current + 1) * 2; 30 | }; 31 | std::vector*> tree_heap; 32 | std::for_each(_init_list.begin(), _init_list.end(), [&tree_heap](value_type _val) { 33 | if (_val == null_number) tree_heap.push_back(nullptr); 34 | else tree_heap.push_back(new tree_node(_val, nullptr, nullptr)); 35 | }); 36 | for (size_t i = 0; i < tree_heap.size(); i++) { 37 | if (tree_heap[i] == nullptr) continue; 38 | tree_heap[i]->left = left_child(i) < tree_heap.size() ? tree_heap[left_child(i)] : nullptr; 39 | tree_heap[i]->right = right_child(i) < tree_heap.size() ? tree_heap[right_child(i)] : nullptr; 40 | } 41 | return tree_heap.front(); 42 | } 43 | 44 | template 45 | tree_node* build_tree(const std::initializer_list &_init_list) { 46 | auto left_child = [](size_t current) { 47 | return (current + 1) * 2 - 1; 48 | }; 49 | auto right_child = [](size_t current) { 50 | return (current + 1) * 2; 51 | }; 52 | std::vector*> tree_heap; 53 | std::for_each(_init_list.begin(), _init_list.end(), [&tree_heap](value_type _val) { 54 | if (_val == null_number) tree_heap.push_back(nullptr); 55 | else tree_heap.push_back(new tree_node(_val, nullptr, nullptr)); 56 | }); 57 | for (size_t i = 0; i < tree_heap.size(); i++) { 58 | if (tree_heap[i] == nullptr) continue; 59 | tree_heap[i]->left = left_child(i) < tree_heap.size() ? tree_heap[left_child(i)] : nullptr; 60 | tree_heap[i]->right = right_child(i) < tree_heap.size() ? tree_heap[right_child(i)] : nullptr; 61 | } 62 | return tree_heap.front(); 63 | } 64 | 65 | template 66 | void print_tree(node *root) { 67 | std::queue BFS; 68 | BFS.push(root); 69 | while (1) { 70 | std::stringstream SS; 71 | size_t current_size = BFS.size(); 72 | size_t elemental_number = 0; 73 | for (size_t i = 0; i < current_size; i++) { 74 | if (BFS.front()) { 75 | SS << BFS.front()->value << ' '; 76 | BFS.push(BFS.front()->left); 77 | BFS.push(BFS.front()->right); 78 | elemental_number++; 79 | } else { 80 | SS << '*' << ' '; 81 | BFS.push(nullptr); 82 | BFS.push(nullptr); 83 | } 84 | BFS.pop(); 85 | } 86 | SS << '\n'; 87 | if (elemental_number == 0) break; 88 | std::cout << SS.str(); 89 | } 90 | } 91 | 92 | } 93 | 94 | using int_tree_node = bin_tree_algo::tree_node; -------------------------------------------------------------------------------- /Common/CQueue.hpp: -------------------------------------------------------------------------------- 1 | # pragma once 2 | 3 | # include 4 | # include "BinaryTree.hpp" 5 | 6 | namespace CQueue { 7 | 8 | template 9 | void init_queue(any) { } 10 | 11 | template 12 | value_type queue_pop(std::queue &queue) { 13 | value_type temp = queue.front(); 14 | queue.pop(); 15 | return temp; 16 | } 17 | 18 | template 19 | void queue_push(std::queue &queue, const value_type &value) { 20 | queue.push(value); 21 | } 22 | 23 | template 24 | bool queue_empty(std::queue &queue) { return queue.empty(); } 25 | 26 | template 27 | unsigned int queue_size(std::queue &queue) { return queue.size(); } 28 | 29 | } 30 | 31 | using namespace CQueue; 32 | using CQueue_int = std::queue; 33 | using CQueue_node_int = std::queue; -------------------------------------------------------------------------------- /Common/Lists.hpp: -------------------------------------------------------------------------------- 1 | # pragma once 2 | 3 | # include 4 | # include 5 | # include 6 | 7 | namespace list_algo { 8 | 9 | template 10 | struct node { 11 | value_type value; 12 | struct node* next; 13 | node() = default; 14 | node(const value_type &val, struct node* nxt) : value(val), next(nxt) { } 15 | }; 16 | 17 | template 18 | node* list_get(const std::initializer_list &_init_list) { 19 | node *head, *current; 20 | head = new node(*_init_list.begin(), nullptr); 21 | current = head; 22 | for (typename std::initializer_list::iterator i = _init_list.begin() + 1; i != _init_list.end(); ++i) { 23 | node *tempPtN = new node(*i, nullptr); 24 | current->next = tempPtN; 25 | current = tempPtN; 26 | } 27 | return head; 28 | } 29 | 30 | template 31 | std::pair*, unsigned int> list_ring_get(const std::initializer_list &_init_list) { 32 | node *head, *current; 33 | head = new node(*_init_list.begin(), head); 34 | current = head; 35 | for (typename std::initializer_list::iterator i = _init_list.begin() + 1; i != _init_list.end(); ++i) { 36 | node *tempPtN = new node(*i, head); 37 | current->next = tempPtN; 38 | current = tempPtN; 39 | } 40 | return { head, _init_list.size() }; 41 | } 42 | 43 | template 44 | node* list_get(const container &_init_list) { 45 | node *head, *current; 46 | head = new node(*_init_list.begin(), nullptr); 47 | current = head; 48 | for (typename container::iterator i = _init_list.begin() + 1; i != _init_list.end(); ++i) { 49 | node *tempPtN = new node(*i, nullptr); 50 | current->next = tempPtN; 51 | current = tempPtN; 52 | } 53 | return head; 54 | } 55 | 56 | template 57 | std::pair*, unsigned int> list_ring_get(const container &_init_list) { 58 | node *head, *current; 59 | head = new node(*_init_list.begin(), head); 60 | current = head; 61 | for (typename container::iterator i = _init_list.begin() + 1; i != _init_list.end(); ++i) { 62 | node *tempPtN = new node(*i, head); 63 | current->next = tempPtN; 64 | current = tempPtN; 65 | } 66 | return { head, _init_list.size() }; 67 | } 68 | 69 | template 70 | void print_list(list_node *_node) { 71 | if (!_node) return; 72 | list_node *begin = _node; 73 | std::cout << begin->value; 74 | for (_node = _node->next; _node && _node != begin; _node = _node->next) 75 | std::cout << ' ' << _node->value; 76 | } 77 | 78 | } //~list_algo 79 | 80 | using node_int = list_algo::node; 81 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 AmachiInori 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Problems/2004-1-1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 使用递归方法编写函数,实现将输入的字符串反向保存 (6分) 6 | * 7 | * 分析 8 | * 本题如果写一般循环的话非常容易。 9 | * 主要考察通过C的基础操作以及尾递归和循环的转化。 10 | * 11 | * 要点 12 | * - C类型字符串的长度如何获取? 13 | * - 循环转尾递归 14 | * - 输入输出字符串(gets puts) (在代码中未体现) 15 | */ 16 | 17 | # include 18 | 19 | void __reverse(char* str, unsigned int left, unsigned int right) { 20 | if (left >= right) return; 21 | char temp_char; 22 | temp_char = str[left]; // 交换字符 23 | str[left++] = str[right]; 24 | str[right--] = temp_char; 25 | __reverse(str, left, right); // 递归传递调用 26 | } 27 | 28 | void reverse(char* str) { // 驱动函数 29 | unsigned int length = 0; 30 | for (length = 0; str[length]; length++); // 获取字符串长度 31 | __reverse(str, 0, length - 1); 32 | } -------------------------------------------------------------------------------- /Problems/2004-1-2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 编写程序,从键盘上输入一个字符串,然后将输入的字符串按照字符的ASCII值排序,删除字符串中的空格, 6 | * 且相同的字符在串中仅出现一次,将排序后的字符串在屏幕上显示 7 | * 8 | * 分析 9 | * 主要考察计数排序的退化情况和ASCII码 10 | * 11 | * 要点 12 | * - 计数排序的退化情况 13 | * - 字符串输入和输出 14 | * - 数组、数组的初始化 15 | * - 基础ASCII码有128个,扩展打印字符集有256个 16 | * 17 | * 附加知识:几个重要的ASCII 18 | * ' ' 32 19 | * '0' 48 20 | * 'A' 65 21 | * 'a' 97 22 | */ 23 | 24 | # include 25 | 26 | int main() { 27 | int index[256] = { 0 }; 28 | char str[1024]; // 虽然这种写法很不漂亮,但考试所迫,必须如此(否则代码的篇幅会变得很长) 29 | gets(str); 30 | for (unsigned int i = 0; str[i]; i++) index[str[i]]++; 31 | for (unsigned int i = 0; i < 256; i++) { 32 | if (i != 32 && index[i]) putchar(i); // 空格的ASCII是32 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Problems/2004-1-3.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 已知有一个含有n个节点的环形链表,定义如下: 6 | struct node { 7 | int value; 8 | struct node* next; 9 | }; 10 | * 编写函数,查找链表中的value成员数值最小节点,并输出其value值 11 | * 12 | * 分析 13 | * 请务必注意本题目的链表是环形表,故不可以用指针域为null来作为遍历的截止条件 14 | * 15 | * 要点 16 | * - 环形链表遍历 17 | * - 截止条件务必认真考虑 18 | * 19 | * 为测试方便,此处链表节点结构体的名称有所改变(从node变为node_int) 20 | * 且均包含在了测试文件库中。 21 | * 22 | */ 23 | 24 | # include "stdio.h" 25 | # include "../Common/Lists.hpp" 26 | 27 | /* 如果想要拿来单独运行,请解除下述代码段的注释并去除include项然后自定义一个main */ 28 | /* 29 | struct node_int { 30 | int value; 31 | struct node_int* next; 32 | }; 33 | */ 34 | 35 | void findMin(node_int* head, unsigned int length) { 36 | node_int *min_node = head; 37 | for (unsigned int i = 0; i < length; i++, head = head->next) { 38 | if (head->value < min_node->value) min_node = head; 39 | } 40 | printf("%d", min_node->value); 41 | } -------------------------------------------------------------------------------- /Problems/2004-2-3.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 已知用二叉链表存储二叉树,试编写判断两颗二叉树是否相等的算法。试写明算法的基本思路。 6 | * 7 | * 分析 8 | * 二叉树相等即两棵树的节点的值完全相等。因此我们可以用BFS或者DFS。 9 | * 考虑到C语言里没有现成的队列ADT,但有调用栈帧,故选用DFS 10 | * 11 | * 要点 12 | * - 二叉树的深度优先遍历 13 | * - C语言无bool类型 14 | * 15 | * 原则上需要在答卷的程序中给出二叉树节点的声明,为测试方便,此处树节点结构体包含在了测试文件库中。 16 | * 我们假定它的声明是这样的: 17 | * struct int_tree_node { 18 | value_type value; 19 | int_tree_node *left; 20 | int_tree_node *right; 21 | }; 22 | * 23 | * 扩展 24 | * 只需要改动程序中的一行就可以实现“判断两个二叉树是否完全轴对称”,如何修改? 25 | */ 26 | 27 | # include "../Common/BinaryTree.hpp" 28 | 29 | /* 如果想要拿来单独运行,请解除下述代码段的注释并去除include项然后自定义一个main */ 30 | /* 31 | struct int_tree_node { 32 | value_type value; 33 | int_tree_node *left; 34 | int_tree_node *right; 35 | }; 36 | */ 37 | 38 | int compare_trees(int_tree_node *first, int_tree_node* second) { 39 | if (!(first && second)) { 40 | if (first || second) return 0; 41 | else return 1; 42 | } 43 | if (first->value != second->value) return 0; 44 | return compare_trees(first->left, second->left) && compare_trees(first->right, second->right); 45 | } -------------------------------------------------------------------------------- /Problems/2005-1-1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 有五个各不相同的正整数,它们的和是135,且按照从小到大的顺序,后面一个数是前面一个数的整倍数。 6 | * 编写程序求这几个数。 7 | * 8 | * 分析 9 | * 翻译题。将题意用编程语言重新写一遍即可。鉴于数字很小,选用五重循环。 10 | */ 11 | #include 12 | 13 | int main() { 14 | for (unsigned int i = 1; i <= 135; i++) 15 | for (unsigned int j = 2; i * j <= 135; j++) 16 | for (unsigned int k = 2; i * j * k <= 135; k++) 17 | for (unsigned int l = 2; i * j * k * l <= 135; l++) 18 | for (unsigned int m = 2; i * j * k * l * m <= 135; m++) 19 | if (i + i * j + i * j * k + i * j * k * l + i * j * k * l * m == 135) 20 | printf("%ud, %ud, %ud, %ud, %ud\n", i, i * j, i * j * k, i * j * k * l, i * j * k * l * m); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Problems/2005-1-2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 编写函数,实现将存放在变量n中的整数,逆序存放在变量m中。 6 | * 例如原来变量n保存的是483,程序运行后变量m中存放整数384 7 | * 8 | * 分析 9 | * 用简单的逐位提取即可,难度较低。 10 | * 可以使用数组存放各位,但没必要,这么做既低效,又会暴露出编程者较低的算法水平。 11 | * 最简洁明快而优雅的写法呈现在下方: 12 | * 13 | * 要点 14 | * - 循环 15 | */ 16 | 17 | int reverse_int(int n) { 18 | int res = 0; 19 | while (n) { 20 | res *= 10; 21 | res += n % 10; // 如果想要强化效率,此处仍可做修改,但没必要 22 | n /= 10; 23 | } 24 | return res; 25 | } -------------------------------------------------------------------------------- /Problems/2005-1-3.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 编写程序,实现从键盘上输入两个字符串,将它们合并之后按ASCII码值从大到小排序在屏幕输出,相同的字符仅输出一次。 6 | * 7 | * 分析 8 | * 算法与2004-1-2完全一致,使用计数排序的退化情况。 9 | * 10 | * 要点 11 | * - 计数排序的退化情况 12 | */ 13 | # include 14 | 15 | int main() { 16 | int index[256] = { 0 }; 17 | char str1[1024], str2[1024]; // 虽然这种写法很不漂亮,但考试所迫,必须如此(否则代码的篇幅会变得很长) 18 | gets(str1); 19 | gets(str2); 20 | for (unsigned int i = 0; str1[i]; i++) index[str1[i]]++; 21 | for (unsigned int i = 0; str2[i]; i++) index[str2[i]]++; 22 | for (unsigned int i = 0; i < 256; i++) { 23 | if (index[i]) putchar(i); 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Problems/2005-1-4.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 有一个保存学生课程成绩的结构数组,保存学生的考号和课程成绩。 6 | * 请编写一个函数,将保存在结构数组中的数据按照课程成绩从高到低的顺序存放在一个单向链表中。 7 | * 结构体定义如下 8 | * struct student { 9 | * int no; 10 | * float score; 11 | * }; 12 | * 13 | * 分析 14 | * 是要求较高的一道题。 15 | * 朴素地想,可以使用链表的插入排序来解决这一问题。但必须想到插入排序的效率偏低,而链表不支持快速排序等折半排序法。 16 | * 故考虑先对数组进行从小到大的排序,再对链表执行头插法构建得到从大到小的链表。 17 | * 本实现要求较高,如无能力则可以使用链表插入排序。 18 | * 19 | * 要点 20 | * - 链表的头插法构建 21 | * - malloc函数 22 | * - 快速排序qsort的库包含及调用 23 | * - 函数指针、void指针及指针类型转换 24 | * 25 | * 背景知识 26 | * qsort方法中比较函数的返回值 27 | * int compare(const void* a, const void* b)中: 28 | * 若返回值为负整数,则a会被排在b之前 29 | * 若返回值为正整数,则a会被排在b之后 30 | * 若返回值是0,则a和b不区分前后 31 | */ 32 | # include // 为了使用qsort和malloc 33 | 34 | struct student { 35 | int no; 36 | float score; 37 | }; 38 | 39 | struct node_stu { 40 | student value; 41 | node_stu* next; 42 | }; 43 | 44 | int compare_stu(const void* a, const void* b) { // 考虑到本题目score是float,故不可轻易返回差值。 45 | return ((student*)a)->score < ((student*)b)->score ? 1 : -1; 46 | } 47 | 48 | node_stu* sort_student(student* array, unsigned int length) { 49 | if (!length) return 0; 50 | qsort(array, length, sizeof(student), compare_stu); 51 | node_stu* res = 0; 52 | for (unsigned int i = 0; i < length; ++i) { 53 | node_stu* temp = (node_stu*)malloc(sizeof(node_stu)); 54 | temp->next = res; 55 | temp->value.no = array[i].no; 56 | temp->value.score = array[i].score; 57 | res = temp; 58 | } 59 | return res; 60 | } -------------------------------------------------------------------------------- /Problems/2005-2-1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 设有一个表头指针为h1的单链表。试设计一个算法,通过遍历一趟链表,将链表中所有节点的链接方向逆转。 6 | * 要求逆转之后的结果链表的表头指针h1指向原链表的最后一个节点。 7 | * 8 | * 分析 9 | * 标准的反转链表题。 10 | * 反转链表实际上就是建立链表,不要对"反转"产生过多的恐惧。 11 | * 建议做https://leetcode-cn.com/problems/reverse-linked-list/来练习。 12 | * 非常典型的程序。 13 | * 如果没有把握临场编写这一程序,建议背诵之。 14 | * 15 | * 要点 16 | * - 反转链表 17 | */ 18 | # include "../Common/Lists.hpp" 19 | 20 | /* 如果想要拿来单独运行,请解除下述代码段的注释并去除include项然后自定义一个main */ 21 | /* 22 | struct node_int { 23 | int value; 24 | struct node_int* next; 25 | }; 26 | */ 27 | 28 | node_int* reverse_list(node_int* h1) { 29 | node_int *current = 0, *__next = h1; 30 | while (__next) { 31 | node_int* temp_next = __next->next; 32 | __next->next = current; 33 | current = __next; 34 | __next = temp_next; 35 | } 36 | return current; 37 | } -------------------------------------------------------------------------------- /Problems/2005-2-2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 请给吃建立二叉排序树的算法 6 | * 7 | * 分析 8 | * 二叉查找树基本算法。 9 | * 程序较长,需要清晰的逻辑。建议打草稿。 10 | * 11 | * 要点 12 | * - 建立二叉查找树 13 | */ 14 | # include "../Common/BinaryTree.hpp" 15 | 16 | /* 如果想要拿来单独运行,请解除下述代码段的注释并去除上边的include项然后自定义一个main */ 17 | /* 18 | struct int_tree_node { 19 | value_type value; 20 | int_tree_node *left; 21 | int_tree_node *right; 22 | }; 23 | */ 24 | 25 | # include 26 | 27 | int_tree_node* BST_build(int* array, unsigned int length) { 28 | if (!length) return 0; 29 | int_tree_node* root = (int_tree_node*)malloc(sizeof(int_tree_node)); 30 | root->value = array[0]; 31 | root->left = 0, root->right = 0; 32 | for (unsigned i = 1; i < length; i++) { 33 | int_tree_node* current = root; 34 | while (current) { 35 | if (current->value < array[i]) { 36 | if (current->right) { 37 | current = current->right; 38 | } else { 39 | current->right = (int_tree_node*)malloc(sizeof(int_tree_node)); 40 | current->right->value = array[i]; 41 | current->right->right = 0, current->right->left = 0; 42 | break; 43 | } 44 | } else { 45 | if (current->left) { 46 | current = current->left; 47 | } else { 48 | current->left = (int_tree_node*)malloc(sizeof(int_tree_node)); 49 | current->left->value = array[i]; 50 | current->left->right = 0, current->left->left = 0; 51 | break; 52 | } 53 | } 54 | } 55 | } 56 | return root; 57 | } -------------------------------------------------------------------------------- /Problems/2005-2-3.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 请给出统计二叉树叶节点个数的递归算法 6 | * 7 | * 分析 8 | * 比2题简单,白给DFS递归题。与判断二叉树相同的算法基本一致。 9 | * 10 | * 要点 11 | * - 叶子节点 12 | */ 13 | 14 | # include "../Common/BinaryTree.hpp" 15 | 16 | /* 如果想要拿来单独运行,请解除下述代码段的注释并去除include项然后自定义一个main */ 17 | /* 18 | struct int_tree_node { 19 | value_type value; 20 | int_tree_node *left; 21 | int_tree_node *right; 22 | }; 23 | */ 24 | 25 | int number_of_node(int_tree_node *root) { 26 | if (!root) return 0; 27 | if (!(root->left || root->right)) return 1; 28 | return number_of_node(root->left) + number_of_node(root->right); 29 | } -------------------------------------------------------------------------------- /Problems/2005-2-4.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 已知一个无符号整数n,写一算法,将其转化为8进制数。要求用链栈来实现。 6 | * 7 | * 分析 8 | * 没啥意思的题,为了用栈而用栈。 9 | * 考研可以将链栈当作ADT来用,但是如果纸面有富裕,建议简单地实现一下。 10 | * 11 | * 要点 12 | * - 链栈ADT 13 | */ 14 | 15 | # include "../Common/Lists.hpp" 16 | # include 17 | # include 18 | 19 | typedef node_int* stack; 20 | 21 | stack initstack() { 22 | stack res = (stack)malloc(sizeof(node_int)); 23 | res->next = 0; 24 | res->value = 0; 25 | return res; 26 | } 27 | 28 | int empty(stack sta) { 29 | return sta->next == 0; 30 | } 31 | 32 | void push(stack sta, int value) { 33 | node_int* temp = (node_int*)malloc(sizeof(node_int)); 34 | temp->next = sta->next; 35 | temp->value = value; 36 | sta->next = temp; 37 | } 38 | 39 | int pop(stack sta) { 40 | node_int* temp = sta->next; 41 | int res = temp->value; 42 | sta->next = sta->next->next; 43 | free(temp); 44 | return res; 45 | } 46 | 47 | // 以上内容考试时如无要求可以不写 48 | 49 | void trans(int n) { 50 | stack numStack = initstack(); 51 | while (n) { 52 | push(numStack, n % 8); 53 | n /= 8; 54 | } 55 | while (!empty(numStack)) { 56 | int temp = pop(numStack); 57 | printf("%d", temp); 58 | } 59 | } -------------------------------------------------------------------------------- /Problems/2006-1-1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 编写函数,采用递归方法实现将输入的字符串反向输出 6 | * 7 | * 分析 8 | * 基础递归,为了考递归而考递归,没什么意思。 9 | */ 10 | 11 | # include 12 | # include 13 | 14 | void __reverse_output(char* _str, unsigned int loca, unsigned int length) { 15 | if (loca >= length) return; 16 | __reverse_output(_str, loca + 1, length); 17 | printf("%c", _str[loca]); 18 | } 19 | 20 | void reverse_output() { 21 | char _str[1024]; 22 | gets(_str); 23 | __reverse_output(_str, 0, strlen(_str)); 24 | } -------------------------------------------------------------------------------- /Problems/2006-1-2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 一个自然数被8除余1,所得之商被8除也余1,再将第二次的商被8除后余7,最后得到一个商为a。 6 | * 又知道被17除余4,所得的商被17除余15,最后得到一个商是a的二倍。 7 | * 求这个自然数 8 | * 9 | * 分析 10 | * 标准翻译题,认真点就不会错 11 | * 这个数应该是1993 12 | */ 13 | 14 | unsigned int get_number() { 15 | for (unsigned int a = 0; ; a++) { 16 | unsigned int current = ((a * 8 + 7) * 8 + 1) * 8 + 1; 17 | if (current % 17 == 4 && current / 17 % 17 == 15 && current / 17 / 17 == 2 * a) 18 | return current; 19 | } 20 | } -------------------------------------------------------------------------------- /Problems/2006-1-3.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 将一个数码倒过来所得到的新数叫原数的反序数。若一个数等于它的反序数,则称它为对称树。 6 | * 求不超过2006的最大对称数。 7 | * 8 | * 分析 9 | * 基本算法与2005-1-2一致,白给,别写错。 10 | */ 11 | 12 | # include "stdio.h" 13 | 14 | int reverse_int(int n) { 15 | int res = 0; 16 | while (n) { 17 | res *= 10; 18 | res += n % 10; 19 | n /= 10; 20 | } 21 | return res; 22 | } 23 | 24 | int main() { 25 | for (int i = 0; i <= 2006; i++) { 26 | if (i == reverse_int(i)) printf("%d\n", i); 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Problems/2006-1-4.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 有一个保存学生课程成绩的结构数组,保存学生的学号、课程编号和课程成绩。 6 | * 请编写一个函数,将保存在结构数组中的数据先按照课程编号从小到大,再按照课程成绩从高到低的顺序存放在一个单向链表中。 7 | * 结构体定义如下 8 | * struct student { 9 | * int sno; // 学生编号 10 | * int cno; // 课程编号 11 | * float score; 12 | * }; 13 | * 14 | * 分析 15 | * 与2005-1-4大致相似,只是需要重新编写一下 16 | * 17 | * 要点 18 | * - 链表的头插法构建 19 | * - malloc函数 20 | * - 快速排序qsort的库包含及调用 21 | * - 函数指针、void指针及指针类型转换 22 | * 23 | * 背景知识 24 | * qsort方法中比较函数的返回值 25 | * int compare(const void* a, const void* b)中: 26 | * 若返回值为负整数,则a会被排在b之前 27 | * 若返回值为正整数,则a会被排在b之后 28 | * 若返回值是0,则a和b不区分前后 29 | */ 30 | 31 | # include // 为了使用qsort和malloc 32 | 33 | struct student { 34 | int sno; // 学生编号 35 | int cno; // 课程编号 36 | float score; 37 | }; 38 | 39 | struct node_stu { 40 | student value; 41 | node_stu* next; 42 | }; 43 | 44 | int compare_stu(const void* a, const void* b) { // 考虑到本题目score是float,故不可轻易返回差值。 // 反向排序 45 | if (((student*)a)->cno == ((student*)b)->cno) // 注意观察括号 46 | return ((student*)a)->score > ((student*)b)->score ? 1 : -1; 47 | else return -((student*)a)->cno + ((student*)b)->cno; 48 | } 49 | 50 | node_stu* sort_student(student* array, unsigned int length) { 51 | if (!length) return 0; 52 | qsort(array, length, sizeof(student), compare_stu); 53 | node_stu* res = 0; 54 | for (unsigned int i = 0; i < length; ++i) { 55 | node_stu* temp = (node_stu*)malloc(sizeof(node_stu)); 56 | temp->next = res; 57 | temp->value.sno = array[i].sno; 58 | temp->value.cno = array[i].cno; 59 | temp->value.score = array[i].score; 60 | res = temp; 61 | } 62 | return res; 63 | } -------------------------------------------------------------------------------- /Problems/2006-2-1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 请给出层序遍历二叉树的算法 6 | * 7 | * 分析 8 | * 基本BFS,简单得很。 9 | * 这都不会你考个锤子计算机。 10 | */ 11 | 12 | # include "../Common/BinaryTree.hpp" 13 | # include "../Common/CQueue.hpp" 14 | 15 | /* 如果想要拿来单独运行,请解除下述代码段的注释并去除include项然后自定义一个main */ 16 | /* 17 | struct int_tree_node { 18 | value_type value; 19 | int_tree_node *left; 20 | int_tree_node *right; 21 | }; 22 | */ 23 | 24 | void BFS_handle(int_tree_node* node) { 25 | printf("%d ", node->value); 26 | } 27 | 28 | void BFS(int_tree_node* root) { 29 | CQueue_node_int my_queue; 30 | init_queue(my_queue); 31 | queue_push(my_queue, root); 32 | int BFS_size; 33 | while (BFS_size = queue_size(my_queue)) { 34 | for (int i = 0; i < BFS_size; i++) { 35 | int_tree_node* current = queue_pop(my_queue); 36 | BFS_handle(current); 37 | if (current->left) queue_push(my_queue, current->left); 38 | if (current->right) queue_push(my_queue, current->right); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /Problems/2006-2-2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 请给出快速排序的算法 6 | * 7 | * 分析 8 | * 基本算法功底。写严版以第一个为pivot的就行了。 9 | */ 10 | 11 | void swap(int* a, int* b) { 12 | int temp = *a; 13 | *a = *b; 14 | *b = temp; 15 | } 16 | 17 | void quick_sort(int* array, unsigned int length) { 18 | __quick_sort(array, 0, length); 19 | } 20 | 21 | void __quick_sort(int* array, unsigned int begin, unsigned int end) { //左开右闭 22 | if (begin + 1 >= end) return; 23 | unsigned int left = begin + 1, right = end - 1; 24 | while (1) { 25 | while (left < right && array[left] <= array[begin]) left++; 26 | while (left < right && array[right] > array[begin]) right--; 27 | if (left < right) { 28 | swap(array + left, array + right); 29 | } else break; 30 | } 31 | swap(array + 0, array + right); 32 | __quick_sort(array, begin, right); 33 | __quick_sort(array, right, end); 34 | } 35 | -------------------------------------------------------------------------------- /Problems/2007-1-1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 从键盘上输入整数n,将n分解为若干质数之积,并输出之。 6 | * 7 | * 分析 8 | * 算是一道比较有水平的题目。个人建议使用递归来做会比较快乐。 9 | * 10 | * 代码解释 11 | * 边找因数,边检验输入数是否是素数。若是素数,直接输出,若不是素数,一分为二,分别进行。 12 | * 对素数输入的效率可能在O(n)。 13 | */ 14 | 15 | # include 16 | 17 | void divid(unsigned int target) { 18 | for (unsigned int i = 2; i <= target; i++) { 19 | if (i == target) { 20 | printf("%d", target); 21 | return; 22 | } else if (target % i == 0) { 23 | divid(i); 24 | return divid(target / i); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Problems/2007-1-2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 一个自然数的七进制表示是一个三位数,而这个自然数的九进制表示也是一个三位数, 6 | * 且这两个三位数的数码顺序刚好相反,求这个三位数 7 | * 8 | * 分析 9 | * 基本进制转换题。然后还有传统艺能"反着写",基本白送。 10 | */ 11 | 12 | int find_number() { 13 | for (int i = 7 * 7 * 7; i < 7 * 7 * 7 * 7; i++) { 14 | int temp_num = i; 15 | int num_in_7 = 0, num_in_9 = 0; 16 | while (temp_num) { 17 | num_in_7 *= 10; 18 | num_in_7 += temp_num % 7; 19 | temp_num /= 7; 20 | } 21 | temp_num = i; 22 | while (temp_num) { 23 | num_in_9 *= 10; 24 | num_in_9 += temp_num % 9; 25 | temp_num /= 9; 26 | } 27 | int reversed_num_9 = 0; 28 | while (num_in_9) { 29 | reversed_num_9 *= 10; 30 | reversed_num_9 += num_in_9 & 10; 31 | num_in_9 /= 10; 32 | } 33 | if (reversed_num_9 == num_in_7) return i; 34 | } 35 | return -1; 36 | } -------------------------------------------------------------------------------- /Problems/2007-1-3.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 请编写一个求字符串长度的递归函数,函数原型为:int strlong(char *string) 6 | * 7 | * 分析 8 | * 传统艺能:循环转尾递归 9 | */ 10 | 11 | int strlong(char* string) { 12 | if (string[0] == '\0') return 0; 13 | else return strlong(string + 1) + 1; 14 | } -------------------------------------------------------------------------------- /Problems/2007-1-4.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 按以下规则生成数列: 6 | * 1. 前2项为2和3 7 | * 2. 若(n - 1)和(n - 2)项的乘积为一位数,则n项就是这个一位数 8 | * 3. 若上述乘积是两位数,则第n项和第(n + 1)分别是这个数的十位和个位数 9 | * 请求出这个数列的第n项。 10 | * 11 | * 分析 12 | * 翻译题。模拟算法即可 13 | */ 14 | 15 | int get_in_list(unsigned int loca) { 16 | int list[loca + 1]; // 多开一个是为了防止最后一次计算大于10 17 | list[0] = 2, list[1] = 3; 18 | for (int i = 2; i < loca; i++) { 19 | int temp = list[i - 1] * list[i - 2]; 20 | if (temp >= 10) { 21 | list[i++] = temp / 10; 22 | list[i] = temp % 10; 23 | } else { 24 | list[i] = temp; 25 | } 26 | } 27 | return list[loca - 1]; 28 | } -------------------------------------------------------------------------------- /Problems/2007-1-5.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 输入一个长度不超过100的字符串,删除串中的重复字符。如 6 | * 原串:abacaeedabcdcd 7 | * 输出:abced 8 | * 9 | * 分析 10 | * 可以使用一个哈希表来记录。 11 | * 如果使用指针传出数据,注意内存要开在堆上 12 | */ 13 | # include 14 | # include 15 | 16 | void unique(char* string) { 17 | unsigned int length = strlen(string); 18 | char result[length] = { '\0' }; 19 | int writing_loca = 0; 20 | char hash[256] = { 0 }; 21 | for (unsigned int i = 0; i < length; i++) { 22 | if (hash[string[i]]) continue; 23 | else { 24 | result[writing_loca++] = string[i]; 25 | hash[string[i]] = 1; 26 | } 27 | } 28 | memcpy(string, result, length * sizeof(char)); 29 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hc495/BIT_885_Problems/c9a36d9d8dbf5d0732844876d01b569378c7ebf8/README.md -------------------------------------------------------------------------------- /Test/2004-1-1-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 测试程序 5 | * 6 | * 题目内容 7 | * 使用递归方法编写函数,实现将输入的字符串反向保存 (6分) 8 | * 9 | * 关键要点 10 | * - C类型字符串的长度如何获取? 11 | * - 递归、递归截止条件 12 | * - 输入输出字符串(gets puts) (在代码中未体现) 13 | */ 14 | 15 | # include "../Problems/2004-1-1.c" 16 | # include 17 | # include 18 | 19 | int main() { 20 | std::string test; 21 | std::getline(std::cin, test); 22 | char* test_char = (char*)test.c_str(); 23 | reverse(test_char); 24 | std::cout << test.c_str(); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Test/2004-1-2-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 编写程序,从键盘上输入一个字符串,然后将输入的字符串按照字符的ASCII值排序,删除字符串中的空格, 6 | * 且相同的字符在串中仅出现一次,将排序后的字符串在屏幕上显示 7 | * 8 | * 分析 9 | * 主要考察计数排序的退化情况和ASCII码 10 | * 11 | * 要点 12 | * - 计数排序的退化情况 13 | * - 字符串输入和输出 14 | * - 数组、数组的初始化 15 | * - 基础ASCII码有127个,扩展打印字符集有256个 16 | * 17 | * 附加知识:几个重要的ASCII 18 | * ' ' 32 19 | * '0' 48 20 | * 'A' 65 21 | * 'a' 97 22 | */ 23 | # include "../Problems/2004-1-2.c" -------------------------------------------------------------------------------- /Test/2004-1-3-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 已知有一个含有n个节点的环形链表,定义如下: 6 | struct node { 7 | int value; 8 | struct node* next; 9 | }; 10 | * 编写函数,查找链表中的value成员数值最小节点,并输出其value值 11 | * 12 | * 分析 13 | * 请务必注意本题目的链表是环形表,故不可以用指针域为null来作为遍历的截止条件 14 | * 15 | * 要点 16 | * - 环形链表遍历 17 | * - 截止条件务必认真考虑 18 | * 19 | * 为测试方便,此处链表节点结构体的名称有所改变(从node变为node_int) 20 | * 且均包含在了测试文件库中。 21 | * 22 | */ 23 | # include "../Problems/2004-1-3.c" 24 | 25 | int main() { 26 | std::pair test = list_algo::list_ring_get({6, 3, 4, 6, 7, 2, 3, 5, 1}); 27 | findMin(test.first, test.second); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Test/2004-2-3-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 已知用二叉链表存储二叉树,试编写判断两颗二叉树是否相等的算法。试写明算法的基本思路。 6 | * 7 | * 分析 8 | * 二叉树相等即两棵树的节点的值完全相等。因此我们可以用BFS或者DFS。 9 | * 考虑到C语言里没有现成的队列ADT,但有调用栈帧,故选用DFS 10 | * 11 | * 要点 12 | * - 二叉树的深度优先遍历 13 | * - C语言无bool类型 14 | * 15 | * 原则上需要在答卷的程序中给出二叉树节点的声明,为测试方便,此处树节点结构体包含在了测试文件库中。 16 | * 我们假定它的声明是这样的: 17 | * struct int_tree_node { 18 | value_type value; 19 | int_tree_node *left; 20 | int_tree_node *right; 21 | }; 22 | */ 23 | 24 | # include "../Problems/2004-2-3.c" 25 | # include 26 | 27 | int main() { 28 | int_tree_node* test1 = bin_tree_algo::build_tree({1,2,3,4,5,114514,7,8}); 29 | int_tree_node* test2 = bin_tree_algo::build_tree({1,2,3,5,5,114514,7,8}); 30 | std::cout << compare_trees(test1, test2); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Test/2005-1-1-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 有五个各不相同的正整数,它们的和是135,且按照从小到大的顺序,后面一个数是前面一个数的整倍数。 6 | * 编写程序求这几个数。 7 | * 8 | * 分析 9 | * 翻译题。将题意用编程语言重新写一遍即可。鉴于数字很小,选用五重循环。 10 | */ 11 | # include "../Problems/2005-1-1.c" -------------------------------------------------------------------------------- /Test/2005-1-2-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 编写函数,实现将存放在变量n中的整数,逆序存放在变量m中。 6 | * 例如原来变量n保存的是483,程序运行后变量m中存放整数384 7 | * 8 | * 分析 9 | * 用简单的逐位提取即可,难度较低。 10 | * 可以使用数组存放各位,但没必要,这么做既低效,又会暴露出编程者较低的算法水平。 11 | * 最简洁明快而优雅的写法呈现在下方: 12 | * 13 | * 要点 14 | * - 循环 15 | */ 16 | 17 | # include "../Problems/2005-1-2.c" 18 | # include 19 | 20 | int main() { 21 | int test = -23456; 22 | std::cout << reverse_int(test); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Test/2005-1-3-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 编写程序,实现从键盘上输入两个字符串,将它们合并之后按ASCII码值从大到小排序在屏幕输出,相同的字符仅输出一次。 6 | * 7 | * 分析 8 | * 算法与2004-1-2完全一致,使用计数排序的退化情况。 9 | * 10 | * 要点 11 | * - 计数排序的退化情况 12 | */ 13 | # include "../Problems/2005-1-3.c" -------------------------------------------------------------------------------- /Test/2005-1-4-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 有一个保存学生课程成绩的结构数组,保存学生的考号和课程成绩。 6 | * 请编写一个函数,将保存在结构数组中的数据按照课程成绩从高到低的顺序存放在一个单向链表中。 7 | * 结构体定义如下 8 | * struct student { 9 | * int no; 10 | * float score; 11 | * }; 12 | * 13 | * 分析 14 | * 是要求较高的一道题。 15 | * 朴素地想,可以使用链表的插入排序来解决这一问题。但必须想到插入排序的效率偏低,而链表不支持快速排序等折半排序法。 16 | * 故考虑先对数组进行从小到大的排序,再对链表执行头插法构建得到从大到小的链表。 17 | * 本实现要求较高,如无能力则可以使用链表插入排序。 18 | * 19 | * 要点 20 | * - 链表的头插法构建 21 | * - malloc函数 22 | * - 快速排序qsort的库包含及调用 23 | * - 函数指针、void指针及指针类型转换 24 | * 25 | * 背景知识 26 | * qsort方法中比较函数的返回值 27 | * int compare(const void* a, const void* b)中: 28 | * 若返回值为负整数,则a会被排在b之前 29 | * 若返回值为正整数,则a会被排在b之后 30 | * 若返回值是0,则a和b不区分前后 31 | */ 32 | # include "../Problems/2005-1-4.c" 33 | # include 34 | # include "../Common/Lists.hpp" 35 | 36 | std::ostream& operator<<(std::ostream& OS, student stu) { 37 | return OS << stu.no << ',' << stu.score << '\n'; 38 | } 39 | 40 | int main() { 41 | std::vector test = {{1, 3}, {2, 25}, {3, 18}, {4, 48}}; 42 | node_stu* res = sort_student(&*test.begin(), test.size()); 43 | list_algo::print_list(res); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Test/2005-2-1-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 设有一个表头指针为h1的单链表。试设计一个算法,通过遍历一趟链表,将链表中所有节点的链接方向逆转。 6 | * 要求逆转之后的结果链表的表头指针h1指向原链表的最后一个节点。 7 | * 8 | * 分析 9 | * 标准的反转链表题。 10 | * 反转链表实际上就是建立链表,不要对"反转"产生过多的恐惧。 11 | * 建议做https://leetcode-cn.com/problems/reverse-linked-list/来练习。 12 | * 非常典型的程序。 13 | * 如果没有把握临场编写这一程序,建议背诵之。 14 | * 15 | * 要点 16 | * - 反转链表 17 | */ 18 | # include "../Common/Lists.hpp" 19 | # include "../Problems/2005-2-1.c" 20 | 21 | int main() { 22 | list_algo::node *test = list_algo::list_get({1, 2, 3, 4, 5, 6, 7}); 23 | list_algo::node *res = reverse_list(test); 24 | list_algo::print_list(res); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Test/2005-2-2-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 请给吃建立二叉排序树的算法 6 | * 7 | * 分析 8 | * 二叉查找树基本算法。 9 | * 10 | * 要点 11 | * - 建立二叉查找树 12 | */ 13 | # include "../Problems/2005-2-2.c" 14 | # include 15 | 16 | int main() { 17 | std::vector test = {5,1,2,4,3,8,7,6,9,12}; 18 | int_tree_node* res = BST_build(&*test.begin(), test.size()); 19 | bin_tree_algo::print_tree(res); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Test/2005-2-3-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 请给出统计二叉树叶节点个数的递归算法 6 | * 7 | * 分析 8 | * 比2题简单,白给DFS递归题。与判断二叉树相同的算法基本一致。 9 | * 10 | * 要点 11 | * - 叶子节点 12 | */ 13 | # include "../Problems/2005-2-3.c" 14 | # include 15 | 16 | int main() { 17 | std::vector test = {5,1,2,4,3,8,7,6,9,12}; 18 | int_tree_node* res = bin_tree_algo::build_tree(test); 19 | std::cout << number_of_node(res); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Test/2005-2-4-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 已知一个无符号整数n,写一算法,将其转化为8进制数。要求用链栈来实现。 6 | * 7 | * 分析 8 | * 没啥意思的题,为了用栈而用栈。 9 | * 考研可以将链栈当作ADT来用,但是如果纸面有富裕,建议简单地实现一下。 10 | * 11 | * 要点 12 | * - 链栈ADT 13 | */ 14 | # include "../Problems/2005-2-4.c" 15 | 16 | int main() { 17 | trans(64); 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Test/2006-1-1-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 编写函数,采用递归方法实现将输入的字符串反向输出 6 | * 7 | * 分析 8 | * 基础递归,为了考递归而考递归,没什么意思。 9 | */ 10 | 11 | # include "../Problems/2006-1-1.c" 12 | 13 | int main() { 14 | reverse_output(); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Test/2006-1-2-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 一个自然数被8除余1,所得之商被8除也余1,再将第二次的商被8除后余7,最后得到一个商为a。 6 | * 又知道被17除余4,所得的商被17除余15,最后得到一个商是a的二倍。 7 | * 求这个自然数 8 | * 9 | * 分析 10 | * 标准翻译题,认真点就不会错 11 | * 这个数应该是1993 12 | */ 13 | 14 | # include "../Problems/2006-1-2.c" 15 | # include 16 | 17 | int main() { 18 | std::cout << get_number(); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Test/2006-1-3-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 将一个数码倒过来所得到的新数叫原数的反序数。若一个数等于它的反序数,则称它为对称树。 6 | * 求不超过2006的最大对称数。 7 | * 8 | * 分析 9 | * 基本算法与2005-1-2一致,白给,别写错。 10 | */ 11 | 12 | # include "../Problems/2006-1-3.c" -------------------------------------------------------------------------------- /Test/2006-1-4-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 有一个保存学生课程成绩的结构数组,保存学生的学号、课程编号和课程成绩。 6 | * 请编写一个函数,将保存在结构数组中的数据先按照课程编号从小到大,再按照课程成绩从高到低的顺序存放在一个单向链表中。 7 | * 结构体定义如下 8 | * struct student { 9 | * int sno; // 学生编号 10 | * int cno; // 课程编号 11 | * float score; 12 | * }; 13 | * 14 | * 分析 15 | * 与2005-1-4大致相似,只是需要重新编写一下 16 | * 17 | * 要点 18 | * - 链表的头插法构建 19 | * - malloc函数 20 | * - 快速排序qsort的库包含及调用 21 | * - 函数指针、void指针及指针类型转换 22 | * 23 | * 背景知识 24 | * qsort方法中比较函数的返回值 25 | * int compare(const void* a, const void* b)中: 26 | * 若返回值为负整数,则a会被排在b之前 27 | * 若返回值为正整数,则a会被排在b之后 28 | * 若返回值是0,则a和b不区分前后 29 | */ 30 | 31 | # include "../Problems/2006-1-4.c" 32 | # include 33 | # include "../Common/Lists.hpp" 34 | 35 | std::ostream& operator<<(std::ostream& OS, student stu) { 36 | return OS << stu.sno << ',' << stu.cno << ',' << stu.score << '\n'; 37 | } 38 | 39 | int main() { 40 | std::vector test = {{1, 3, 15}, {2, 3, 25}, {3, 3, 18}, {4, 1, 48}}; 41 | node_stu* res = sort_student(&*test.begin(), test.size()); 42 | list_algo::print_list(res); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Test/2006-2-1-test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Chinese Comment by UTF-8 3 | * 4 | * 题目内容 5 | * 请给出层序遍历二叉树的算法 6 | * 7 | * 分析 8 | * 基本BFS,简单得很。 9 | */ 10 | 11 | # include "../Problems/2006-2-1.c" 12 | # include 13 | 14 | int main() { 15 | int_tree_node* test1 = bin_tree_algo::build_tree({1,2,3,4,114514,114514,7,8,114514,114514,9}); 16 | print_tree(test1); 17 | BFS(test1); 18 | return 0; 19 | } --------------------------------------------------------------------------------