├── Chapter10 ├── 10.1.cpp ├── 10.2.cpp ├── 10.3.cpp ├── 10.4.cpp ├── 10.5.cpp └── 10.6.cpp ├── Chapter11 ├── 11.1.cpp ├── 11.2.cpp ├── 11.3.cpp ├── 11.4.cpp ├── 11.5.cpp ├── 11.6.cpp └── 11.7.cpp ├── Chapter12 ├── 12.1.cpp ├── 12.2.cpp ├── 12.3.cpp ├── 12.4.cpp ├── 12.5.cpp ├── 12.6.cpp ├── 12.7.cpp └── 12.8.cpp ├── Chapter2 ├── 2.1.cpp ├── 2.10.cpp ├── 2.11.cpp ├── 2.2.cpp ├── 2.3.cpp ├── 2.4.cpp ├── 2.5.cpp ├── 2.6.cpp ├── 2.7.cpp ├── 2.8.cpp └── 2.9.cpp ├── Chapter3 ├── 3.1.cpp ├── 3.2.cpp ├── 3.3.cpp ├── 3.4.cpp ├── 3.5.cpp ├── 3.6.cpp └── 3.7.cpp ├── Chapter4 ├── 4.1.cpp ├── 4.2.cpp ├── 4.3.cpp ├── 4.4.cpp ├── 4.5.cpp ├── 4.6.cpp └── 4.7.cpp ├── Chapter5 ├── 5.1.cpp └── 5.2.cpp ├── Chapter6 ├── 6.1.cpp ├── 6.10.cpp ├── 6.11.cpp ├── 6.12.cpp ├── 6.13.cpp ├── 6.2.cpp ├── 6.3.cpp ├── 6.4.cpp ├── 6.5.cpp ├── 6.6.cpp ├── 6.7.cpp ├── 6.8.cpp └── 6.9.cpp ├── Chapter7 ├── 7.1.cpp └── 7.2.cpp ├── Chapter8 ├── 8.1.cpp ├── 8.2.cpp └── 8.3.cpp ├── Chapter9 ├── 9.1.cpp ├── 9.2.cpp └── 9.3.cpp └── README.md /Chapter10/10.1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:二叉搜索树 3 | * 题目来源:浙江大学复试上机题 4 | * 题目链接:http://t.cn/Ai9PUJtK 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | struct TreeNode { 14 | int data; 15 | TreeNode* leftChild; 16 | TreeNode* rightChild; 17 | TreeNode(int x): data(x), leftChild(nullptr), rightChild(nullptr) {} 18 | }; 19 | 20 | TreeNode* Insert(TreeNode* root, int x) { 21 | if (root == nullptr) { //创建新节点 22 | root = new TreeNode(x); 23 | } else if (x < root->data) { //插入左子树 24 | root->leftChild = Insert(root->leftChild, x); 25 | } else if (root->data < x) { //插入右子树 26 | root->rightChild = Insert(root->rightChild, x); 27 | } 28 | return root; 29 | } 30 | 31 | bool Same(TreeNode* original, TreeNode* root) { 32 | if (original == nullptr && root == nullptr) { 33 | return true; 34 | } 35 | if (original != nullptr && root != nullptr && original->data == root->data) { 36 | return Same(original->leftChild, root->leftChild) && Same(original->rightChild, root->rightChild); 37 | } 38 | return false; 39 | } 40 | 41 | int main() { 42 | int n; 43 | while (scanf("%d", &n) != EOF) { 44 | if (n == 0) { 45 | break; 46 | } 47 | TreeNode* original = nullptr; //建立空树 48 | string str; 49 | cin >> str; 50 | for (int j = 0; j < str.size(); ++j) { 51 | original = Insert(original, str[j] - '0'); 52 | } 53 | for (int i = 0; i < n; ++i) { 54 | TreeNode* root = nullptr; 55 | cin >> str; 56 | for (int j = 0; j < str.size(); ++j) { 57 | root = Insert(root, str[j] - '0'); 58 | } 59 | if (Same(original, root)) { 60 | printf("YES\n"); 61 | } else { 62 | printf("NO\n"); 63 | } 64 | } 65 | } 66 | return 0; 67 | } -------------------------------------------------------------------------------- /Chapter10/10.2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:查找第K小数 3 | * 题目来源:北京邮电大学复试上机题 4 | * 题目链接:http://t.cn/AiCu5hcK 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | int main() { 15 | int n; 16 | while (scanf("%d", &n) != EOF) { 17 | priority_queue, greater > myPriorityQueue; 18 | while (n--) { 19 | int x; 20 | scanf("%d", &x); 21 | myPriorityQueue.push(x); 22 | } 23 | int k; 24 | scanf("%d", &k); 25 | int current = myPriorityQueue.top(); 26 | myPriorityQueue.pop(); 27 | for (int i = 1; i < k; ++i) { 28 | while (current == myPriorityQueue.top()) { 29 | myPriorityQueue.pop(); 30 | } 31 | current = myPriorityQueue.top(); 32 | myPriorityQueue.pop(); 33 | } 34 | printf("%d\n", current); 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Chapter10/10.3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:搬水果 3 | * 题目来源:吉林大学复试上机题 4 | * 题目链接:http://t.cn/AiCu5nsQ 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | int main() { 15 | int n; 16 | while (scanf("%d", &n) != EOF) { 17 | if (n == 0) { 18 | break; 19 | } 20 | priority_queue, greater > myPriorityQueue; 21 | while (n--) { 22 | int x; 23 | scanf("%d", &x); 24 | myPriorityQueue.push(x); 25 | } 26 | int answer = 0; 27 | while (1 < myPriorityQueue.size()) { 28 | int a = myPriorityQueue.top(); 29 | myPriorityQueue.pop(); 30 | int b = myPriorityQueue.top(); 31 | myPriorityQueue.pop(); 32 | answer += a + b; 33 | myPriorityQueue.push(a + b); 34 | } 35 | printf("%d\n", answer); 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Chapter10/10.4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:统计同成绩学生人数 3 | * 题目来源:浙江大学复试上机题 4 | * 题目链接:http://t.cn/AiCuM7nj 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | int main() { 15 | int n; 16 | while (scanf("%d", &n) != EOF) { 17 | if (n == 0) { 18 | break; 19 | } 20 | map myMap; 21 | while (n--) { 22 | int score; 23 | scanf("%d", &score); 24 | myMap[score]++; 25 | } 26 | int query; 27 | scanf("%d", &query); 28 | printf("%d\n", myMap[query]); 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Chapter10/10.5.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:开门人和关门人 3 | * 题目来源:浙江大学复试上机题 4 | * 题目链接:http://t.cn/AiCuM09f 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | int main() { 15 | int n; 16 | while (scanf("%d", &n) != EOF) { 17 | map arrive; 18 | map leave; 19 | while (n--) { 20 | string id, arriveTime, leaveTime; 21 | cin >> id >> arriveTime >> leaveTime; 22 | arrive[arriveTime] = id; 23 | leave[leaveTime] = id; 24 | } 25 | cout << arrive.begin()->second << " " << leave.rbegin()->second << endl; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Chapter10/10.6.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:谁是你的潜在朋友 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/AiCux4f7 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int MAXN = 200 + 10; 15 | 16 | int books[MAXN]; 17 | 18 | int main() { 19 | int n, m; 20 | while (scanf("%d%d", &n, &m) != EOF) { 21 | map myMap; 22 | for (int i = 0; i < n; ++i) { 23 | scanf("%d", &books[i]); 24 | myMap[books[i]]++; 25 | } 26 | for (int i = 0; i < n; ++i) { 27 | if (myMap[books[i]] == 1) { 28 | printf("BeiJu\n"); 29 | } else { 30 | printf("%d\n", myMap[books[i]] - 1); 31 | } 32 | } 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Chapter11/11.1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:找出直系亲属 3 | * 题目来源:浙江大学复试上机题 4 | * 题目链接:http://t.cn/AiOzTX5c 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | const int MAXN = 30; 14 | 15 | int children[MAXN]; 16 | 17 | int Generation(int x, int y) { 18 | int level; 19 | 20 | level = 0; 21 | int a = x; 22 | while (children[a] != a) { 23 | a = children[a]; 24 | level++; 25 | if (a == y) { 26 | return level; 27 | } 28 | } 29 | 30 | level = 0; 31 | int b = y; 32 | while (children[b] != b) { 33 | b = children[b]; 34 | level--; 35 | if (b == x) { 36 | return level; 37 | } 38 | } 39 | 40 | return 0; 41 | } 42 | 43 | string Relationship(int level) { 44 | string answer; 45 | if (level == 0) { 46 | answer = "-"; 47 | } else if (level == 1) { 48 | answer = "parent"; 49 | } else if (level == 2) { 50 | answer = "grandparent"; 51 | } else if (level > 2) { 52 | for (int j = 0; j < level - 2; ++j) { 53 | answer += "great-"; 54 | } 55 | answer += "grandparent"; 56 | } else if (level == -1) { 57 | answer = "child"; 58 | } else if (level == -2) { 59 | answer = "grandchild"; 60 | } else if (level < -2) { 61 | for (int j = 0; j < -2 - level; ++j) { 62 | answer += "great-"; 63 | } 64 | answer += "grandchild"; 65 | } 66 | return answer; 67 | } 68 | 69 | int main() { 70 | int n, m; 71 | while (scanf("%d%d", &n, &m) != EOF) { 72 | for (int i = 0; i < MAXN; ++i) { 73 | children[i] = i; 74 | } 75 | for (int i = 0; i < n; ++i) { 76 | char child, father, mother; 77 | cin >> child >> father >> mother; 78 | if (father - 'A' != '-') { 79 | children[father - 'A'] = child - 'A'; 80 | } 81 | if (mother - 'A' != '-') { 82 | children[mother - 'A'] = child - 'A'; 83 | } 84 | } 85 | for (int i = 0; i < m; ++i) { 86 | char guy1, guy2; 87 | cin >> guy1 >> guy2; 88 | int level = Generation(guy1 - 'A', guy2 - 'A'); 89 | cout << Relationship(level) << endl; 90 | } 91 | } 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /Chapter11/11.2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:第一题 3 | * 题目来源:上海交通大学复试上机题 4 | * 题目链接:http://t.cn/AiOhkgMJ 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | const int MAXN = 1e6; 14 | 15 | int father[MAXN]; //父亲结点 16 | int height[MAXN]; //结点高度 17 | bool visit[MAXN]; //标记 18 | 19 | void Initial(int n) { //初始化 20 | for (int i = 0; i < n; i++) { 21 | father[i] = i; //每个结点的父亲为自己 22 | height[i] = 0; //每个结点的高度为零 23 | } 24 | } 25 | 26 | int Find(int x) { //查找根结点 27 | if (x != father[x]) { //路径压缩 28 | father[x] = Find(father[x]); 29 | } 30 | return father[x]; 31 | } 32 | 33 | void Union(int x, int y) { //合并集合 34 | x = Find(x); 35 | y = Find(y); 36 | if (x != y) { //矮树作为高树的子树 37 | if (height[x] < height[y]) { 38 | father[x] = y; 39 | } else if (height[y] < height[x]) { 40 | father[y] = x; 41 | } else { 42 | father[y] = x; 43 | height[x]++; 44 | } 45 | } 46 | return ; 47 | } 48 | 49 | int main() { 50 | Initial(MAXN); //初始化 51 | int x, y; 52 | while (scanf("%d%d", &x, &y) != EOF) { 53 | visit[x] = true; 54 | visit[y] = true; 55 | Union(x, y); //合并集合 56 | } 57 | int component = 0; //连通分量 58 | for (int i = 0; i < MAXN; i++) { 59 | if (visit[i] && Find(i) == i) { //集合数目 60 | component++; 61 | } 62 | } 63 | printf("%d\n", component); 64 | return 0; 65 | } -------------------------------------------------------------------------------- /Chapter11/11.3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:Head of a Gang 3 | * 题目来源:浙江大学复试上机题 4 | * 题目链接:http://t.cn/AiOzQMBH 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int MAXN = 1000 + 10; 15 | 16 | struct Relation { 17 | string name1; 18 | string name2; 19 | int time; 20 | }; 21 | 22 | Relation arr[MAXN]; 23 | map father; 24 | map weight; 25 | 26 | string Find(string name) { 27 | if (name != father[name]) { 28 | father[name] = Find(father[name]); 29 | } 30 | return father[name]; 31 | } 32 | 33 | void Union(string name1, string name2) { 34 | name1 = Find(name1); 35 | name2 = Find(name2); 36 | if (name1 != name2) { 37 | if (weight[name1] < weight[name2]) { 38 | father[name1] = name2; 39 | } else { 40 | father[name2] = name1; 41 | } 42 | } 43 | } 44 | 45 | int main() { 46 | int n, k; 47 | while (scanf("%d%d", &n, &k) != EOF) { 48 | father.clear(); 49 | weight.clear(); 50 | for (int i = 0; i < n; ++i) { 51 | cin >> arr[i].name1 >> arr[i].name2 >> arr[i].time; 52 | weight[arr[i].name1] += arr[i].time; 53 | weight[arr[i].name2] += arr[i].time; 54 | father[arr[i].name1] = arr[i].name1; 55 | father[arr[i].name2] = arr[i].name2; 56 | } 57 | for (int i = 0; i < n; ++i) { 58 | Union(arr[i].name1, arr[i].name2); 59 | } 60 | map sum; 61 | map count; 62 | map answer; 63 | for (map::iterator it = father.begin(); it != father.end(); ++it) { 64 | string root = Find(it->first); 65 | sum[root] += weight[it->first]; 66 | count[root]++; 67 | } 68 | for (map::iterator it = sum.begin(); it != sum.end(); ++it) { 69 | if (k * 2 < it->second && 2 < count[it->first]) { 70 | answer[it->first] = count[it->first]; 71 | } 72 | } 73 | printf("%d\n", answer.size()); 74 | for (map::iterator it = answer.begin(); it != answer.end(); ++it) { 75 | cout << it->first << " " << it->second << endl; 76 | } 77 | } 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /Chapter11/11.4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:Freckles 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/AiW3Hbqr 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | const int MAXN = 100 + 10; 16 | 17 | struct Edge { 18 | int from; //边的起点 19 | int to; //边的终点 20 | double length; //边的长度 21 | bool operator< (const Edge& e) const { 22 | return length < e.length; 23 | } 24 | }; 25 | 26 | struct Point { 27 | double x, y; 28 | }; 29 | 30 | Point point[MAXN]; 31 | Edge edge[MAXN * MAXN]; //边集 32 | int father[MAXN]; //父亲结点 33 | int height[MAXN]; //结点高度 34 | 35 | void Initial(int n) { //初始化 36 | for (int i = 0; i <= n; i++) { 37 | father[i] = i; 38 | height[i] = 0; 39 | } 40 | } 41 | 42 | int Find(int x) { //查找根结点 43 | if (x != father[x]) { 44 | father[x] = Find(father[x]); 45 | } 46 | return father[x]; 47 | } 48 | 49 | void Union(int x, int y) { //合并集合 50 | x = Find(x); 51 | y = Find(y); 52 | if (x != y) { 53 | if (height[x] < height[y]) { 54 | father[x] = y; 55 | } else if (height[y] < height[x]) { 56 | father[y] = x; 57 | } else { 58 | father[y] = x; 59 | height[x]++; 60 | } 61 | } 62 | return ; 63 | } 64 | 65 | double Kruskal(int n, int edgeNumber) { 66 | Initial(n); 67 | sort(edge, edge + edgeNumber); //按权值排序 68 | double sum = 0; 69 | for (int i = 0; i < edgeNumber; ++i) { 70 | Edge current = edge[i]; 71 | if (Find(current.from) != Find(current.to)) { 72 | Union(current.from, current.to); 73 | sum += current.length; 74 | } 75 | } 76 | return sum; 77 | } 78 | 79 | int main() { 80 | int n; 81 | while (scanf("%d", &n) != EOF) { 82 | for (int i = 0; i < n; ++i) { 83 | scanf("%lf%lf", &point[i].x, &point[i].y); 84 | } 85 | int edgeNumber = n * (n - 1) / 2; 86 | int index = 0; 87 | for (int i = 0; i < n; ++i) { 88 | for (int j = i + 1; j < n; ++j) { 89 | edge[index].from = i; 90 | edge[index].to = j; 91 | edge[index].length = sqrt(pow(point[i].x - point[j].x, 2) + pow(point[i].y - point[j].y, 2)); 92 | index++; 93 | } 94 | } 95 | double answer = Kruskal(n, edgeNumber); 96 | printf("%.2f\n", answer); 97 | } 98 | return 0; 99 | } -------------------------------------------------------------------------------- /Chapter11/11.5.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:Jungle Roads 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/AiW33P91 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int MAXN = 100 + 10; 15 | 16 | struct Edge { 17 | int from; //边的起点 18 | int to; //边的终点 19 | int length; //边的长度 20 | Edge() {} 21 | Edge(int f, int t, int l):from(f), to(t), length(l) {} 22 | bool operator< (const Edge& e) const { 23 | return length < e.length; 24 | } 25 | }; 26 | 27 | Edge edge[MAXN * MAXN]; //边集 28 | int father[MAXN]; //父亲结点 29 | int height[MAXN]; //结点高度 30 | 31 | void Initial(int n) { //初始化 32 | for (int i = 0; i <= n; i++) { 33 | father[i] = i; 34 | height[i] = 0; 35 | } 36 | } 37 | 38 | int Find(int x) { //查找根结点 39 | if (x != father[x]) { 40 | father[x] = Find(father[x]); 41 | } 42 | return father[x]; 43 | } 44 | 45 | void Union(int x, int y) { //合并集合 46 | x = Find(x); 47 | y = Find(y); 48 | if (x != y) { 49 | if (height[x] < height[y]) { 50 | father[x] = y; 51 | } else if (height[y] < height[x]) { 52 | father[y] = x; 53 | } else { 54 | father[y] = x; 55 | height[x]++; 56 | } 57 | } 58 | return ; 59 | } 60 | 61 | int Kruskal(int n, int edgeNumber) { 62 | Initial(n); 63 | sort(edge, edge + edgeNumber); //按权值排序 64 | int sum = 0; 65 | for (int i = 0; i < edgeNumber; ++i) { 66 | Edge current = edge[i]; 67 | if (Find(current.from) != Find(current.to)) { 68 | Union(current.from, current.to); 69 | sum += current.length; 70 | } 71 | } 72 | return sum; 73 | } 74 | 75 | int main() { 76 | int n; 77 | while (scanf("%d", &n) != EOF) { 78 | if (n == 0) { 79 | break; 80 | } 81 | int edgeNumber = 0; 82 | for (int i = 0; i < n - 1; ++i) { 83 | char from; 84 | int number; 85 | cin >> from >> number; 86 | for (int j = 0; j < number; ++j) { 87 | char to; 88 | int length; 89 | cin >> to >> length; 90 | edge[edgeNumber++] = Edge(from - 'A', to - 'A', length); 91 | } 92 | } 93 | int answer = Kruskal(n, edgeNumber); 94 | printf("%d\n", answer); 95 | } 96 | return 0; 97 | } -------------------------------------------------------------------------------- /Chapter11/11.6.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:最短路径 3 | * 题目来源:上海交通大学复试上机题 4 | * 题目链接:http://t.cn/AilPKHTx 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | struct BigInteger { 17 | int digit[10000]; 18 | int length; 19 | BigInteger(); 20 | BigInteger(int x); 21 | BigInteger(string str); 22 | BigInteger(const BigInteger& b); 23 | BigInteger operator=(int x); 24 | BigInteger operator=(string str); 25 | BigInteger operator=(const BigInteger& b); 26 | bool operator<(const BigInteger& b); 27 | bool operator<=(const BigInteger& b); 28 | bool operator==(const BigInteger& b); 29 | BigInteger operator+(const BigInteger& b); 30 | BigInteger operator-(const BigInteger& b); 31 | BigInteger operator*(const BigInteger& b); 32 | BigInteger operator/(const BigInteger& b); 33 | BigInteger operator%(const BigInteger& b); 34 | friend istream& operator>>(istream& in, BigInteger& b); 35 | friend ostream& operator<<(ostream& out, const BigInteger& b); 36 | }; 37 | 38 | istream& operator>>(istream& in, BigInteger& b) { 39 | string str; 40 | in >> str; 41 | b = str; 42 | return in; 43 | } 44 | 45 | ostream& operator<<(ostream& out, const BigInteger& b) { 46 | for (int i = b.length - 1; i >= 0; --i) { 47 | out << b.digit[i]; 48 | } 49 | return out; 50 | } 51 | 52 | BigInteger::BigInteger() { 53 | memset(digit, 0, sizeof(digit)); 54 | length = 0; 55 | } 56 | 57 | BigInteger::BigInteger(int x) { 58 | memset(digit, 0, sizeof(digit)); 59 | length = 0; 60 | if (x == 0) { 61 | digit[length++] = x; 62 | } 63 | while (x != 0) { 64 | digit[length++] = x % 10; 65 | x /= 10; 66 | } 67 | } 68 | 69 | BigInteger::BigInteger(string str) { 70 | memset(digit, 0, sizeof(digit)); 71 | length = str.size(); 72 | for (int i = 0; i < length; ++i) { 73 | digit[i] = str[length - i - 1] - '0'; 74 | } 75 | } 76 | 77 | BigInteger::BigInteger(const BigInteger& b) { 78 | memset(digit, 0, sizeof(digit)); 79 | length = b.length; 80 | for (int i = 0; i < length; ++i) { 81 | digit[i] = b.digit[i]; 82 | } 83 | } 84 | 85 | BigInteger BigInteger::operator=(int x) { 86 | memset(digit, 0, sizeof(digit)); 87 | length = 0; 88 | if (x == 0) { 89 | digit[length++] = x; 90 | } 91 | while (x != 0) { 92 | digit[length++] = x % 10; 93 | x /= 10; 94 | } 95 | return *this; 96 | } 97 | 98 | BigInteger BigInteger::operator=(string str) { 99 | memset(digit, 0, sizeof(digit)); 100 | length = str.size(); 101 | for (int i = 0; i < length; ++i) { 102 | digit[i] = str[length - i - 1] - '0'; 103 | } 104 | return *this; 105 | } 106 | 107 | BigInteger BigInteger::operator=(const BigInteger& b) { 108 | memset(digit, 0, sizeof(digit)); 109 | length = b.length; 110 | for (int i = 0; i < length; ++i) { 111 | digit[i] = b.digit[i]; 112 | } 113 | return *this; 114 | } 115 | 116 | bool BigInteger::operator<(const BigInteger& b) { 117 | if (length < b.length) { 118 | return true; 119 | } else if (b.length < length) { 120 | return false; 121 | } else { 122 | for (int i = length - 1; i >= 0; --i) { 123 | if (digit[i] == b.digit[i]) { 124 | continue; 125 | } else { 126 | return digit[i] < b.digit[i]; 127 | } 128 | } 129 | } 130 | return false; 131 | } 132 | 133 | bool BigInteger::operator<=(const BigInteger& b) { 134 | if (length < b.length) { 135 | return true; 136 | } else if (b.length < length) { 137 | return false; 138 | } else { 139 | for (int i = length - 1; i >= 0; --i) { 140 | if (digit[i] == b.digit[i]) { 141 | continue; 142 | } else { 143 | return digit[i] < b.digit[i]; 144 | } 145 | } 146 | } 147 | return true; 148 | } 149 | 150 | bool BigInteger::operator==(const BigInteger& b) { 151 | if (length != b.length) { 152 | return false; 153 | } else { 154 | for (int i = length - 1; i >= 0; --i) { 155 | if (digit[i] != b.digit[i]) { 156 | return false; 157 | } 158 | } 159 | } 160 | return true; 161 | } 162 | 163 | BigInteger BigInteger::operator+(const BigInteger& b) { 164 | BigInteger answer; 165 | int carry = 0; 166 | for (int i = 0; i < length || i < b.length; ++i) { 167 | int current = carry + digit[i] + b.digit[i]; 168 | carry = current / 10; 169 | answer.digit[answer.length++] = current % 10; 170 | } 171 | if (carry != 0) { 172 | answer.digit[answer.length++] = carry; 173 | } 174 | return answer; 175 | } 176 | 177 | BigInteger BigInteger::operator-(const BigInteger& b) { 178 | BigInteger answer; 179 | int carry = 0; 180 | for (int i = 0; i < length; ++i) { 181 | int current = digit[i] - b.digit[i] - carry; 182 | if (current < 0) { 183 | current += 10; 184 | carry = 1; 185 | } else { 186 | carry = 0; 187 | } 188 | answer.digit[answer.length++] = current; 189 | } 190 | while (answer.digit[answer.length - 1] == 0 && answer.length > 1) { 191 | answer.length--; 192 | } 193 | return answer; 194 | 195 | } 196 | 197 | BigInteger BigInteger::operator*(const BigInteger& b) { 198 | BigInteger answer; 199 | answer.length = length + b.length; 200 | for (int i = 0; i < length; ++i) { 201 | for (int j = 0; j < b.length; ++j) { 202 | answer.digit[i + j] += digit[i] * b.digit[j]; 203 | } 204 | } 205 | for (int i = 0; i < answer.length; ++i) { 206 | answer.digit[i + 1] += answer.digit[i] / 10; 207 | answer.digit[i] %= 10; 208 | } 209 | while (answer.digit[answer.length - 1] == 0 && answer.length > 1) { 210 | answer.length--; 211 | } 212 | return answer; 213 | } 214 | 215 | BigInteger BigInteger::operator/(const BigInteger& b) { 216 | BigInteger answer; 217 | answer.length = length; 218 | BigInteger remainder = 0; 219 | BigInteger temp = b; 220 | for (int i = length - 1; i >= 0; --i) { 221 | if (!(remainder.length == 1 && remainder.digit[0] == 0)) { 222 | for (int j = remainder.length - 1; j >= 0; --j) { 223 | remainder.digit[j + 1] = remainder.digit[j]; 224 | } 225 | remainder.length++; 226 | } 227 | remainder.digit[0] = digit[i]; 228 | while (temp <= remainder) { 229 | remainder = remainder - temp; 230 | answer.digit[i]++; 231 | } 232 | } 233 | while (answer.digit[answer.length - 1] == 0 && answer.length > 1) { 234 | answer.length--; 235 | } 236 | return answer; 237 | } 238 | 239 | BigInteger BigInteger::operator%(const BigInteger& b) { 240 | BigInteger remainder = 0; 241 | BigInteger temp = b; 242 | for (int i = length - 1; i >= 0; --i) { 243 | if (!(remainder.length == 1 && remainder.digit[0] == 0)) { 244 | for (int j = remainder.length - 1; j >= 0; --j) { 245 | remainder.digit[j + 1] = remainder.digit[j]; 246 | } 247 | remainder.length++; 248 | } 249 | remainder.digit[0] = digit[i]; 250 | while (temp <= remainder) { 251 | remainder = remainder - temp; 252 | } 253 | } 254 | return remainder; 255 | } 256 | 257 | const int MAXN = 100 + 10; 258 | const int MOD = 1e5; 259 | 260 | struct Edge { 261 | int to; //终点 262 | BigInteger length; //长度 263 | Edge(int t, BigInteger l): to(t), length(l) {} 264 | }; 265 | 266 | struct Point { 267 | int number; //点的编号 268 | BigInteger distance; //源点到该点距离 269 | Point(int n, BigInteger d): number(n), distance(d) {} 270 | bool operator< (Point p) const { 271 | return p.distance < distance; //距离小的优先级高 272 | } 273 | }; 274 | 275 | vector graph[MAXN]; //邻接表实现的图 276 | BigInteger minDistance[MAXN]; //源点到各点最短距离 277 | 278 | void Dijkstra(int start) { 279 | minDistance[start] = 0; 280 | priority_queue myPriorityQueue; 281 | myPriorityQueue.push(Point(start, minDistance[start])); 282 | while (!myPriorityQueue.empty()) { 283 | int u = myPriorityQueue.top().number; //离源点最近的点 284 | myPriorityQueue.pop(); 285 | for (int i = 0; i < graph[u].size(); ++i) { 286 | int v = graph[u][i].to; 287 | BigInteger l = graph[u][i].length; 288 | if (minDistance[u] + l < minDistance[v]) { 289 | minDistance[v] = minDistance[u] + l; 290 | myPriorityQueue.push(Point(v, minDistance[v])); 291 | } 292 | } 293 | } 294 | return ; 295 | } 296 | 297 | int main() { 298 | int n, m; 299 | while (scanf("%d%d", &n, &m) != EOF) { 300 | memset(graph, 0, sizeof(graph)); 301 | BigInteger length = 1; 302 | for (int i = 0; i < m; ++i) { 303 | int from, to; 304 | scanf("%d%d", &from, &to); 305 | graph[from].push_back(Edge(to, length)); 306 | graph[to].push_back(Edge(from, length)); 307 | length = length * BigInteger(2); 308 | } 309 | BigInteger INF = length; 310 | fill(minDistance, minDistance + n, INF); 311 | Dijkstra(0); 312 | for (int i = 1; i < n; ++i) { 313 | if (minDistance[i] == INF) { 314 | cout << -1 << endl; 315 | } else { 316 | cout << minDistance[i] % MOD << endl; 317 | } 318 | } 319 | } 320 | return 0; 321 | } -------------------------------------------------------------------------------- /Chapter11/11.7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:I Wanna Go Home 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://dwz.win/Kaw 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | using namespace std; 16 | 17 | const int MAXN = 600 + 10; 18 | const int INF = INT_MAX; 19 | 20 | struct Edge{ 21 | int to; 22 | int length; 23 | Edge(int t, int l): to(t), length(l) {} 24 | }; 25 | 26 | struct Point{ 27 | int number; 28 | int distance; 29 | Point(int n, int d): number(n), distance(d) {} 30 | bool operator< (const Point& p) const{ 31 | return distance > p.distance; 32 | } 33 | }; 34 | 35 | vector graph[MAXN]; //邻接表实现的图 36 | int minDistance[MAXN]; //源点到各点最短距离 37 | int arr[MAXN]; 38 | 39 | void Dijkstra(int start){ 40 | minDistance[start] = 0; 41 | priority_queue myPriorityQueue; 42 | myPriorityQueue.push(Point(start, minDistance[start])); 43 | while (!myPriorityQueue.empty()) { 44 | int u = myPriorityQueue.top().number; //离源点最近的点 45 | myPriorityQueue.pop(); 46 | for (int i = 0; i < graph[u].size(); ++i) { 47 | int v = graph[u][i].to; 48 | int l = graph[u][i].length; 49 | if (!(arr[u - 1] == 2 && arr[v - 1] == 1) && minDistance[v] > minDistance[u] + l) { 50 | minDistance[v] = minDistance[u] + l; 51 | myPriorityQueue.push(Point(v, minDistance[v])); 52 | } 53 | } 54 | } 55 | } 56 | 57 | int main(){ 58 | int n, m; 59 | while (scanf("%d", &n) != EOF) { 60 | if (n == 0) { 61 | break; 62 | } 63 | scanf("%d", &m); 64 | memset(graph, 0, sizeof(graph)); 65 | fill(minDistance, minDistance + n, INF); 66 | while (m--) { 67 | int from, to, length; 68 | scanf("%d%d%d", &from, &to, &length); 69 | graph[from].push_back(Edge(to, length)); 70 | graph[to].push_back(Edge(from, length)); 71 | } 72 | for (int i = 0; i < n; ++i){ 73 | scanf("%d", &arr[i]); 74 | } 75 | int start = 1; 76 | int terminal = 2; 77 | Dijkstra(start); 78 | if (minDistance[terminal] == INF) { 79 | minDistance[terminal] = -1; 80 | } 81 | printf("%d\n", minDistance[terminal]); 82 | } 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /Chapter12/12.1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:吃糖果 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/AiQsVyKz 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | const int MAXN = 25; 14 | 15 | int number[MAXN]; 16 | 17 | int main() { 18 | number[1] = 1; 19 | number[2] = 2; 20 | for (int i = 3; i < MAXN; ++i) { 21 | number[i] = number[i - 1] + number[i - 2]; 22 | } 23 | int n; 24 | while (scanf("%d", &n) != EOF) { 25 | printf("%d\n", number[n]); 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Chapter12/12.2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:最大连续子序列 3 | * 题目来源:浙江大学复试上机题 4 | * 题目链接:http://t.cn/AiYoUkjP 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int MAXN = 1e4 + 10; 15 | const int INF = INT_MAX; 16 | 17 | int arr[MAXN]; 18 | int dp[MAXN]; 19 | 20 | int main() { 21 | int n; 22 | while (scanf("%d", &n) != EOF) { 23 | if (n == 0) { 24 | break; 25 | } 26 | for (int i = 0; i < n; ++i) { 27 | scanf("%d", &arr[i]); 28 | } 29 | int maximum = -INF; 30 | int start, last; 31 | int head, tail; 32 | for (int i = 0; i < n; ++i) { 33 | if (i == 0 || dp[i - 1] < 0) { 34 | dp[i] = arr[i]; 35 | start = arr[i]; 36 | last = arr[i]; 37 | } else { 38 | dp[i] = dp[i - 1] + arr[i]; 39 | last = arr[i]; 40 | } 41 | if (maximum < dp[i]) { 42 | maximum = dp[i]; 43 | head = start; 44 | tail = last; 45 | } 46 | } 47 | if (maximum < 0) { 48 | maximum = 0; 49 | head = arr[0]; 50 | tail = arr[n - 1]; 51 | } 52 | printf("%d %d %d\n", maximum, head, tail); 53 | } 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Chapter12/12.3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:合唱队形 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/AiYNyHPe 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int MAXN = 100 + 10; 15 | const int INF = INT_MAX; 16 | 17 | int height[MAXN]; 18 | int ascend[MAXN]; 19 | int descend[MAXN]; 20 | 21 | int main() { 22 | int n; 23 | while (scanf("%d", &n) != EOF) { 24 | for (int i = 0; i < n; ++i) { 25 | scanf("%d", &height[i]); 26 | } 27 | for (int i = 0; i < n; ++i) { 28 | ascend[i] = 1; 29 | for (int j = 0; j < i; ++j) { 30 | if (height[j] < height[i]) { 31 | ascend[i] = max(ascend[i], ascend[j] + 1); 32 | } 33 | } 34 | } 35 | for (int i = n - 1; i >= 0; --i) { 36 | descend[i] = 1; 37 | for (int j = n - 1; j > i; --j) { 38 | if (height[j] < height[i]) { 39 | descend[i] = max(descend[i], descend[j] + 1); 40 | } 41 | } 42 | } 43 | int minimum = INF; 44 | for (int i = 0; i < n; ++i) { 45 | minimum = min(minimum, n - ascend[i] - descend[i] + 1); 46 | } 47 | printf("%d\n", minimum); 48 | } 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Chapter12/12.4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:采药 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://dwz.win/Knc 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | const int MAXN = 100 + 10; 14 | const int MAXM = 1000 + 10; 15 | 16 | int dp[MAXM]; 17 | int value[MAXN]; //物品价值 18 | int weight[MAXN]; //物品重量 19 | 20 | int main() { 21 | int n, m; //n件物品,m容量的背包 22 | while (scanf("%d%d", &m, &n) != EOF) { 23 | for (int i = 0; i < n; ++i) { 24 | scanf("%d%d", &weight[i], &value[i]); 25 | } 26 | for (int i = 0; i <= m; ++i) { 27 | dp[i] = 0; //初始化 28 | } 29 | for (int i = 0; i < n; ++i) { 30 | for (int j = m; j >= weight[i]; --j) { 31 | dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); 32 | } 33 | } 34 | printf("%d\n", dp[m]); 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Chapter12/12.5.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:采药 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://dwz.win/Knc 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | const int MAXN = 100 + 10; 14 | const int MAXM = 1000 + 10; 15 | 16 | int dp[MAXM]; 17 | int value[MAXN]; //物品价值 18 | int weight[MAXN]; //物品重量 19 | 20 | int main() { 21 | int n, m; //n件物品,m容量的背包 22 | while (scanf("%d%d", &m, &n) != EOF) { 23 | for (int i = 0; i < n; ++i) { 24 | scanf("%d%d", &weight[i], &value[i]); 25 | } 26 | for (int i = 0; i <= m; ++i) { 27 | dp[i] = 0; //初始化 28 | } 29 | for (int i = 0; i < n; ++i) { 30 | for (int j = m; j >= weight[i]; --j) { 31 | dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); 32 | } 33 | } 34 | printf("%d\n", dp[m]); 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Chapter12/12.6.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:最小邮票数 3 | * 题目来源:清华大学复试上机题 4 | * 题目链接:http://t.cn/AiYlwchD 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | const int MAXN = 20 + 10; 14 | const int MAXM = 100 + 10; 15 | const int INF = 0x7f7f7f7f; 16 | 17 | int dp[MAXM]; 18 | int weight[MAXN]; 19 | 20 | int main() { 21 | int n, m; 22 | while (scanf("%d%d", &m, &n) != EOF) { 23 | for (int i = 0; i < n; ++i) { 24 | scanf("%d", &weight[i]); 25 | } 26 | for (int i = 1; i <= m; ++i) { 27 | dp[i] = INF; 28 | } 29 | dp[0] = 0; 30 | for (int i = 0; i < n; ++i) { 31 | for (int j = m; j >= weight[i]; --j) { 32 | dp[j] = min(dp[j], dp[j - weight[i]] + 1); 33 | } 34 | } 35 | if (dp[m] == INF) { 36 | printf("%d\n", 0); 37 | } else { 38 | printf("%d\n", dp[m]); 39 | } 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Chapter12/12.7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:放苹果 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/AiQsyOnq 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | const int MAXN = 10 + 10; 14 | 15 | int dp[MAXN][MAXN]; 16 | 17 | int main() { 18 | int m, n; 19 | while (scanf("%d%d", &m, &n) != EOF) { 20 | for (int i = 0; i <= m; ++i) { 21 | dp[i][1] = 1; 22 | } 23 | for (int j = 0; j <= n; ++j) { 24 | dp[0][j] = 1; 25 | } 26 | for (int i = 1; i <= m; ++i) { 27 | for (int j = 1; j <= n; ++j) { 28 | if (j <= i) { 29 | dp[i][j] = dp[i][j - 1] + dp[i - j][j]; 30 | } else { 31 | dp[i][j] = dp[i][i]; 32 | } 33 | } 34 | } 35 | printf("%d\n", dp[m][n]); 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Chapter12/12.8.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:整数拆分 3 | * 题目来源:清华大学复试上机题 4 | * 题目链接:http://t.cn/AiQsUM0Q 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | const int MOD = 1e9; 13 | const int MAXN = 1e6 + 10; 14 | 15 | long long dp[MAXN]; 16 | 17 | int main() { 18 | dp[1] = 1; 19 | dp[2] = 2; 20 | for (int i = 3; i < MAXN; ++i) { 21 | if (i % 2 == 0) { 22 | dp[i] = (dp[i / 2] + dp[i - 2]) % MOD; 23 | } else { 24 | dp[i] = dp[i - 1]; 25 | } 26 | } 27 | int n; 28 | while (scanf("%d", &n) != EOF) { 29 | cout << dp[n] << endl; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Chapter2/2.1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:与7无关的数 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/E9lOOZQ 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | bool Judge(int number) { 14 | bool flag = false; 15 | if (number % 7 == 0) { 16 | flag = true; 17 | } 18 | while (number != 0) { 19 | if (number % 10 == 7) { 20 | flag = true; 21 | } 22 | number /= 10; 23 | } 24 | return flag; 25 | } 26 | 27 | int main() { 28 | int n; 29 | while (scanf("%d", &n) != EOF) { 30 | int sum = 0; 31 | for (int i = 1; i <= n; ++i) { 32 | if (!Judge(i)) { 33 | sum += i * i; 34 | } 35 | } 36 | printf("%d\n", sum); 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Chapter2/2.10.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:路经打印 3 | * 题目来源:上海交通大学复试上机题 4 | * 题目链接:http://t.cn/E9dvHs4 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const string space = " "; 15 | 16 | struct Node { 17 | string data; 18 | Node* child; 19 | Node* brother; 20 | Node() {} 21 | Node(string s, Node* c = nullptr, Node* b = nullptr): data(s), child(c), brother(b) {} 22 | }; 23 | 24 | Node* Insert(Node* root, vector strings, int index) { 25 | if (index >= strings.size()) { 26 | return nullptr; 27 | } 28 | Node* p = root->child; 29 | if (p == nullptr || strings[index] < p->data) { 30 | root->child = new Node(strings[index], nullptr, p); 31 | Insert(root->child, strings, index + 1); 32 | } else { 33 | while (p->brother != nullptr && p->brother->data <= strings[index]) { 34 | p = p->brother; 35 | } 36 | if (p->data == strings[index]) { 37 | Insert(p, strings, index + 1); 38 | } else { 39 | Node* q = new Node(strings[index], nullptr, p->brother); 40 | p->brother = q; 41 | Insert(q, strings, index + 1); 42 | } 43 | } 44 | return root; 45 | } 46 | 47 | void Visit(Node* root, int number) { 48 | if (root == nullptr) { 49 | return; 50 | } 51 | for (int i = 0; i < number; ++i) { 52 | cout << space; 53 | } 54 | cout << root->data << endl; 55 | Visit(root->child, number + 1); 56 | Visit(root->brother, number); 57 | } 58 | 59 | vector Get(string str) { 60 | vector strings; 61 | int start = 0; 62 | for (int i = 0; i < str.size(); ++i) { 63 | if (str[i] == '\\') { 64 | strings.push_back(str.substr(start, i - start)); 65 | start = i + 1; 66 | } else if (i == str.size() - 1) { 67 | strings.push_back(str.substr(start, i - start + 1)); 68 | } 69 | } 70 | return strings; 71 | } 72 | 73 | int main() { 74 | int n; 75 | while (scanf("%d", &n) != EOF) { 76 | if (n == 0) { 77 | break; 78 | } 79 | Node* root = new Node(""); 80 | while (n--) { 81 | string str; 82 | cin >> str; 83 | vector strings = Get(str); 84 | Insert(root, strings, 0); 85 | } 86 | Visit(root->child, 0); 87 | cout << endl; 88 | } 89 | return 0; 90 | } -------------------------------------------------------------------------------- /Chapter2/2.11.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:坠落的蚂蚁 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/E9dhoRA 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | const int MAXN = 100 + 10; 16 | 17 | struct Ant { 18 | int position; 19 | int direction; 20 | }; 21 | 22 | bool Compare(Ant x, Ant y) { 23 | return x.position < y.position; 24 | } 25 | 26 | Ant ants[MAXN]; 27 | 28 | int main() { 29 | int n ; 30 | while(scanf("%d",&n) != EOF) { 31 | vector left; 32 | vector right; 33 | int position; 34 | for (int i = 0; i < n; ++i) { 35 | scanf("%d%d", &ants[i].position, &ants[i].direction); 36 | if (ants[i].direction == 0) { 37 | position = ants[i].position; 38 | } 39 | } 40 | sort(ants, ants + n, Compare); 41 | for (int i = 0; i < n; ++i) { 42 | if (ants[i].position < position && ants[i].direction == 1) { 43 | left.push_back(ants[i].position); 44 | } 45 | if (ants[i].position > position && ants[i].direction == -1) { 46 | right.push_back(ants[i].position); 47 | } 48 | } 49 | if (left.size() == right.size()) { 50 | printf("Cannot fall!\n"); 51 | } else if (left.size() < right.size()) { 52 | printf("%d\n", right[left.size()]); 53 | } else { 54 | printf("%d\n", 100 - left[left.size() - right.size() - 1]); 55 | } 56 | } 57 | return 0 ; 58 | } 59 | -------------------------------------------------------------------------------- /Chapter2/2.2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:百鸡问题 3 | * 题目来源:哈尔滨工业大学复试上机题 4 | * 题目链接:http://t.cn/E9ldhru 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | int main() { 14 | int n; 15 | while (scanf("%d", &n) != EOF) { 16 | for (int x = 0; x <= 100; ++x) { 17 | for (int y = 0; y <= 100 - x; ++y) { 18 | int z = 100 - x - y; 19 | if (15 * x + 9 * y + z <= 3 * n) { 20 | printf("x=%d,y=%d,z=%d\n", x, y, z); 21 | } 22 | } 23 | } 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Chapter2/2.3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:Old Bill 3 | * 题目来源:上海交通大学复试上机题 4 | * 题目链接:http://t.cn/E9jqijR 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | bool Function(int n, int x, int y, int z) { 14 | for (int i = 9; i >= 1; --i) { 15 | for (int j = 9; j >= 0; --j) { 16 | int price = i * 1e4 + x * 1e3 + y * 1e2 + z * 1e1 + j; 17 | if (price % n == 0) { 18 | printf("%d %d %d", i, j, price / n); 19 | return true; 20 | } 21 | } 22 | } 23 | return false; 24 | } 25 | 26 | int main() { 27 | int n; 28 | while (scanf("%d", &n) != EOF) { 29 | int x, y, z; 30 | scanf("%d%d%d", &x, &y, &z); 31 | if (!Function(n, x, y, z)) { 32 | printf("0\n"); 33 | } 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Chapter2/2.4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:Repeater 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/E9jcaVb 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | char pattern[5][5]; 16 | char matrix[3000][3000]; 17 | 18 | void Fill(int x, int y, int n) { 19 | for (int i = 0; i < n; ++i) { 20 | for (int j = 0; j < n; ++j) { 21 | matrix[x + i][y + j] = pattern[i][j]; 22 | } 23 | } 24 | } 25 | 26 | void DFS(int x, int y, int q, int n) { 27 | if (q == 1) { 28 | Fill(x, y, n); 29 | return; 30 | } 31 | int size = pow(n, q - 1); 32 | for (int i = 0; i < n; ++i) { 33 | for (int j = 0; j < n; ++j) { 34 | if(pattern[i][j] != ' ') { 35 | DFS(x + i * size, y + j * size, q - 1, n); 36 | } 37 | } 38 | } 39 | } 40 | 41 | int main() { 42 | int n; 43 | while (scanf("%d", &n) != EOF) { 44 | if (n == 0) { 45 | break; 46 | } 47 | getchar(); 48 | for (int i = 0; i < n; ++i) { 49 | for (int j = 0; j < n; ++j) { 50 | scanf("%c", &pattern[i][j]); 51 | } 52 | getchar(); 53 | } 54 | memset(matrix, 32, sizeof(matrix)); 55 | int q; 56 | scanf("%d", &q); 57 | DFS(0, 0, q, n); 58 | for (int i = 0; i < (int)pow(n, q); ++i) { 59 | for (int j = 0; j < (int)pow(n, q); ++j) { 60 | printf("%c", matrix[i][j]); 61 | } 62 | printf("\n"); 63 | } 64 | } 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Chapter2/2.5.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:Hello World for U 3 | * 题目来源:浙江大学复试上机题 4 | * 题目链接:http://t.cn/E9jizni 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int MAXN = 80 + 10; 15 | 16 | char matrix[MAXN][MAXN]; 17 | 18 | int main() { 19 | string str; 20 | while (cin >> str) { 21 | int row = (str.size() + 2) / 3; 22 | int col = (str.size() + 2) - 2 * row; 23 | memset(matrix, 32, sizeof(matrix)); 24 | int index = 0; 25 | for (int i = 0; i < row; ++i) { 26 | matrix[i][0] = str[index++]; 27 | } 28 | for (int j = 1; j < col - 1; ++j) { 29 | matrix[row - 1][j] = str[index++]; 30 | } 31 | for (int i = row - 1; i >= 0; --i) { 32 | matrix[i][col - 1] = str[index++]; 33 | } 34 | for (int i = 0; i < row; ++i) { 35 | for (int j = 0; j < col; ++j) { 36 | printf("%c", matrix[i][j]); 37 | } 38 | printf("\n"); 39 | } 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Chapter2/2.6.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:日期差值 3 | * 题目来源:上海交通大学复试上机题 4 | * 题目链接:http://t.cn/E9Yz0LE 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int dayTable[2][13]={ 15 | {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 16 | {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} 17 | }; 18 | 19 | bool IsLeapYear(int year) { 20 | return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 21 | } 22 | 23 | int Number(int year, int month, int day) { 24 | int number = 0; 25 | for (int i = 1; i < year; ++i) { 26 | if (IsLeapYear(i)) { 27 | number += 366; 28 | } else { 29 | number += 365; 30 | } 31 | } 32 | int row = IsLeapYear(year); 33 | for (int j = 0; j < month; ++j){ 34 | number += dayTable[row][j]; 35 | } 36 | number += day; 37 | return number; 38 | } 39 | 40 | int main() { 41 | int year, month, day; 42 | while (scanf("%04d%02d%02d", &year, &month, &day) != EOF) { 43 | int number1 = Number(year, month, day); 44 | scanf("%4d%2d%2d", &year, &month, &day); 45 | int number2 = Number(year, month, day); 46 | printf("%d\n", abs(number1 - number2) + 1); 47 | } 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Chapter2/2.7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:Day of Week 3 | * 题目来源:上海交通大学复试上机题 4 | * 题目链接:http://t.cn/E9YZLbi 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int dayTable[2][13]={ 15 | {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 16 | {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} 17 | }; 18 | 19 | string monthName[13] = { 20 | "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" 21 | }; 22 | 23 | string weekName[7] = { 24 | "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" 25 | }; 26 | 27 | bool IsLeapYear(int year) { 28 | return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 29 | } 30 | 31 | int Number(int year, int month, int day) { 32 | int number = 0; 33 | for (int i = 1; i < year; ++i) { 34 | if (IsLeapYear(i)) { 35 | number += 366; 36 | } else { 37 | number += 365; 38 | } 39 | } 40 | int row = IsLeapYear(year); 41 | for (int j = 0; j < month; ++j){ 42 | number += dayTable[row][j]; 43 | } 44 | number += day; 45 | return number; 46 | } 47 | 48 | int main() { 49 | int year, month, day; 50 | string monthInput; 51 | while (cin >> day >> monthInput >> year) { 52 | for (int i = 1; i <= 12; ++i) { 53 | if (monthInput == monthName[i]) { 54 | month = i; 55 | break; 56 | } 57 | } 58 | int number = Number(year, month, day); 59 | cout << weekName[number % 7] << endl; 60 | } 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Chapter2/2.8.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:日期类 3 | * 题目来源:北京理工大学复试上机题 4 | * 题目链接:http://t.cn/E9RJUp4 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | const int dayTable[2][13]={ 14 | {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 15 | {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} 16 | }; 17 | 18 | bool IsLeapYear(int year) { 19 | return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 20 | } 21 | 22 | int main() { 23 | int caseNumber; 24 | scanf("%d", &caseNumber); 25 | while (caseNumber--) { 26 | int year, month, day; 27 | scanf("%d%d%d", &year, &month, &day); 28 | int row = IsLeapYear(year); 29 | day += 1; 30 | if (day > dayTable[row][month]) { 31 | month++; 32 | day = 1; 33 | } 34 | if (month > 12) { 35 | year++; 36 | month = 1; 37 | } 38 | printf("%04d-%02d-%02d\n", year, month, day); 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Chapter2/2.9.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:Grading 3 | * 题目来源:浙江大学复试上机题 4 | * 题目链接:http://t.cn/E9rDPSq 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | int main() { 15 | int perfect, tolerance, grade1, grade2, grade3, gradeJudge; 16 | while (cin >> perfect >> tolerance >> grade1 >> grade2 >> grade3 >> gradeJudge) { 17 | double answer; 18 | if (abs(grade1 - grade2) <= tolerance) { 19 | answer = (grade1 + grade2) / 2.0; 20 | } else if (abs(grade1 - grade3) <= tolerance && abs(grade2 - grade3) > tolerance) { 21 | answer = (grade1 + grade3) / 2.0; 22 | } else if (abs(grade2 - grade3) <= tolerance && abs(grade1 - grade3) > tolerance) { 23 | answer = (grade2 + grade3) / 2.0; 24 | } else if (abs(grade1 - grade3) <= tolerance && abs(grade2 - grade3) <= tolerance) { 25 | answer = max(max(grade1, grade2), grade3); 26 | } else { 27 | answer = gradeJudge; 28 | } 29 | printf("%.1f\n", answer); 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Chapter3/3.1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:特殊排序 3 | * 题目来源:华中科技大学复试上机题 4 | * 题目链接:http://t.cn/E9gio39 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int MAXN = 1000 + 10; 15 | int arr[MAXN]; 16 | 17 | int main() { 18 | int n; 19 | while (scanf("%d", &n) != EOF) { 20 | for (int i = 0; i < n; ++i) { 21 | scanf("%d", &arr[i]); 22 | } 23 | sort(arr, arr + n); 24 | printf("%d\n", arr[n - 1]); 25 | for (int i = 0; i < n - 1; ++i) { 26 | printf("%d ", arr[i]); 27 | } 28 | if (n == 1) { 29 | printf("-1"); 30 | } 31 | printf("\n"); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Chapter3/3.2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:整数奇偶排序 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/E9glPvp 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | int arr[10]; 15 | 16 | bool Compare(int x, int y) { 17 | if (x % 2 == 1 && y % 2 == 1) { 18 | return y < x; 19 | } else if (x % 2 == 0 && y % 2 == 0) { 20 | return x < y; 21 | } else { 22 | return x % 2 > y % 2; 23 | } 24 | } 25 | 26 | int main() { 27 | while (scanf("%d", &arr[0]) != EOF) { 28 | for (int i = 1; i < 10; ++i) { 29 | scanf("%d", &arr[i]); 30 | } 31 | sort(arr, arr + 10, Compare); 32 | for (int i = 0; i < 10; ++i) { 33 | printf("%d ", arr[i]); 34 | } 35 | printf("\n"); 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Chapter3/3.3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:小白鼠排队 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/E9gXh9Z 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int MAXN = 100 + 10; 15 | 16 | struct Mouse { 17 | int weight; 18 | string color; 19 | }; 20 | 21 | Mouse arr[MAXN]; 22 | 23 | bool Compare(Mouse x, Mouse y) { 24 | return x.weight > y.weight; 25 | } 26 | 27 | int main() { 28 | int n; 29 | while (scanf("%d", &n) != EOF) { 30 | for (int i = 0; i < n; ++i) { 31 | cin >> arr[i].weight >> arr[i].color; 32 | } 33 | sort(arr, arr + n, Compare); 34 | for (int i = 0; i < n; ++i) { 35 | cout << arr[i].color << endl; 36 | } 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Chapter3/3.4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:奥运排序问题 3 | * 题目来源:浙江大学复试上机题 4 | * 题目链接:http://t.cn/E9gYpyl 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | const int MAXN = 200 + 10; 16 | const double INF = INT_MAX; 17 | 18 | struct Country { 19 | int id; 20 | double gold, medal, goldRate, medalRate; 21 | int bestMethod; 22 | int bestOrder; 23 | int order[4]; 24 | Country() {} 25 | Country(int id, int gold, int medal, int people) { 26 | bestMethod = MAXN; 27 | bestOrder = MAXN; 28 | this->id = id; 29 | this->gold = gold; //金牌总数 30 | this->medal = medal; //奖牌总数 31 | if (people == 0 && gold != 0) { //金牌比例 32 | goldRate = INF; 33 | } else { 34 | goldRate = gold / (double)people; 35 | } 36 | if (people == 0 && medal != 0) { //奖牌比例 37 | medalRate = INF; 38 | } else { 39 | medalRate = medal / (double)people; 40 | } 41 | } 42 | }; 43 | 44 | Country arr[MAXN]; 45 | 46 | bool CompareID(Country c1, Country c2){ 47 | return c1.id < c2.id; 48 | } 49 | 50 | bool CompareGold(Country c1, Country c2){ 51 | return c1.gold > c2.gold; 52 | } 53 | 54 | bool CompareMedal(Country c1, Country c2){ 55 | return c1.medal > c2.medal; 56 | } 57 | 58 | bool CompareGoldRate(Country c1, Country c2){ 59 | return c1.goldRate > c2.goldRate; 60 | } 61 | 62 | bool CompareMedalRate(Country c1, Country c2){ 63 | return c1.medalRate > c2.medalRate; 64 | } 65 | 66 | void Solve(int n) { 67 | sort(arr, arr + n, CompareGold); 68 | for (int i = 0; i < n; ++i) { 69 | arr[i].order[0] = i + 1; 70 | } 71 | for (int i = 1; i < n; ++i) { 72 | if (arr[i].gold == arr[i - 1].gold) { 73 | arr[i].order[0] = arr[i - 1].order[0]; 74 | } 75 | } 76 | 77 | sort(arr, arr + n, CompareMedal); 78 | for (int i = 0; i < n; ++i) { 79 | arr[i].order[1] = i + 1; 80 | } 81 | for (int i = 1; i < n; ++i) { 82 | if (arr[i].medal == arr[i - 1].medal) { 83 | arr[i].order[1] = arr[i - 1].order[1]; 84 | } 85 | } 86 | 87 | sort(arr, arr + n, CompareGoldRate); 88 | for (int i = 0; i < n; ++i) { 89 | arr[i].order[2] = i + 1; 90 | } 91 | for (int i = 1; i < n; ++i) { 92 | if (arr[i].goldRate == arr[i - 1].goldRate) { 93 | arr[i].order[2] = arr[i - 1].order[2]; 94 | } 95 | } 96 | 97 | sort(arr, arr + n, CompareMedalRate); 98 | for (int i = 0; i < n; ++i) { 99 | arr[i].order[3] = i + 1; 100 | } 101 | for (int i = 1; i < n; ++i) { 102 | if (arr[i].medalRate == arr[i - 1].medalRate) { 103 | arr[i].order[3] = arr[i - 1].order[3]; 104 | } 105 | } 106 | 107 | sort(arr, arr + n, CompareID); 108 | for (int i = 0; i < n; ++i) { 109 | for (int j = 0; j < 4; ++j) { 110 | if (arr[i].bestOrder > arr[i].order[j]) { 111 | arr[i].bestOrder = arr[i].order[j] ; 112 | arr[i].bestMethod = j + 1; 113 | } 114 | } 115 | } 116 | } 117 | 118 | int main() { 119 | int n, m; 120 | while (scanf("%d%d", &n, &m) != EOF) { 121 | for (int i = 0; i < n; ++i) { 122 | int gold, medal, people; 123 | scanf("%d%d%d", &gold, &medal, &people); 124 | arr[i] = Country(i, gold, medal, people); 125 | } 126 | Solve(n); 127 | for (int i = 0; i < m; ++i) { 128 | int index; 129 | scanf("%d", &index); 130 | printf("%d:%d\n", arr[index].bestOrder, arr[index].bestMethod); 131 | } 132 | printf("\n"); 133 | } 134 | return 0; 135 | } 136 | -------------------------------------------------------------------------------- /Chapter3/3.5.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:找最小数 3 | * 题目来源:北京邮电大学复试上机题 4 | * 题目链接:http://t.cn/E9gekWa 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int INF = INT_MAX; 15 | 16 | struct point { 17 | int x, y; 18 | }; 19 | 20 | int main() { 21 | int n; 22 | while (scanf("%d", &n) != EOF) { 23 | point mininum; 24 | mininum.x = INF; 25 | mininum.y = INF; 26 | for (int i = 0; i < n; ++i) { 27 | point current; 28 | scanf("%d%d", ¤t.x, ¤t.y); 29 | if (current.x< mininum.x || (current.x == mininum.x && current.y < mininum.y)) { 30 | mininum = current; 31 | } 32 | } 33 | printf("%d %d\n", mininum.x, mininum.y); 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Chapter3/3.6.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:打印极值点下标 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/E9gekWa 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | const int MAXN = 80 + 10; 14 | 15 | int arr[MAXN]; 16 | 17 | int main() { 18 | int n; 19 | while (scanf("%d", &n) != EOF) { 20 | for (int i = 0; i < n; ++i) { 21 | scanf("%d", &arr[i]); 22 | } 23 | if (arr[0] != arr[1]) { 24 | printf("%d ", 0); 25 | } 26 | for (int i = 1; i < n - 1; ++i) { 27 | if ((arr[i] < arr[i - 1] && arr[i] < arr[i + 1]) || (arr[i] > arr[i - 1] && arr[i] > arr[i + 1])){ 28 | printf("%d ", i); 29 | } 30 | } 31 | if (arr[n - 2] != arr[n -1]) { 32 | printf("%d ", n - 1); 33 | } 34 | printf("\n"); 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Chapter3/3.7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:找位置 3 | * 题目来源:华中科技大学复试上机题 4 | * 题目链接:http://t.cn/E9eh4jY 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | vector alpha; 17 | map> myMap; 18 | 19 | int main() { 20 | string str; 21 | while (cin >> str) { 22 | alpha.clear(); 23 | myMap.clear(); 24 | for (int i = 0; i < str.size(); ++i) { 25 | if (myMap.count(str[i]) == 0) { 26 | alpha.push_back(str[i]); 27 | myMap[str[i]] = vector(1, i); 28 | } else { 29 | myMap[str[i]].push_back(i); 30 | } 31 | } 32 | for (int i = 0; i < alpha.size(); ++i) { 33 | if (myMap[alpha[i]].size() == 1) { 34 | continue; 35 | } 36 | for (int j = 0; j < myMap[alpha[i]].size(); ++j) { 37 | if (j == myMap[alpha[i]].size() - 1) { 38 | printf("%c:%d\n", alpha[i], myMap[alpha[i]][j]); 39 | } else { 40 | printf("%c:%d,", alpha[i], myMap[alpha[i]][j]); 41 | } 42 | } 43 | } 44 | } 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Chapter4/4.1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:skew数 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/Ai8IALKI 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | int main() { 14 | string str; 15 | while (cin >> str) { 16 | int sum = 0; 17 | for (int i = 0; i < str.size(); ++i) { 18 | sum += (str[i] - '0') * ((1 << (str.size() - i)) - 1); 19 | } 20 | printf("%d\n", sum); 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Chapter4/4.2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:单词替换 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/Ai8Iycp6 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | int main() { 16 | string line; 17 | while (getline(cin, line)) { 18 | string original, replace; 19 | cin >> original >> replace; 20 | stringstream stringStream(line); 21 | string str; 22 | while (stringStream >> str) { 23 | if (str == original) { 24 | cout << replace << " "; 25 | } else { 26 | cout << str << " "; 27 | } 28 | } 29 | cout << endl; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Chapter4/4.3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:首字母大写 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/Ai8I2hco 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | int main() { 15 | string str; 16 | while (getline(cin, str)) { 17 | for(int i = 0; i < str.size(); ++i) { 18 | if (i == 0 || str[i - 1] == '\n' || str[i - 1] == '\r' || str[i - 1] == '\t' || str[i - 1] == ' ') { 19 | str[i] = toupper(str[i]); 20 | } 21 | } 22 | cout << str << endl; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Chapter4/4.4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:浮点数加法 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/Ai8I4v0j 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | string Add(string str1, string str2) { 15 | string fractional1 = str1.substr(str1.find('.') + 1); 16 | string fractional2 = str2.substr(str2.find('.') + 1); 17 | if (fractional1.size() < fractional2.size()) { //小数对齐 18 | fractional1 = fractional1 + string(fractional2.size() - fractional1.size(), '0'); 19 | } else { 20 | fractional2 = fractional2 + string(fractional1.size() - fractional2.size(), '0'); 21 | } 22 | 23 | string integral1 = str1.substr(0, str1.find('.')); 24 | string integral2 = str2.substr(0, str2.find('.')); 25 | if (integral1.size() < integral2.size()) { //整数对齐 26 | integral1 = string(integral2.size() - integral1.size(), '0') + integral1; 27 | } else { 28 | integral2 = string(integral1.size() - integral2.size(), '0') + integral2; 29 | } 30 | 31 | int carry = 0; 32 | string fractional(fractional1.size(), ' '); 33 | for (int i = fractional.size() - 1; i >= 0; --i) { //小数相加 34 | int current = fractional1[i] - '0' + fractional2[i] - '0' + carry; 35 | fractional[i] = current % 10 + '0'; 36 | carry = current / 10; 37 | } 38 | 39 | string integral(integral1.size(), ' '); 40 | for (int i = integral.size() - 1; i >= 0; --i) { //整数相加 41 | int current = integral1[i] - '0' + integral2[i] - '0' + carry; 42 | integral[i] = current % 10 + '0'; 43 | carry = current / 10; 44 | } 45 | if (carry != 0) { 46 | integral = to_string(carry) + integral; 47 | } 48 | 49 | return integral + '.' + fractional; 50 | } 51 | 52 | int main() { 53 | string str1, str2; 54 | while (cin >> str1 >> str2) { 55 | cout << Add(str1, str2) << endl; 56 | } 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Chapter4/4.5.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:后缀子串排序 3 | * 题目来源:上海交通大学复试上机题 4 | * 题目链接:http://dwz.win/GKF 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | int main(){ 16 | string str; 17 | cin >> str; 18 | string array[str.size()]; 19 | for (int i = 0; i < str.size(); ++i) { 20 | array[i] = str.substr(i); 21 | } 22 | sort(array, array + str.size()); 23 | for (int i = 0; i < str.size(); ++i) { 24 | cout << array[i] << endl; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Chapter4/4.6.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:字符串匹配 3 | * 题目来源:北京航天航空大学复试上机题 4 | * 题目链接:http://dwz.win/GKJ 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int MAXN = 1e3 + 10; 15 | 16 | string texts[MAXN]; 17 | string pattern; 18 | 19 | bool Equal(char x, char y) { 20 | if(isalpha(x)&&isalpha(y)) { 21 | return toupper(x) == toupper(y); 22 | } else { 23 | return x == y; 24 | } 25 | } 26 | 27 | bool Match(string text, string pattern) { 28 | for (int i = 0, j = 0; i < text.size() && j < pattern.size(); ++i, ++j) { 29 | bool flag = false; 30 | if (pattern[j] == '[') { 31 | for (; pattern[j] != ']'; ++j) { 32 | if (Equal(text[i], pattern[j])) { 33 | flag = true; 34 | } 35 | } 36 | } else if (Equal(text[i], pattern[j])) { 37 | flag = true; 38 | } 39 | if (!flag) { 40 | return false; 41 | } 42 | } 43 | return true; 44 | } 45 | 46 | int main() { 47 | int n; 48 | while (scanf("%d", &n) != EOF) { 49 | for (int i = 0; i < n; ++i) { 50 | cin >> texts[i]; 51 | } 52 | cin >> pattern; 53 | for (int i = 0; i < n; ++i) { 54 | if (Match(texts[i], pattern)) { 55 | cout << i + 1 << " " << texts[i] << endl; 56 | } 57 | } 58 | } 59 | return 0; 60 | } -------------------------------------------------------------------------------- /Chapter4/4.7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:String Matching 3 | * 题目来源:上海交通大学复试上机题 4 | * 题目链接:http://dwz.win/GMr 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int MAXM = 1e6 + 10; 15 | 16 | int nextTable[MAXM]; 17 | 18 | void GetNextTable(string pattern) { //创建next表 19 | int m = pattern.size(); 20 | int j = 0; 21 | nextTable[j] = -1; 22 | int t = nextTable[j]; 23 | while (j < m) { 24 | if (t == -1 || pattern[j] == pattern[t]) { 25 | j++; 26 | t++; 27 | nextTable[j] = t; 28 | } else { 29 | t = nextTable[t]; 30 | } 31 | } 32 | return ; 33 | } 34 | 35 | int KMP(string text, string pattern) { 36 | GetNextTable(pattern); 37 | int n = text.size(); 38 | int m = pattern.size(); 39 | int i = 0; 40 | int j = 0; 41 | int number = 0; //记录匹配次数 42 | while (i < n) { 43 | if (j == -1 || text[i] == pattern[j]) { //当前字符匹配成功 44 | i++; 45 | j++; 46 | } else { 47 | j = nextTable[j]; //当前字符匹配失败 48 | } 49 | if (j == m) { //模式串匹配成功 50 | number++; 51 | j = nextTable[j]; 52 | } 53 | } 54 | return number; 55 | } 56 | 57 | int main() { 58 | string text, pattern; 59 | while (cin >> text >> pattern) { 60 | printf("%d\n", KMP(text, pattern)); 61 | } 62 | return 0; 63 | } -------------------------------------------------------------------------------- /Chapter5/5.1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:堆栈的使用 3 | * 题目来源:吉林大学复试上机题 4 | * 题目链接:http://t.cn/AiKKM6F6 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | int main() { 15 | int n; 16 | while (scanf("%d", &n) != EOF) { 17 | if (n == 0) { 18 | break; 19 | } 20 | stack Stack; 21 | for (int i = 0; i < n; ++i) { 22 | char c; 23 | cin >> c; 24 | if (c == 'A') { 25 | if (Stack.empty()) { 26 | printf("E\n"); 27 | } else { 28 | printf("%d\n", Stack.top()); 29 | } 30 | } else if (c == 'O') { 31 | if (Stack.empty()) { 32 | continue; 33 | } else { 34 | Stack.pop(); 35 | } 36 | } else if (c == 'P') { 37 | int data; 38 | cin >> data; 39 | Stack.push(data); 40 | } 41 | } 42 | cout << endl; 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Chapter5/5.2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:计算表达式 3 | * 题目来源:上海交通大学复试上机题 4 | * 题目链接:http://t.cn/AiKKJjJ5 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | int Priority(char c) { //优先级顺序 #,$,+-,*/ 17 | if (c == '#') { 18 | return 0; 19 | } else if (c == '$') { 20 | return 1; 21 | } else if (c == '+' || c == '-') { 22 | return 2; 23 | } else { 24 | return 3; 25 | } 26 | } 27 | 28 | double GetNumber(string str, int& index) { //获得下个数字 29 | double number = 0; 30 | while (isdigit(str[index])) { 31 | number = number * 10 + str[index] - '0'; 32 | index++; 33 | } 34 | return number; 35 | } 36 | 37 | double Calculate(double x, double y, char op) { 38 | double result = 0; 39 | if (op == '+') { 40 | result = x + y; 41 | } else if (op == '-') { 42 | result = x - y; 43 | } else if (op == '*') { 44 | result = x * y; 45 | } else if (op == '/') { 46 | result = x / y; 47 | } 48 | return result; 49 | } 50 | 51 | int main() { 52 | string str; 53 | while (getline(cin, str)) { 54 | if (str == "0") { 55 | break; 56 | } 57 | stack oper; //运算符栈 58 | stack data; //运算数栈 59 | str += '$'; //字符串尾部添加$ 60 | oper.push('#'); //运算符栈底添加# 61 | int index = 0; //字符串下标 62 | while (index < str.size()) { 63 | if (str[index] == ' ') { 64 | index++; 65 | } else if (isdigit(str[index])) { 66 | data.push(GetNumber(str, index)); 67 | } else { 68 | if (Priority(oper.top()) < Priority(str[index])) { 69 | oper.push(str[index]); 70 | index++; 71 | } else { 72 | double y = data.top(); 73 | data.pop(); 74 | double x = data.top(); 75 | data.pop(); 76 | data.push(Calculate(x, y, oper.top())); 77 | oper.pop(); 78 | } 79 | } 80 | } 81 | printf("%g\n", data.top()); 82 | } 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /Chapter6/6.1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:八进制 3 | * 题目来源:华中科技大学复试上机题 4 | * 题目链接:http://t.cn/AiCu0lHe 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | void Convert(int n, int x) { //10进制转换成为x进制 15 | vector answer; 16 | if (n == 0) { 17 | answer.push_back(0); 18 | } else { 19 | while (n != 0) { 20 | answer.push_back(n % x); 21 | n /= x; 22 | } 23 | } 24 | for (int i = answer.size() - 1; i >= 0; --i) { 25 | printf("%d", answer[i]); 26 | } 27 | printf("\n"); 28 | } 29 | 30 | int main() { 31 | int n; 32 | while (scanf("%d", &n) != EOF) { 33 | Convert(n, 8); 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Chapter6/6.10.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:A+B for Matrices 3 | * 题目来源:浙江大学复试上机题 4 | * 题目链接:http://t.cn/Aipb7GBG 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | const int MAXN = 20; 14 | 15 | struct Matrix { 16 | int matrix[MAXN][MAXN]; 17 | int row, col; //行与列 18 | Matrix(int r, int c): row(r), col(c) {} //构造函数 19 | }; 20 | 21 | Matrix Add(Matrix x, Matrix y) { //矩阵加法 22 | Matrix answer(x.row, x.col); 23 | for (int i = 0; i < answer.row; ++i) { 24 | for (int j = 0; j < answer.col; ++j) { 25 | answer.matrix[i][j] = x.matrix[i][j] + y.matrix[i][j]; 26 | } 27 | } 28 | return answer; 29 | } 30 | 31 | void InputMatrix(Matrix &x) { //矩阵输入 32 | for (int i = 0; i < x.row; ++i) { 33 | for (int j = 0; j < x.col; ++j) { 34 | scanf("%d", &x.matrix[i][j]); 35 | } 36 | } 37 | } 38 | 39 | int Count(Matrix x) { 40 | int number = 0; 41 | for (int i = 0; i < x.row; ++i) { 42 | bool flag = true; 43 | for (int j = 0; j < x.col; ++j) { 44 | if (x.matrix[i][j] != 0) { 45 | flag = false; 46 | break; 47 | } 48 | } 49 | if (flag) { 50 | number++; 51 | } 52 | } 53 | for (int j = 0; j < x.col; ++j) { 54 | bool flag = true; 55 | for (int i = 0; i < x.row; ++i) { 56 | if (x.matrix[i][j] != 0) { 57 | flag = false; 58 | break; 59 | } 60 | } 61 | if (flag) { 62 | number++; 63 | } 64 | } 65 | return number; 66 | } 67 | 68 | int main() { 69 | int m, n; 70 | while (scanf("%d", &m) != EOF) { 71 | if (m == 0) { 72 | break; 73 | } 74 | scanf("%d", &n); 75 | Matrix a(m, n); 76 | Matrix b(m, n); 77 | InputMatrix(a); 78 | InputMatrix(b); 79 | Matrix sum = Add(a, b); 80 | printf("%d\n", Count(sum)); 81 | } 82 | return 0; 83 | } -------------------------------------------------------------------------------- /Chapter6/6.11.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:递推数列 3 | * 题目来源:清华大学复试上机题 4 | * 题目链接:http://t.cn/AipbZ2sS 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | const int MAXN = 10; 14 | 15 | struct Matrix { 16 | int matrix[MAXN][MAXN]; 17 | int row, col; //行与列 18 | Matrix(int r, int c): row(r), col(c) {} //构造函数 19 | }; 20 | 21 | Matrix Multiply(Matrix x, Matrix y, int mod) { //矩阵乘法 22 | Matrix answer(x.row, y.col); 23 | for (int i = 0; i < answer.row; ++i) { 24 | for (int j = 0; j < answer.col; ++j) { 25 | answer.matrix[i][j] = 0; 26 | for (int k = 0; k < x.col; ++k) { 27 | answer.matrix[i][j] += (x.matrix[i][k] * y.matrix[k][j]) % mod; 28 | answer.matrix[i][j] %= mod; 29 | } 30 | } 31 | } 32 | return answer; 33 | } 34 | 35 | Matrix QuickPower(Matrix x, int k, int mod) { //矩阵快速幂 36 | Matrix answer(x.row, x.col); 37 | for (int i = 0; i < answer.row; ++i) { 38 | for (int j = 0; j < answer.col; ++j) { 39 | if (i == j) { 40 | answer.matrix[i][j] = 1; 41 | } else { 42 | answer.matrix[i][j] = 0; 43 | } 44 | } 45 | } 46 | while (k != 0) { 47 | if (k % 2 == 1) { 48 | answer = Multiply(answer, x, mod); 49 | } 50 | k /= 2; 51 | x = Multiply(x, x, mod); 52 | } 53 | return answer; 54 | } 55 | int main() { 56 | int a0, a1, p, q, k; 57 | scanf("%d%d%d%d%d", &a0, &a1, &p, &q, &k); 58 | Matrix m = Matrix(2, 2); 59 | m.matrix[0][0] = p * a1 + q * a0; 60 | m.matrix[0][1] = a1; 61 | m.matrix[1][0] = a1; 62 | m.matrix[1][1] = a0; 63 | 64 | Matrix x = Matrix(2, 2); 65 | x.matrix[0][0] = p; 66 | x.matrix[0][1] = q; 67 | x.matrix[1][0] = 1; 68 | x.matrix[1][1] = 0; 69 | 70 | Matrix answer = Multiply(QuickPower(x, k - 1, 1e4), m, 1e4); 71 | printf("%d\n", answer.matrix[0][1]); 72 | return 0; 73 | } -------------------------------------------------------------------------------- /Chapter6/6.12.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:数字阶梯求和 3 | * 题目来源:哈尔滨工业大学复试上机题 4 | * 题目链接:http://t.cn/Aipak8BQ 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | const int MAXN = 1e4; 14 | int arr[MAXN]; 15 | 16 | int main() { 17 | int a, n; 18 | while (scanf("%d%d", &a, &n) != EOF) { 19 | int carry = 0; 20 | for(int i = n; i >= 1; --i) { 21 | arr[i] = ((i * a) + carry) % 10; 22 | carry = ((i * a) + carry) / 10; 23 | } 24 | if (carry != 0) { 25 | printf("%d", carry); 26 | } 27 | for (int i = 1; i <= n; ++i) { 28 | printf("%d", arr[i]); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Chapter6/6.13.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:大整数的因子 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/AipaFCJE 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | const int MAXN = 10000; 17 | 18 | struct BigInteger { 19 | int digit[MAXN]; 20 | int length; 21 | BigInteger(); 22 | BigInteger(int x); 23 | BigInteger(string str); 24 | BigInteger(const BigInteger& b); 25 | BigInteger operator=(int x); 26 | BigInteger operator=(string str); 27 | BigInteger operator=(const BigInteger& b); 28 | bool operator<(const BigInteger& b); 29 | bool operator<=(const BigInteger& b); 30 | bool operator==(const BigInteger& b); 31 | BigInteger operator+(const BigInteger& b); 32 | BigInteger operator-(const BigInteger& b); 33 | BigInteger operator*(const BigInteger& b); 34 | BigInteger operator/(const BigInteger& b); 35 | BigInteger operator%(const BigInteger& b); 36 | friend istream& operator>>(istream& in, BigInteger& b); 37 | friend ostream& operator<<(ostream& out, const BigInteger& b); 38 | }; 39 | 40 | istream& operator>>(istream& in, BigInteger& b) { 41 | string str; 42 | in >> str; 43 | b = str; 44 | return in; 45 | } 46 | 47 | ostream& operator<<(ostream& out, const BigInteger& b) { 48 | for (int i = b.length - 1; i >= 0; --i) { 49 | out << b.digit[i]; 50 | } 51 | return out; 52 | } 53 | 54 | BigInteger::BigInteger() { 55 | memset(digit, 0, sizeof(digit)); 56 | length = 0; 57 | } 58 | 59 | BigInteger::BigInteger(int x) { 60 | memset(digit, 0, sizeof(digit)); 61 | length = 0; 62 | if (x == 0) { 63 | digit[length++] = x; 64 | } 65 | while (x != 0) { 66 | digit[length++] = x % 10; 67 | x /= 10; 68 | } 69 | } 70 | 71 | BigInteger::BigInteger(string str) { 72 | memset(digit, 0, sizeof(digit)); 73 | length = str.size(); 74 | for (int i = 0; i < length; ++i) { 75 | digit[i] = str[length - i - 1] - '0'; 76 | } 77 | } 78 | 79 | BigInteger::BigInteger(const BigInteger& b) { 80 | memset(digit, 0, sizeof(digit)); 81 | length = b.length; 82 | for (int i = 0; i < length; ++i) { 83 | digit[i] = b.digit[i]; 84 | } 85 | } 86 | 87 | BigInteger BigInteger::operator=(int x) { 88 | memset(digit, 0, sizeof(digit)); 89 | length = 0; 90 | if (x == 0) { 91 | digit[length++] = x; 92 | } 93 | while (x != 0) { 94 | digit[length++] = x % 10; 95 | x /= 10; 96 | } 97 | return *this; 98 | } 99 | 100 | BigInteger BigInteger::operator=(string str) { 101 | memset(digit, 0, sizeof(digit)); 102 | length = str.size(); 103 | for (int i = 0; i < length; ++i) { 104 | digit[i] = str[length - i - 1] - '0'; 105 | } 106 | return *this; 107 | } 108 | 109 | BigInteger BigInteger::operator=(const BigInteger& b) { 110 | memset(digit, 0, sizeof(digit)); 111 | length = b.length; 112 | for (int i = 0; i < length; ++i) { 113 | digit[i] = b.digit[i]; 114 | } 115 | return *this; 116 | } 117 | 118 | bool BigInteger::operator<(const BigInteger& b) { 119 | if (length < b.length) { 120 | return true; 121 | } else if (b.length < length) { 122 | return false; 123 | } else { 124 | for (int i = length - 1; i >= 0; --i) { 125 | if (digit[i] == b.digit[i]) { 126 | continue; 127 | } else { 128 | return digit[i] < b.digit[i]; 129 | } 130 | } 131 | } 132 | return false; 133 | } 134 | 135 | bool BigInteger::operator<=(const BigInteger& b) { 136 | if (length < b.length) { 137 | return true; 138 | } else if (b.length < length) { 139 | return false; 140 | } else { 141 | for (int i = length - 1; i >= 0; --i) { 142 | if (digit[i] == b.digit[i]) { 143 | continue; 144 | } else { 145 | return digit[i] < b.digit[i]; 146 | } 147 | } 148 | } 149 | return true; 150 | } 151 | 152 | bool BigInteger::operator==(const BigInteger& b) { 153 | if (length != b.length) { 154 | return false; 155 | } else { 156 | for (int i = length - 1; i >= 0; --i) { 157 | if (digit[i] != b.digit[i]) { 158 | return false; 159 | } 160 | } 161 | } 162 | return true; 163 | } 164 | 165 | BigInteger BigInteger::operator+(const BigInteger& b) { 166 | BigInteger answer; 167 | int carry = 0; 168 | for (int i = 0; i < length || i < b.length; ++i) { 169 | int current = carry + digit[i] + b.digit[i]; 170 | carry = current / 10; 171 | answer.digit[answer.length++] = current % 10; 172 | } 173 | if (carry != 0) { 174 | answer.digit[answer.length++] = carry; 175 | } 176 | return answer; 177 | } 178 | 179 | BigInteger BigInteger::operator-(const BigInteger& b) { 180 | BigInteger answer; 181 | int carry = 0; 182 | for (int i = 0; i < length; ++i) { 183 | int current = digit[i] - b.digit[i] - carry; 184 | if (current < 0) { 185 | current += 10; 186 | carry = 1; 187 | } else { 188 | carry = 0; 189 | } 190 | answer.digit[answer.length++] = current; 191 | } 192 | while (answer.digit[answer.length - 1] == 0 && answer.length > 1) { 193 | answer.length--; 194 | } 195 | return answer; 196 | 197 | } 198 | 199 | BigInteger BigInteger::operator*(const BigInteger& b) { 200 | BigInteger answer; 201 | answer.length = length + b.length; 202 | for (int i = 0; i < length; ++i) { 203 | for (int j = 0; j < b.length; ++j) { 204 | answer.digit[i + j] += digit[i] * b.digit[j]; 205 | } 206 | } 207 | for (int i = 0; i < answer.length; ++i) { 208 | answer.digit[i + 1] += answer.digit[i] / 10; 209 | answer.digit[i] %= 10; 210 | } 211 | while (answer.digit[answer.length - 1] == 0 && answer.length > 1) { 212 | answer.length--; 213 | } 214 | return answer; 215 | } 216 | 217 | BigInteger BigInteger::operator/(const BigInteger& b) { 218 | BigInteger answer; 219 | answer.length = length; 220 | BigInteger remainder = 0; 221 | BigInteger temp = b; 222 | for (int i = length - 1; i >= 0; --i) { 223 | if (!(remainder.length == 1 && remainder.digit[0] == 0)) { 224 | for (int j = remainder.length - 1; j >= 0; --j) { 225 | remainder.digit[j + 1] = remainder.digit[j]; 226 | } 227 | remainder.length++; 228 | } 229 | remainder.digit[0] = digit[i]; 230 | while (temp <= remainder) { 231 | remainder = remainder - temp; 232 | answer.digit[i]++; 233 | } 234 | } 235 | while (answer.digit[answer.length - 1] == 0 && answer.length > 1) { 236 | answer.length--; 237 | } 238 | return answer; 239 | } 240 | 241 | BigInteger BigInteger::operator%(const BigInteger& b) { 242 | BigInteger remainder = 0; 243 | BigInteger temp = b; 244 | for (int i = length - 1; i >= 0; --i) { 245 | if (!(remainder.length == 1 && remainder.digit[0] == 0)) { 246 | for (int j = remainder.length - 1; j >= 0; --j) { 247 | remainder.digit[j + 1] = remainder.digit[j]; 248 | } 249 | remainder.length++; 250 | } 251 | remainder.digit[0] = digit[i]; 252 | while (temp <= remainder) { 253 | remainder = remainder - temp; 254 | } 255 | } 256 | return remainder; 257 | } 258 | 259 | int main() { 260 | string str; 261 | while (cin >> str) { 262 | if (str == "-1") { 263 | break; 264 | } 265 | BigInteger c = str; 266 | vector answer; 267 | for (int i = 2; i <= 9; ++i) { 268 | if (c % BigInteger(i) == 0) { 269 | answer.push_back(i); 270 | } 271 | } 272 | for (int i = 0; i < answer.size(); ++i) { 273 | if (i == answer.size() - 1) { 274 | printf("%d\n", answer[i]); 275 | } else { 276 | printf("%d ", answer[i]); 277 | } 278 | } 279 | if (answer.size() == 0) { 280 | printf("none\n"); 281 | } 282 | } 283 | return 0; 284 | } -------------------------------------------------------------------------------- /Chapter6/6.2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:又一版 A+B 3 | * 题目来源:浙江大学复试上机题 4 | * 题目链接:http://t.cn/AiCuOSWv 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | void Convert(long long n, int x) { //10进制转换成为x进制 15 | vector answer; 16 | if (n == 0) { 17 | answer.push_back(0); 18 | } else { 19 | while (n != 0) { 20 | answer.push_back(n % x); 21 | n /= x; 22 | } 23 | } 24 | for (int i = answer.size() - 1; i >= 0; --i) { 25 | printf("%d", answer[i]); 26 | } 27 | printf("\n"); 28 | } 29 | 30 | int main() { 31 | int m; 32 | while (scanf("%d", &m) != EOF) { 33 | if (m == 0) { 34 | break; 35 | } 36 | long long a, b; 37 | scanf("%lld%lld", &a, &b); 38 | Convert(a + b, m); 39 | } 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Chapter6/6.3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:进制转换 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/AiCuig9B 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | int CharToInt(char c) { //字符变成数字 15 | if ('0' <= c && c <= '9') { 16 | return c - '0'; 17 | } else { 18 | return c - 'A' + 10; 19 | } 20 | } 21 | 22 | void Convert(string str, int x) { //x进制转换10进制 23 | int number = 0; 24 | for (int i = 0; i < str.size(); ++i) { 25 | number *= x; 26 | number += CharToInt(str[i]); 27 | } 28 | printf("%d\n", number); 29 | } 30 | 31 | int main() { 32 | string str; 33 | while (cin >> str) { 34 | str = str.substr(2); 35 | Convert(str, 16); 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Chapter6/6.4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:数制转换 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/AiCu6ne4 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | char IntToChar(int x) { //数字变成字符 15 | if (x < 10) { 16 | return x + '0'; 17 | } else { 18 | return x - 10 + 'A'; 19 | } 20 | } 21 | 22 | int CharToInt(char c) { //字符变成数字 23 | if ('0' <= c && c <= '9') { 24 | return c - '0'; 25 | } else if ('A' <= c && c <= 'Z') { 26 | return c - 'A' + 10; 27 | } else { 28 | return c - 'a' + 10; 29 | } 30 | } 31 | 32 | long long Convert2Ten(string str, int x) { //x进制转换10进制 33 | long long number = 0; 34 | for (int i = 0; i < str.size(); ++i) { 35 | number *= x; 36 | number += CharToInt(str[i]); 37 | } 38 | return number; 39 | } 40 | 41 | void Convert2Target(long long n, int x) { //10进制转换成为x进制 42 | vector answer; 43 | if (n == 0) { 44 | answer.push_back('0'); 45 | } else { 46 | while (n != 0) { 47 | answer.push_back(IntToChar(n % x)); 48 | n /= x; 49 | } 50 | } 51 | for (int i = answer.size() - 1; i >= 0; --i) { 52 | printf("%c", answer[i]); 53 | } 54 | printf("\n"); 55 | } 56 | 57 | int main() { 58 | int a, b; 59 | string str; 60 | while (cin >> a >> str >> b) { 61 | long long number = Convert2Ten(str, a); 62 | Convert2Target(number, b); 63 | } 64 | return 0; 65 | } -------------------------------------------------------------------------------- /Chapter6/6.5.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:最简真分数 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/AiCua2g8 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | const int MAXN = 600 + 10; 14 | 15 | int arr[MAXN]; 16 | 17 | int GCD(int a, int b) { 18 | if (b == 0) { 19 | return a; 20 | } else { 21 | return GCD(b, a % b); 22 | } 23 | } 24 | 25 | int main() { 26 | int n; 27 | while (scanf("%d", &n) != EOF) { 28 | if (n == 0) { 29 | break; 30 | } 31 | for (int i = 0; i < n; ++i) { 32 | scanf("%d", &arr[i]); 33 | } 34 | int answer = 0; 35 | for (int i = 0; i < n; ++i) { 36 | for (int j = i + 1; j < n; ++j) { 37 | if (GCD(arr[i], arr[j]) == 1) { 38 | answer++; 39 | } 40 | } 41 | } 42 | printf("%d\n", answer); 43 | } 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Chapter6/6.6.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:Prime Number 3 | * 题目来源:上海交通大学复试上机题 4 | * 题目链接:http://t.cn/AiCulrSh 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int MAXN = 1e5 + 10; 15 | 16 | vector prime; 17 | bool isPrime[MAXN]; 18 | 19 | void Initial() { 20 | fill(isPrime, isPrime + MAXN, true); 21 | isPrime[0] = false; 22 | isPrime[1] = false; 23 | for (int i = 2; i < MAXN; ++i) { 24 | if (!isPrime[i]) { 25 | continue; 26 | } 27 | prime.push_back(i); 28 | if (i > MAXN / i) { 29 | continue; 30 | } 31 | for (int j = i * i; j < MAXN; j += i) { 32 | isPrime[j] = false; 33 | } 34 | } 35 | return ; 36 | } 37 | 38 | int main() { 39 | Initial(); 40 | int k; 41 | while (scanf("%d", &k) != EOF) { 42 | printf("%d\n", prime[k - 1]); 43 | } 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Chapter6/6.7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:约数的个数 3 | * 题目来源:清华大学复试上机题 4 | * 题目链接:http://t.cn/Aip7dTUp 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | const int MAXN = 4e4; 16 | 17 | vector prime; //保存质数 18 | bool isPrime[MAXN]; //标记数组 19 | 20 | void Initial() { 21 | for (int i = 0; i < MAXN; ++i) { //初始化 22 | isPrime[i] = true; 23 | } 24 | isPrime[0] = false; 25 | isPrime[1] = false; 26 | for (int i = 2; i < MAXN; ++i) { 27 | if (!isPrime[i]) { //非质数则跳过 28 | continue; 29 | } 30 | prime.push_back(i); 31 | if (i > MAXN / i) { 32 | continue; 33 | } 34 | for (int j = i * i; j < MAXN; j += i) { 35 | isPrime[j] = false; //质数的倍数为非质数 36 | } 37 | } 38 | return ; 39 | } 40 | 41 | int NumberOfFactor(int number) { 42 | int answer = 1; 43 | for (int i = 0; i < prime.size() && prime[i] <= number; ++i) { 44 | int exponent = 0; 45 | while (number % prime[i] == 0) { 46 | exponent++; 47 | number /= prime[i]; 48 | } 49 | answer *= exponent + 1; 50 | } 51 | if (number > 1) { 52 | answer *= 2; 53 | } 54 | return answer; 55 | } 56 | 57 | int main() { 58 | Initial(); 59 | int n; 60 | while (scanf("%d", &n) != EOF) { 61 | if (n == 0) { 62 | break; 63 | } 64 | for (int i = 0; i < n; ++i) { 65 | int number; 66 | scanf("%d", &number); 67 | printf("%d\n", NumberOfFactor(number)); 68 | } 69 | } 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Chapter6/6.8.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:整除问题 3 | * 题目来源:上海交通大学复试上机题 4 | * 题目链接:http://t.cn/Aip7eHBD 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | using namespace std; 16 | 17 | const int MAXN = 1e3 + 10; 18 | const int INF = INT_MAX; 19 | 20 | vector prime; 21 | bool isPrime[MAXN]; 22 | 23 | void Initial() { 24 | for (int i = 0; i < MAXN; ++i) { 25 | isPrime[i] = true; 26 | } 27 | isPrime[0] = false; 28 | isPrime[1] = false; 29 | for (int i = 2; i < MAXN; ++i) { 30 | if (!isPrime[i]) { 31 | continue; 32 | } 33 | prime.push_back(i); 34 | if (i > MAXN / i) { 35 | continue; 36 | } 37 | for (int j = i * i; j < MAXN; j += i) { 38 | isPrime[j] = false; 39 | } 40 | } 41 | return ; 42 | } 43 | 44 | int main() { 45 | Initial(); 46 | int n, a; 47 | while (scanf("%d%d", &n, &a) != EOF) { 48 | map exponentN; //存储n!的质因数分解 49 | for (int j = 2; j <= n; ++j) { 50 | int number = j; 51 | for (int i = 0; i < prime.size() && prime[i] <= number; ++i) { 52 | while (number % prime[i] == 0) { 53 | exponentN[prime[i]]++; 54 | number /= prime[i]; 55 | } 56 | } 57 | } 58 | map exponentA; //存储a的质因数分解 59 | for (int i = 0; i < prime.size() && prime[i] <= a; ++i) { 60 | while (a % prime[i] == 0) { 61 | exponentA[prime[i]]++; 62 | a /= prime[i]; 63 | } 64 | } 65 | int answer = INF; 66 | for (map::iterator it = exponentA.begin(); it != exponentA.end(); ++it) { 67 | if (exponentN.find(it->first) == exponentN.end()) { 68 | answer = 0; 69 | } else { 70 | answer = min(answer, exponentN[it->first] / it->second); 71 | } 72 | } 73 | printf("%d\n", answer); 74 | } 75 | return 0; 76 | } -------------------------------------------------------------------------------- /Chapter6/6.9.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:求root(N, k) 3 | * 题目来源:清华大学复试上机题 4 | * 题目链接:http://t.cn/AipAw4B1 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | int QuickPower(int x, int y, int k) { 14 | int result = 1; 15 | x %= k; 16 | while(y != 0) { 17 | if(y % 2 == 1) { 18 | result *= x; 19 | result %= k; 20 | } 21 | x = (x * x) % k; 22 | y /= 2; 23 | } 24 | return result % k; 25 | } 26 | int main() { 27 | int x, y, k; 28 | while (scanf("%d%d%d", &x, &y, &k) != EOF) { 29 | int result = QuickPower(x, y, k - 1); 30 | if (x != 0 && result == 0) { 31 | result = k - 1; 32 | } 33 | printf("%d\n", result); 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Chapter7/7.1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:代理服务器 3 | * 题目来源:清华大学复试上机题 4 | * 题目链接:http://t.cn/E9emuS9 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int MAXN = 1e3 + 10; 15 | 16 | string proxy[MAXN]; 17 | string server[5 * MAXN]; 18 | int position[MAXN]; 19 | 20 | int Update(int current, int n, int m) { 21 | for (int i = current; i < m; ++i) { 22 | int index = lower_bound(proxy, proxy + n, server[i]) - proxy; 23 | if (index == n || proxy[index] != server[i] ) { 24 | continue; 25 | } 26 | position[index] = min(position[index], i); 27 | } 28 | for (int i = 0; i < n; ++i) { 29 | current = max(position[i], current); 30 | } 31 | return current; 32 | } 33 | 34 | int main() { 35 | int n, m; 36 | while (scanf("%d", &n) != EOF) { 37 | for (int i = 0; i < n; ++i) { 38 | cin >> proxy[i]; 39 | } 40 | sort(proxy, proxy + n); 41 | scanf("%d", &m); 42 | for (int i = 0; i < m; ++i) { 43 | cin >> server[i]; 44 | } 45 | int number = -1; 46 | if (n == 1) { 47 | for (int j = 0; j < m; ++j) { 48 | if (proxy[0] == server[j]) { 49 | break; 50 | } 51 | } 52 | } else { 53 | int current = 0; 54 | while (current != m) { 55 | fill(position, position + n, m); 56 | current = Update(current, n, m); 57 | number++; 58 | } 59 | } 60 | printf("%d\n", number); 61 | } 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Chapter7/7.2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:To Fill or Not to Fill 3 | * 题目来源:浙江大学复试上机题 4 | * 题目链接:http://dwz.win/JGT 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | const int MAXN = 500 + 10; 16 | const int INF = INT_MAX; 17 | 18 | struct Station { 19 | double price; 20 | double position; 21 | }; 22 | 23 | Station arr[MAXN]; 24 | 25 | bool Compare(Station x, Station y) { 26 | if (x.position == y.position) { 27 | return x.position < y.price; 28 | } else { 29 | return x.position < y.position; 30 | } 31 | } 32 | 33 | int main() { 34 | int capacity, distance, average, number; 35 | while (scanf("%d%d%d%d", &capacity, &distance, &average, &number) != EOF) { 36 | double maxDistance = capacity * average; 37 | for (int i = 0; i < number; ++i) { 38 | scanf("%lf%lf", &arr[i].price, &arr[i].position); 39 | } 40 | arr[number].price = 0; 41 | arr[number].position = distance; 42 | sort(arr, arr + number, Compare); 43 | if (arr[0].position != 0) { 44 | printf("The maximum travel distance = %.2f\n", 0.0); 45 | continue; 46 | } 47 | double position = arr[0].position; 48 | double price = arr[0].price; 49 | int index = 0; 50 | double gas = 0; 51 | double answer = 0; 52 | bool reach = true; 53 | while (position != distance) { 54 | bool flag = false; 55 | double newPrice = INF; 56 | int newPosition = 0; 57 | for (int i = index + 1; i <= number && arr[i].position <= position + maxDistance; ++i) { 58 | if (arr[i].price <= price) { 59 | answer += price * ((arr[i].position - position) / average - gas); 60 | gas = 0; 61 | index = i; 62 | price = arr[i].price; 63 | position = arr[i].position; 64 | flag = true; 65 | break; 66 | } else if (arr[i].price <= newPrice) { 67 | newPrice = arr[i].price; 68 | newPosition = arr[i].position; 69 | } 70 | } 71 | if (!flag && newPrice != INF) { 72 | answer += price * (capacity - gas); 73 | gas = capacity - (newPosition - position) / average; 74 | price = newPrice; 75 | position = newPosition; 76 | } 77 | if (!flag && newPrice == INF) { 78 | printf("The maximum travel distance = %.2f\n", position + maxDistance); 79 | reach = false; 80 | break; 81 | } 82 | } 83 | if (reach) { 84 | printf("%.2f\n", answer); 85 | } 86 | } 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /Chapter8/8.1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:杨辉三角形 3 | * 题目来源:西北工业大学复试上机题 4 | * 题目链接:http://t.cn/Ai0KcLRI 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | int Pascal(int row, int col) { 14 | if (row == 1 || col == 1 || col == row) { 15 | return 1; 16 | } else { 17 | return Pascal(row - 1, col - 1) + Pascal(row - 1, col); 18 | } 19 | } 20 | 21 | int main() { 22 | int n; 23 | while (scanf("%d", &n) != EOF) { 24 | for (int i = 1; i <= n; ++i) { 25 | for (int j = 1; j <= i; ++j) { 26 | if (i != j) { 27 | printf("%d ", Pascal(i, j)); 28 | } else { 29 | printf("%d\n", Pascal(i, j)); 30 | } 31 | } 32 | } 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Chapter8/8.2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:全排列 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/Ai0K0hXZ 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | const int MAXN = 10; 16 | 17 | bool visit[MAXN]; 18 | char sequence[MAXN]; 19 | 20 | void GetPermutation(string str, int index) { 21 | if (index == str.size()) { 22 | for (int i = 0; i < str.size(); ++i) { 23 | printf("%c", sequence[i]); 24 | } 25 | printf("\n"); 26 | } 27 | for (int i = 0; i < str.size(); ++i) { 28 | if (visit[i]) { 29 | continue; 30 | } 31 | visit[i] = true; 32 | sequence[index] = str[i]; 33 | GetPermutation(str, index + 1); 34 | visit[i] = false; 35 | } 36 | return ; 37 | } 38 | 39 | int main() { 40 | string str; 41 | while (cin >> str) { 42 | sort(str.begin(), str.end()); 43 | GetPermutation(str, 0); 44 | printf("\n"); 45 | } 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Chapter8/8.3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:2的幂次方 3 | * 题目来源:上海交通大学复试上机题 4 | * 题目链接:http://dwz.win/JPE 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | string Function(int n) { 15 | stack power; 16 | int exponent = 0; 17 | while (n != 0) { 18 | if (n % 2 == 1) { 19 | power.push(exponent); 20 | } 21 | n /= 2; 22 | exponent++; 23 | } 24 | string answer = ""; 25 | while (!power.empty()) { 26 | if (power.top() == 0) { 27 | answer += "2(0)"; 28 | } else if (power.top() == 1) { 29 | answer += "2"; 30 | } else { 31 | answer += "2(" + Function(power.top()) + ")"; 32 | } 33 | power.pop(); 34 | if (!power.empty()) { 35 | answer += "+"; 36 | } 37 | } 38 | return answer; 39 | } 40 | 41 | int main() { 42 | int n; 43 | while (scanf("%d", &n) != EOF) { 44 | cout << Function(n) << endl; 45 | } 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Chapter9/9.1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:玛雅人的密码 3 | * 题目来源:清华大学复试上机题 4 | * 题目链接:http://t.cn/Ai0lUhJj 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | int BFS(string str) { 17 | map myMap; 18 | queue sequence; 19 | sequence.push(str); 20 | myMap[str] = 0; 21 | int answer = -1; 22 | while (!sequence.empty()) { 23 | string current = sequence.front(); 24 | sequence.pop(); 25 | if(current.find("2012") != -1) { 26 | answer = myMap[current]; 27 | break; 28 | } 29 | for (int i = 0; i < current.size() - 1; ++i) { 30 | string newStr = current; 31 | swap(newStr[i], newStr[i + 1]); 32 | if (myMap.count(newStr) == 0) { 33 | sequence.push(newStr); 34 | myMap[newStr] = myMap[current] + 1; 35 | } 36 | } 37 | } 38 | return answer; 39 | } 40 | 41 | int main() { 42 | int n; 43 | while(scanf("%d", &n) != EOF) { 44 | string str; 45 | cin >> str; 46 | int answer = BFS(str); 47 | printf("%d\n", answer); 48 | } 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Chapter9/9.2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:神奇的口袋 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/Ai0u0GUz 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int MAXN = 25; 15 | 16 | int arr[MAXN]; 17 | bool visit[MAXN]; 18 | 19 | void DFS(int index, int sum, int n, int& answer) { 20 | if (sum == 40) { 21 | answer++; 22 | return; 23 | } 24 | for (int i = index; i < n; ++i) { 25 | if (visit[i] || sum + arr[i] > 40) { 26 | continue; 27 | } 28 | visit[i] = true; 29 | DFS(i + 1, sum + arr[i], n, answer); 30 | visit[i] = false; 31 | } 32 | return ; 33 | } 34 | 35 | int main() { 36 | int n; 37 | while(scanf("%d", &n) != EOF) { 38 | for (int i = 0; i < n; ++i) { 39 | scanf("%d", &arr[i]); 40 | } 41 | memset(visit, false, sizeof(visit)); 42 | int answer = 0; 43 | DFS(0, 0, n, answer); 44 | printf("%d\n", answer); 45 | } 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Chapter9/9.3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目名称:八皇后 3 | * 题目来源:北京大学复试上机题 4 | * 题目链接:http://t.cn/Ai0uOazs 5 | * 代码作者:杨泽邦(炉灰) 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | const int MAXN = 10; 17 | 18 | bool visit[MAXN]; 19 | vector answer; 20 | 21 | bool valid(int x, int y, vector> sequence) { 22 | for (int i = 0; i < sequence.size(); ++i) { 23 | if (abs(sequence[i].second - y) == abs(sequence[i].first - x)) { 24 | return false; 25 | } 26 | } 27 | return true; 28 | } 29 | 30 | void DFS(int x, vector> sequence) { 31 | if (sequence.size() == 8) { 32 | int number = 0; 33 | for (int i = 0; i < sequence.size(); ++i) { 34 | number *= 10; 35 | number += sequence[i].second; 36 | } 37 | answer.push_back(number); 38 | return; 39 | } 40 | for (int y = 1; y <= 8; ++y) { 41 | if (visit[y] || !valid(x, y, sequence)) { 42 | continue; 43 | } 44 | visit[y] = true; 45 | sequence.push_back(pair(x, y)); 46 | DFS(x + 1, sequence); 47 | sequence.pop_back(); 48 | visit[y] = false; 49 | } 50 | return ; 51 | } 52 | 53 | int main() { 54 | memset(visit, false, sizeof(visit)); 55 | vector> sequence; 56 | DFS(1, sequence); 57 | int n; 58 | while(scanf("%d", &n) != EOF) { 59 | cout << answer[n - 1] << endl; 60 | } 61 | return 0; 62 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 王道考研机试指南(第二版) 习题代码 2 | 3 | 大家好,我是王道考研机试指南(第二版)的作者炉灰。 4 | 5 | 我将本书所有的习题代码都提交到GitHub上进行托管,大家可以自行查看、学习和下载。 6 | 7 | 点击**Star**进行收藏,以方便之后进行查看。 8 | 9 | 点击**Watch**保持关注,如代码有后续更新,会第一时间收到通知。 10 | 11 | 点击**Fork**克隆到自己的GitHub下,如有bug,请使用**Issues**和我进行讨论;如有修改意见,请使用**Pull requests**提交你的修改意见。 12 | 13 | **如出现书中代码与GitHub代码不一致的情况,以GitHub的代码为准。** 14 | 15 | 祝各位考生考上自己心仪的院校!!! 16 | 17 | 题目名称与链接 18 | | 题目编号 | 题目名称 | 题目链接 | 参考代码 | 19 | |:--------:|:----------:|:-------------------------------------------:|:-------------------------------------------:| 20 | | 习题2.1 | 与7无关的数(北京大学复试上机题) | http://t.cn/E9lOOZQ | https://github.com/BenedictYoung/Practice/blob/master/Chapter2/2.1.cpp| 21 | | 习题2.2 | 百鸡问题(北京哈尔滨工业大学复试上机题) | http://t.cn/E9ldhru | https://github.com/BenedictYoung/Practice/blob/master/Chapter2/2.2.cpp| 22 | | 习题2.3 | old bill(上海交通大学复试上机题) | http://t.cn/E9jqijR | https://github.com/BenedictYoung/Practice/blob/master/Chapter2/2.3.cpp| 23 | | 习题2.4 | Repeater(北京大学复试上机题) | http://t.cn/E9jcaVb | https://github.com/BenedictYoung/Practice/blob/master/Chapter2/2.4.cpp| 24 | | 习题2.5 | Hello World for U(浙江大学复试上机题) | http://t.cn/E9jizni | https://github.com/BenedictYoung/Practice/blob/master/Chapter2/2.5.cpp| 25 | | 习题2.6 | 日期差值(上海交通大学复试上机题) | http://t.cn/E9Yz0LE | https://github.com/BenedictYoung/Practice/blob/master/Chapter2/2.6.cpp| 26 | | 习题2.7 | Day of Week(清华大学复试上机题) | http://t.cn/E9YZLbi | https://github.com/BenedictYoung/Practice/blob/master/Chapter2/2.7.cpp| 27 | | 习题2.8 | 日期类(北京理工大学复试上机题) | http://t.cn/E9RJUp4 | https://github.com/BenedictYoung/Practice/blob/master/Chapter2/2.8.cpp| 28 | | 习题2.9 | Grading(浙江大学复试上机题) | http://t.cn/E9rDPSq | https://github.com/BenedictYoung/Practice/blob/master/Chapter2/2.9.cpp| 29 | | 习题2.10 | 路径打印(上海交通大学复试上机题) | http://t.cn/E9dvHs4 | https://github.com/BenedictYoung/Practice/blob/master/Chapter2/2.10.cpp| 30 | | 习题2.11 | 坠落的蚂蚁(北京大学复试上机题) | http://t.cn/E9dhoRA | https://github.com/BenedictYoung/Practice/blob/master/Chapter2/2.11.cpp| 31 | | 习题3.1 | 特殊排序(华中科技大学复试上机题) | http://t.cn/E9gio39 | https://github.com/BenedictYoung/Practice/blob/master/Chapter3/3.1.cpp| 32 | | 习题3.2 | 整数奇偶排序(北京大学复试上机题) | http://t.cn/E9glPvp | https://github.com/BenedictYoung/Practice/blob/master/Chapter3/3.2.cpp| 33 | | 习题3.3 | 小白鼠排队(北京大学复试上机题) | http://t.cn/E9gXh9Z |https://github.com/BenedictYoung/Practice/blob/master/Chapter3/3.3.cpp| 34 | | 习题3.4 | 奥运排序问题(浙江大学复试上机题) | http://t.cn/E9gYpyl |https://github.com/BenedictYoung/Practice/blob/master/Chapter3/3.4.cpp| 35 | | 习题3.5 | 找最小数(北京邮电大学复试上机题) | http://t.cn/E9gekWa |https://github.com/BenedictYoung/Practice/blob/master/Chapter3/3.5.cpp| 36 | | 习题3.6 | 打印极值点下标(北京大学复试上机题) | http://t.cn/E9ehDw4 |https://github.com/BenedictYoung/Practice/blob/master/Chapter3/3.6.cpp| 37 | | 习题3.7 | 找位置(华中科技大学复试上机题) | http://t.cn/E9eh4jY |https://github.com/BenedictYoung/Practice/blob/master/Chapter3/3.7.cpp| 38 | | 习题4.1 | skew数(北京大学复试上机题) | http://t.cn/Ai8IALKI | https://github.com/BenedictYoung/Practice/blob/master/Chapter4/4.1.cpp| 39 | | 习题4.2 | 单词替换(北京大学复试上机题) | http://t.cn/Ai8Iycp6 | https://github.com/BenedictYoung/Practice/blob/master/Chapter4/4.2.cpp| 40 | | 习题4.3 | 首字母大写(北京大学复试上机题) | http://t.cn/Ai8I2hco | https://github.com/BenedictYoung/Practice/blob/master/Chapter4/4.3.cpp| 41 | | 习题4.4 | 浮点数加法(北京大学复试上机题) | http://t.cn/Ai8I4v0j | https://github.com/BenedictYoung/Practice/blob/master/Chapter4/4.4.cpp| 42 | | 习题4.5 | 后缀字符串排序(上海交通大学复试上机题) | http://1t.click/VGP | https://github.com/BenedictYoung/Practice/blob/master/Chapter4/4.5.cpp| 43 | | 习题4.6 | 字符串匹配(北京航空航天大学复试上机题) | http://1t.click/VGG | https://github.com/BenedictYoung/Practice/blob/master/Chapter4/4.6.cpp| 44 | | 习题4.7 | String Matching(上海交通大学复试上机题) | http://1t.click/VGH | https://github.com/BenedictYoung/Practice/blob/master/Chapter4/4.7.cpp| 45 | | 习题5.1 | 堆栈的使用(吉林大学复试上机题) | http://t.cn/AiKKM6F6 | https://github.com/BenedictYoung/Practice/blob/master/Chapter5/5.1.cpp| 46 | | 习题5.2 | 计算表达式(上海交通大学复试上机题) | http://t.cn/AiKKJjJ5 | https://github.com/BenedictYoung/Practice/blob/master/Chapter5/5.2.cpp| 47 | | 习题6.1 | 八进制(华中科技大学复试上机题) | http://t.cn/AiCu0lHe | https://github.com/BenedictYoung/Practice/blob/master/Chapter6/6.1.cpp| 48 | | 习题6.2 | 又一版A+B(浙江大学复试上机题) | http://t.cn/AiCuOSWv | https://github.com/BenedictYoung/Practice/blob/master/Chapter6/6.2.cpp| 49 | | 习题6.3 | 进制转换(北京大学复试上机题) | http://t.cn/AiCuig9B | https://github.com/BenedictYoung/Practice/blob/master/Chapter6/6.3.cpp| 50 | | 习题6.4 | 数制转换(北京大学复试上机题) | http://t.cn/AiCu6ne4 | https://github.com/BenedictYoung/Practice/blob/master/Chapter6/6.4.cpp| 51 | | 习题6.5 | 最简真分数(北京大学复试上机题) | http://t.cn/AiCua2g8 | https://github.com/BenedictYoung/Practice/blob/master/Chapter6/6.5.cpp| 52 | | 习题6.6 | Prime Number(上海交通大学复试上机题) | http://t.cn/AiCulrSh | https://github.com/BenedictYoung/Practice/blob/master/Chapter6/6.6.cpp| 53 | | 习题6.7 | 约数的个数(清华大学复试上机题) | http://t.cn/Aip7dTUp | https://github.com/BenedictYoung/Practice/blob/master/Chapter6/6.7.cpp| 54 | | 习题6.8 | 整除问题(上海交通大学复试上机题) | http://t.cn/Aip7eHBD | https://github.com/BenedictYoung/Practice/blob/master/Chapter6/6.8.cpp| 55 | | 习题6.9 | 求root(N,K)(清华大学复试上机题) | http://t.cn/AipAw4B1 | https://github.com/BenedictYoung/Practice/blob/master/Chapter6/6.9.cpp| 56 | | 习题6.10 | A+B for Matrices(浙江大学复试上机题) | http://t.cn/Aipb7GBG | https://github.com/BenedictYoung/Practice/blob/master/Chapter6/6.10.cpp| 57 | | 习题6.11 | 递推数列(清华大学复试上机题) | http://t.cn/AipbZ2sS | https://github.com/BenedictYoung/Practice/blob/master/Chapter6/6.11.cpp| 58 | | 习题6.12 | 数字阶梯求和(哈尔滨工业大学复试上机题) | http://t.cn/Aipak8BQ | https://github.com/BenedictYoung/Practice/blob/master/Chapter6/6.12.cpp| 59 | | 习题6.13 | 大整数的因子(北京大学复试上机题) | http://t.cn/AipaFCJE | https://github.com/BenedictYoung/Practice/blob/master/Chapter6/6.13.cpp| 60 | | 习题7.1 | 代理服务器(清华大学复试上机题) | http://t.cn/E9emuS9 | https://github.com/BenedictYoung/Practice/blob/master/Chapter7/7.1.cpp| 61 | | 习题7.2 | To Fill or Not to Fill(清华大学复试上机题) | http://dwz.date/b2tj | https://github.com/BenedictYoung/Practice/blob/master/Chapter7/7.2.cpp| 62 | | 习题8.1 | 杨辉三角形(西北工业大学复试上机题) | http://t.cn/Ai0KcLRI | https://github.com/BenedictYoung/Practice/blob/master/Chapter8/8.1.cpp| 63 | | 习题8.2 | 全排列(北京大学复试上机题) | http://t.cn/Ai0K0hXZ | https://github.com/BenedictYoung/Practice/blob/master/Chapter8/8.2.cpp| 64 | | 习题8.3 | 2的幂次方(上海交通大学复试上机题) | http://suo.im/5D5hnX | https://github.com/BenedictYoung/Practice/blob/master/Chapter8/8.3.cpp| 65 | | 习题9.1 | 玛雅人的密码 | http://t.cn/Ai0lUhJj | https://github.com/BenedictYoung/Practice/blob/master/Chapter9/9.1.cpp| 66 | | 习题9.2 | 神奇的口袋(北京大学复试上机题) |http://t.cn/Ai0u0GUz | https://github.com/BenedictYoung/Practice/blob/master/Chapter9/9.2.cpp| 67 | | 习题9.3 | 八皇后(北京大学复试上机题) |http://t.cn/Ai0uOazs | https://github.com/BenedictYoung/Practice/blob/master/Chapter9/9.3.cpp| 68 | | 习题10.1 | 二叉搜索树(浙江大学复试上机题) | http://t.cn/Ai9PUJtK | https://github.com/BenedictYoung/Practice/blob/master/Chapter10/10.1.cpp| 69 | | 习题10.2 | 查找第K小的数(北京邮电大学复试上机题) | http://t.cn/AiCu5hcK | https://github.com/BenedictYoung/Practice/blob/master/Chapter10/10.2.cpp| 70 | | 习题10.3 | 搬水果(吉林大学复试上机题) | http://t.cn/AiCu5nsQ | https://github.com/BenedictYoung/Practice/blob/master/Chapter10/10.3.cpp| 71 | | 习题10.4 | 统计同成绩学生人数(浙江大学复试上机题) | http://t.cn/Ai0u0GUz | https://github.com/BenedictYoung/Practice/blob/master/Chapter10/10.4.cpp| 72 | | 习题10.5 | 开门人和关门人(浙江大学复试上机题) | http://t.cn/AiCuM09f | https://github.com/BenedictYoung/Practice/blob/master/Chapter10/10.5.cpp| 73 | | 习题10.6 | 谁是你的潜在朋友(北京大学复试上机题) | http://t.cn/AiCux4f7 | https://github.com/BenedictYoung/Practice/blob/master/Chapter10/10.6.cpp| 74 | | 习题11.1 | 找出直系亲属(浙江大学复试上机题) | http://t.cn/AiOzTX5c | https://github.com/BenedictYoung/Practice/blob/master/Chapter11/11.1.cpp| 75 | | 习题11.2 | 第一题(上海交通大学复试上机题) | http://t.cn/AiOhkgMJ | https://github.com/BenedictYoung/Practice/blob/master/Chapter11/11.2.cpp| 76 | | 习题11.3 | Head of a Gang(浙江大学复试上机题) | http://t.cn/AiOzQMBH | https://github.com/BenedictYoung/Practice/blob/master/Chapter11/11.3.cpp| 77 | | 习题11.4 | Freckles(北京大学复试上机题) | http://t.cn/AiW3Hbqr | https://github.com/BenedictYoung/Practice/blob/master/Chapter11/11.4.cpp| 78 | | 习题11.5 | Jungle Roads(北京大学复试上机题) | http://t.cn/AiW33P91 | https://github.com/BenedictYoung/Practice/blob/master/Chapter11/11.5.cpp| 79 | | 习题11.6 | 最短路径(上海交通大学复试上机题) | http://t.cn/AilPCAuL | https://github.com/BenedictYoung/Practice/blob/master/Chapter11/11.6.cpp| 80 | | 习题12.1 | 吃糖果(北京大学复试上机题) | http://t.cn/AiQsVyKz | https://github.com/BenedictYoung/Practice/blob/master/Chapter12/12.1.cpp| 81 | | 习题12.2 | 最大连续子序列(浙江大学复试上机题) | http://t.cn/AiYoUkjP | https://github.com/BenedictYoung/Practice/blob/master/Chapter12/12.2.cpp| 82 | | 习题12.3 | 合唱队形(北京大学复试上机题) | http://t.cn/AiYNyHPe | https://github.com/BenedictYoung/Practice/blob/master/Chapter12/12.3.cpp| 83 | | 习题12.4 | Coincidence(上海交通大学复试上机题) | http://t.cn/AiY03RO5 | https://github.com/BenedictYoung/Practice/blob/master/Chapter12/12.4.cpp| 84 | | 习题12.5 | 采药(北京大学复试上机题) | http://dwz.win/Knc | https://github.com/BenedictYoung/Practice/blob/master/Chapter12/12.5.cpp| 85 | | 习题12.6 | 最小邮票数(清华大学复试上机题) | http://t.cn/AiYlwchD| https://github.com/BenedictYoung/Practice/blob/master/Chapter12/12.6.cpp| 86 | | 习题12.7 | 放苹果(北京大学复试上机题) | http://t.cn/AiQsyOnq | https://github.com/BenedictYoung/Practice/blob/master/Chapter12/12.7.cpp| 87 | | 习题12.8 | 整数拆分(清华大学复试上机题) | http://t.cn/AiQsUM0Q | https://github.com/BenedictYoung/Practice/blob/master/Chapter12/12.8.cpp| 88 | --------------------------------------------------------------------------------