# 题目来源
https://noobdream.com//Major/article/56/#:~:text = 标签 %3A 华中科技大学历年机试真题精讲 学习人数 %3A 36.7k, 题库地址: http%3A%2F%2Fwww.noobdream.com%2FDreamJudge%2FContest%2F48%2F%23Problem http%3A%2F%2Fwww.noobdream.com%2FDreamJudge%2FContest%2F49%2F%23Problem 你可以在 DreamJudge 的题库中搜索报考目标的院校名称获取该院校历年机试真题
http://www.noobdream.com/DreamJudge/Contest/48/#Problem
# 统计单词
![1694087672837]()
C++ 编程中,一般在输入方面都是使用 cin。而 cin 是使用空白来定字符串的界的。这就导致了对于带有空格的字符串,只能读取第一个空格之前的部分,比如字符串 “I LOVE YOU”,只能读入 “I”,后面的都无法读入。
注意下面读取一行字符串(其中含有空格)的方法和根据空格分割字符串的方法。
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| using namespace std; |
| |
| |
| int main(){ |
| string s; |
| getline(cin, s); |
| |
| vector<int> res; |
| |
| vector<string> strs; |
| |
| istringstream str(s); |
| string temp; |
| |
| while(str >> temp){ |
| strs.push_back(temp); |
| } |
| |
| for(int i = 0; i < strs.size(); i++){ |
| |
| if(i != strs.size() -1){ |
| res.push_back(strs[i].length()); |
| }else{ |
| |
| temp = strs[i]; |
| |
| |
| if(temp.length() == 1){ |
| continue; |
| }else{ |
| res.push_back(temp.length() - 1); |
| } |
| } |
| } |
| |
| for(int i = 0; i < res.size(); i++){ |
| cout << res[i] << " "; |
| } |
| |
| } |
# IP 地址
![1694088832846]()
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| using namespace std; |
| |
| |
| int main(){ |
| string s; |
| getline(cin, s); |
| |
| for(int i = 0; i < s.length(); i++){ |
| if(s[i] == '.'){ |
| s[i] = ' '; |
| } |
| } |
| |
| |
| vector<string> strs; |
| |
| istringstream str(s); |
| string temp; |
| |
| while(str >> temp){ |
| strs.push_back(temp); |
| } |
| |
| bool res = true; |
| |
| for(string str: strs){ |
| int val = stoi(str); |
| if(val < 0 || val > 255){ |
| res = false; |
| break; |
| } |
| } |
| |
| if(res){ |
| cout << "Yes!" << endl; |
| }else{ |
| cout << "No!" << endl; |
| } |
| } |
255.255.255.255
# 二叉排序树
![1694092314433]()
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| using namespace std; |
| |
| |
| struct TreeNode { |
| TreeNode* left, * right; |
| int val; |
| TreeNode(int _val) : val(_val), left(NULL), right(NULL) {} |
| }; |
| |
| TreeNode* root; |
| |
| int insertNode(int val, TreeNode* ptr) { |
| |
| if (val < ptr->val) { |
| |
| if (ptr->left == NULL) { |
| ptr->left = new TreeNode(val); |
| return ptr->val; |
| } |
| else { |
| return insertNode(val, ptr->left); |
| } |
| |
| } |
| else if (ptr->val < val) { |
| |
| if (ptr->right == NULL) { |
| ptr->right = new TreeNode(val); |
| return ptr->val; |
| }else{ |
| return insertNode(val, ptr->right); |
| } |
| |
| } |
| |
| return -1; |
| } |
| |
| int main() { |
| int N; |
| cin >> N; |
| int val; |
| for (int i = 0; i < N; i++) { |
| cin >> val; |
| |
| if (root == NULL) { |
| root = new TreeNode(val); |
| cout << "-1" << endl; |
| } |
| else { |
| cout << insertNode(val, root) << endl; |
| } |
| } |
| |
| } |
# 字符串连接
![1694093284456]()
| #include <cstdio> |
| #include <cstring> |
| #include <iostream> |
| using namespace std; |
| |
| int main() |
| { |
| char a[210] = {}, b[105] = {}; |
| scanf("%s", a); |
| scanf("%s", b); |
| |
| int len1 = strlen(a); |
| int len2 = strlen(b); |
| int i, j; |
| for (i = len1, j = 0;j < len2;i++, j++) |
| a[i] = b[j]; |
| a[i] = '\0'; |
| printf("%s\n", a); |
| |
| |
| return 0; |
| } |
下面的是错误代码
| int main() { |
| char ch; |
| |
| while((ch = getchar()) != '\n'){ |
| if(ch != ' '){ |
| cout << ch; |
| } |
| } |
| |
| } |
https://noobdream.com/DreamJudge/Issue/page/1397/#
# a+b
![1694096175205]()
| 2 6 |
| 10000000000000000000 10000000000000000000000000000000 |
| |
| |
| 8 |
| 10000000000010000000000000000000 |
注意:根据输入输出样例,这里是要求不停进行的,每一行两个数。
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <cstring> |
| using namespace std; |
| |
| |
| int main() { |
| |
| char a[1002] = { '0' }; |
| char b[1002] = { '0' }; |
| |
| string a_str, b_str; |
| |
| |
| while (cin >> a_str >> b_str) { |
| memset(a, '0', 1002); |
| memset(b, '0', 1002); |
| |
| for (int i = 1001, j = a_str.length() - 1; i >= 0 && j >= 0; i--, j--) { |
| a[i] = a_str[j]; |
| } |
| |
| for (int i = 1001, j = b_str.length() - 1; i >= 0 && j >= 0; i--, j--) { |
| b[i] = b_str[j]; |
| } |
| |
| int cur = 0; |
| |
| for (int i = 1001; i >= 0; i--) { |
| int bit = a[i] - '0' + b[i] - '0' + cur; |
| |
| if (bit > 9) { |
| a[i] = bit % 10 + '0'; |
| cur = bit / 10; |
| } |
| else { |
| a[i] = bit + '0'; |
| cur = 0; |
| } |
| } |
| |
| int i = 0; |
| while (a[i] == '0') { |
| i++; |
| } |
| |
| for (; i < 1002; i++) { |
| cout << a[i]; |
| } |
| cout << endl; |
| } |
| |
| } |
# 特殊排序
![1694097199692]()
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <cstring> |
| using namespace std; |
| |
| |
| int main() { |
| |
| int N, val; |
| vector<int> ve; |
| |
| while (cin >> N) { |
| |
| ve.clear(); |
| |
| for (int i = 0; i < N; i++) { |
| cin >> val; |
| ve.push_back(val); |
| } |
| |
| if (ve.size() == 1) { |
| cout << ve[0] << endl << "-1" << endl; |
| |
| } |
| else { |
| sort(ve.begin(), ve.end()); |
| cout << ve[ve.size() - 1] << endl; |
| |
| for (int i = 0; i <= ve.size() - 2; i++) { |
| cout << ve[i] << " "; |
| } |
| cout << endl; |
| } |
| } |
| |
| } |
# 最长 & 最短文本
![1694137856532]()
| hello |
| she |
| sorry |
| he |
| |
| |
| he |
| hello |
| sorry |
| #include<iostream> |
| #include<vector> |
| #include<string.h> |
| #include<algorithm> |
| |
| using namespace std; |
| |
| bool cmp(string a,string b){ |
| return a.length()<b.length(); |
| } |
| int main(){ |
| |
| vector<string> vec; |
| string s; |
| |
| |
| while(getline(cin, s)){ |
| vec.push_back(s); |
| } |
| |
| sort(vec.begin(),vec.end(),cmp); |
| |
| int aend=0; |
| while(vec[aend].length()==vec[0].length()) aend++; |
| |
| int bstart=vec.size()-1; |
| while(vec[bstart].length()==vec[vec.size()-1].length()) bstart--; |
| |
| |
| for(int i=0;i<aend;i++) cout<<vec[i]<<endl; |
| for(int i=bstart+1;i<vec.size();i++) cout<<vec[i]<<endl; |
| |
| |
| } |
根据换行符分隔的字符串用 getline 作为输入
![1694137959057]()
# 八进制
![1694138410528]()
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <cstring> |
| using namespace std; |
| |
| bool cmp(string a, string b) { |
| return a.length() < b.length(); |
| } |
| |
| int main() { |
| int num; |
| char s[10]; |
| |
| while (cin >> num) { |
| int index = 0; |
| |
| while (num != 0) { |
| s[index++] = num % 8 + '0'; |
| num = num / 8; |
| } |
| |
| for (int i = index - 1; i >= 0; i--) { |
| cout << s[i]; |
| } |
| |
| cout << endl; |
| } |
| |
| |
| } |
# 阶乘 2
![1694140308718]()
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <map> |
| #include <cstring> |
| using namespace std; |
| |
| |
| map<int, int> mp; |
| |
| |
| |
| int fn(int n) { |
| if (mp[n] != 0) { |
| return mp[n]; |
| } |
| if (n == 1) |
| return 1; |
| |
| |
| int res = n * fn(n - 1); |
| |
| mp[n] = res; |
| return res; |
| } |
| |
| |
| int solve(int n) { |
| if (n <= 0) { |
| return 0; |
| } |
| |
| return fn(n) + solve(n - 2); |
| } |
| |
| |
| |
| int main() { |
| mp[1] = 1; |
| |
| int num; |
| |
| cin >> num; |
| |
| if (num % 2 == 0) { |
| cout << solve(num - 1) << " " << solve(num) << endl; |
| } |
| else { |
| cout << solve(num) << " " << solve(num - 1) << endl; |
| } |
| } |
# 找位置
![1694142044419]()
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <map> |
| #include <cstring> |
| using namespace std; |
| |
| |
| |
| int main() { |
| string s; |
| cin >> s; |
| |
| map<char, vector<int> > mp; |
| |
| for (int i = 0; i < s.length(); i++) { |
| char ch = s[i]; |
| |
| if (mp.find(ch) == mp.end()) { |
| |
| mp[ch] = vector<int>{ i }; |
| } |
| else { |
| mp[ch].push_back(i); |
| } |
| } |
| |
| |
| map<char, vector<int> >::iterator it = mp.begin(); |
| |
| vector<char> ve; |
| map<char, int> isIn; |
| |
| for (int i = 0; i < s.length(); i++) { |
| char ch = s[i]; |
| |
| if (mp[ch].size() > 1 && isIn[ch] == 0) { |
| ve.push_back(ch); |
| isIn[ch] = 1; |
| } |
| } |
| |
| for (int i = 0; i < ve.size(); i++) { |
| char ch = ve[i]; |
| |
| for (int j = 0; j < mp[ch].size(); j++) { |
| cout << ch << ":" << mp[ch][j]; |
| |
| if (j != mp[ch].size() - 1) { |
| cout << ","; |
| } |
| } |
| cout << endl; |
| } |
| |
| } |
tolower 将字符转为小写
# 回文字符串
![1694142485360]()
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <map> |
| #include <cstring> |
| using namespace std; |
| |
| |
| bool solve(string s) { |
| int i = 0, j = s.length() - 1; |
| |
| while (i < j) { |
| if (s[i++] != s[j--]) { |
| return false; |
| } |
| } |
| |
| return true; |
| } |
| |
| int main() { |
| |
| string s; |
| |
| while (cin >> s) { |
| if (solve(s)) { |
| cout << "Yes!" << endl; |
| } |
| else { |
| cout << "No!" << endl; |
| } |
| } |
| |
| |
| } |
# N 阶楼梯上楼问题
![1694145478388]()
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <map> |
| #include <cstring> |
| using namespace std; |
| |
| |
| int main() { |
| |
| |
| int N; |
| |
| |
| while (cin >> N) { |
| vector<long> dp(N + 1, 0); |
| dp[1] = 1; |
| dp[2] = 2; |
| |
| for (int i = 3; i <= N; i++) { |
| dp[i] = dp[i - 1] + dp[i - 2]; |
| } |
| |
| cout << dp[N] << endl; |
| } |
| |
| } |
注意这里要用 long 类型,int 类型过不去。
# 大整数排序
![1694146196473]()
| 3 |
| 11111111111111111111111111111 |
| 2222222222222222222222222222222222 |
| 33333333 |
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <map> |
| #include <cstring> |
| using namespace std; |
| |
| bool cmp(string a, string b) { |
| if (a.length() < b.length()) { |
| return true; |
| } |
| else if (a.length() > b.length()) { |
| return false; |
| } |
| else { |
| return a < b; |
| } |
| |
| } |
| |
| int main() { |
| int N; |
| vector<string> ve; |
| string s; |
| |
| while (cin >> N) { |
| |
| ve.clear(); |
| for (int i = 0; i < N; i++) { |
| cin >> s; |
| |
| |
| ve.push_back(s); |
| } |
| |
| |
| |
| sort(ve.begin(), ve.end(), cmp); |
| |
| for (int i = 0; i < ve.size(); i++) { |
| cout << ve[i] << endl; |
| } |
| } |
| |
| } |
# 二叉排序树 2
![1694147946788]()
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <map> |
| #include <cstring> |
| using namespace std; |
| |
| |
| struct TreeNode { |
| int val; |
| TreeNode* left, * right; |
| TreeNode(int _val): val(_val), left(NULL), right(NULL){} |
| }; |
| |
| |
| void insert(int val, TreeNode* ptr) { |
| if (val < ptr->val) { |
| if (ptr->left == NULL) { |
| ptr->left = new TreeNode(val); |
| } |
| else { |
| insert(val, ptr->left); |
| } |
| } |
| else if (val > ptr->val) { |
| if (ptr->right == NULL) { |
| ptr->right = new TreeNode(val); |
| } |
| else { |
| insert(val, ptr->right); |
| } |
| } |
| } |
| |
| void destroy(TreeNode* ptr) { |
| if (ptr == NULL) |
| return; |
| |
| destroy(ptr->left); |
| destroy(ptr->right); |
| |
| delete ptr; |
| } |
| |
| void front(TreeNode* ptr) { |
| if (ptr == NULL) |
| return; |
| cout << ptr->val << " "; |
| |
| front(ptr->left); |
| front(ptr->right); |
| } |
| |
| void middle(TreeNode* ptr) { |
| if (ptr == NULL) |
| return; |
| |
| middle(ptr->left); |
| cout << ptr->val << " "; |
| middle(ptr->right); |
| } |
| |
| |
| void post(TreeNode* ptr) { |
| if (ptr == NULL) |
| return; |
| |
| post(ptr->left); |
| |
| post(ptr->right); |
| |
| cout << ptr->val << " "; |
| } |
| |
| TreeNode* root; |
| |
| int main() { |
| int N; |
| |
| while (cin >> N) { |
| int rootVal, val; |
| cin >> rootVal; |
| |
| root = new TreeNode(rootVal); |
| |
| for (int i = 1; i < N; i++) { |
| cin >> val; |
| insert(val, root); |
| } |
| |
| front(root); |
| cout << endl; |
| middle(root); |
| cout << endl; |
| post(root); |
| cout << endl; |
| |
| destroy(root); |
| } |
| |
| |
| } |
# 矩阵最大值
![1694156196889]()
| 3 3 |
| 1 1 1 |
| 1 1 1 |
| 1 1 1 |
| 3 3 |
| 3 2 3 |
| 2 3 2 |
| 3 2 3 |
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <map> |
| #include <cstring> |
| #include <numeric> |
| using namespace std; |
| |
| |
| |
| int main() { |
| |
| int m, n; |
| |
| while (cin >> m >> n) { |
| vector<vector<int> > ve; |
| ve.clear(); |
| |
| for (int i = 0; i < m; i++) { |
| vector<int> temp; |
| int val; |
| |
| int maxVal, maxValIndex = 0; |
| |
| for (int j = 0; j < n; j++) { |
| cin >> val; |
| temp.push_back(val); |
| |
| if (j == 0) { |
| maxVal = val; |
| maxValIndex = 0; |
| } |
| else { |
| if (val > maxVal) { |
| maxVal = val; |
| maxValIndex = j; |
| } |
| } |
| } |
| |
| int sum = accumulate(temp.begin(), temp.end(), 0); |
| temp[maxValIndex] = sum; |
| |
| ve.push_back(temp); |
| } |
| |
| for (int i = 0; i < m; i++) { |
| for (int j = 0; j < n; j++) { |
| cout << ve[i][j] << " "; |
| } |
| cout << endl; |
| } |
| } |
| } |
# 守形数
![1694156712868]()
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <map> |
| #include <cstring> |
| #include <numeric> |
| using namespace std; |
| |
| |
| |
| int main() { |
| int N; |
| |
| while (cin >> N) { |
| int N2 = (int)pow(N, 2); |
| |
| string N_str = to_string(N); |
| string N2_str = to_string(N2); |
| |
| if (N_str == N2_str.substr(N2_str.length() - N_str.length(), N_str.length())) { |
| cout << "Yes!" << endl; |
| } |
| else { |
| cout << "No!" << endl; |
| } |
| |
| } |
| |
| |
| } |
# 最大的两个数
![1694158781515]()
| 1 2 4 9 8 |
| -1 4 9 8 8 |
| 12 9 8 7 0 |
| 7 8 9 7 0 |
| |
| |
| 12 9 9 9 8 |
| 7 8 9 8 8 |
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <map> |
| #include <cstring> |
| #include <numeric> |
| using namespace std; |
| |
| |
| struct node { |
| int val; |
| int index; |
| node(int _val, int _index): val(_val), index(_index) {} |
| }; |
| |
| bool cmp(node a, node b) { |
| return a.val > b.val; |
| } |
| |
| |
| int main() { |
| |
| int arr[4][5]; |
| |
| for (int i = 0; i < 4; i++) { |
| for (int j = 0; j < 5; j++) { |
| int val; |
| cin >> val; |
| |
| arr[i][j] = val; |
| } |
| } |
| |
| vector<vector<int> > ve; |
| |
| for (int i = 0; i < 5; i++) { |
| vector<node> temp; |
| for (int j = 0; j < 4; j++) { |
| temp.push_back(node(arr[j][i], j)); |
| } |
| sort(temp.begin(), temp.end(), cmp); |
| |
| if (temp[0].index < temp[1].index) { |
| ve.push_back({ temp[0].val, temp[1].val }); |
| } |
| else { |
| ve.push_back({ temp[1].val, temp[0].val }); |
| } |
| } |
| |
| for (int i = 0; i < 2; i++) { |
| for (int j = 0; j < 5; j++) { |
| cout << ve[j][i] << " "; |
| } |
| cout << endl; |
| } |
| } |
# 遍历链表
![1694158864854]()
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <map> |
| #include <cstring> |
| #include <numeric> |
| using namespace std; |
| |
| struct ListNode |
| { |
| int val; |
| ListNode* next; |
| ListNode(int _val): val(_val), next(NULL){} |
| }; |
| |
| ListNode* root; |
| |
| void insert(int val) { |
| if (root == NULL) { |
| root = new ListNode(val); |
| return; |
| } |
| |
| ListNode* fakeHead = new ListNode(0); |
| fakeHead->next = root; |
| |
| ListNode* pre = fakeHead, * cur = root; |
| |
| while (cur != NULL && cur->val < val) { |
| pre = pre->next; |
| cur = cur->next; |
| } |
| |
| pre->next = new ListNode(val); |
| pre->next->next = cur; |
| |
| root = fakeHead->next; |
| |
| delete fakeHead; |
| } |
| |
| void travel() { |
| ListNode* ptr = root; |
| |
| while (ptr != NULL) { |
| cout << ptr->val << " "; |
| ptr = ptr->next; |
| } |
| cout << endl; |
| } |
| |
| void destroy() { |
| ListNode* ptr = new ListNode(0), * temp; |
| ptr->next = root; |
| |
| while (ptr->next != NULL) { |
| temp = ptr->next; |
| ptr->next = temp->next; |
| delete temp; |
| } |
| |
| delete ptr; |
| |
| root = NULL; |
| } |
| |
| int main() { |
| int n; |
| |
| while (cin >> n) { |
| |
| int val; |
| for (int i = 0; i < n; i++) { |
| cin >> val; |
| insert(val); |
| } |
| travel(); |
| destroy(); |
| } |
| } |
# 成绩排序
![1694172625294]()
| 3 |
| abc 20 99 |
| bcd 19 97 |
| bed 20 97 |
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <map> |
| #include <cstring> |
| #include <numeric> |
| using namespace std; |
| |
| struct Student |
| { |
| string name; |
| int age, score; |
| }; |
| |
| bool cmp(Student a, Student b) { |
| if (a.score != b.score) { |
| return a.score < b.score; |
| } |
| else { |
| if (a.name != b.name) { |
| return a.name < b.name; |
| } |
| else { |
| return a.age < b.age; |
| } |
| } |
| } |
| |
| int main() { |
| int N; |
| |
| while (cin >> N) { |
| vector<Student> ve; |
| |
| for (int i = 0; i < N; i++) { |
| Student stu; |
| cin >> stu.name; |
| cin >> stu.age; |
| cin >> stu.score; |
| ve.push_back(stu); |
| } |
| |
| sort(ve.begin(), ve.end(), cmp); |
| |
| for (int i = 0; i < N; i++) { |
| cout << ve[i].name << " " << ve[i].age << " " << ve[i].score << endl; |
| } |
| } |
| } |
# 对称矩阵
![1694173673928]()
| 4 |
| 16 19 16 6 |
| 19 16 14 5 |
| 16 14 16 3 |
| 6 5 3 16 |
| 2 |
| 1 2 |
| 3 4 |
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <map> |
| #include <cstring> |
| #include <numeric> |
| using namespace std; |
| |
| |
| int main() { |
| int N; |
| |
| while (cin >> N) { |
| vector<vector<int> > ve; |
| |
| for (int i = 0; i < N; i++) { |
| vector<int> temp; |
| |
| for (int j = 0; j < N; j++) { |
| int val; |
| cin >> val; |
| |
| temp.push_back(val); |
| } |
| |
| ve.push_back(temp); |
| } |
| |
| bool res = true; |
| |
| for (int i = 0; i < N; i++) { |
| for (int j = i; j < N; j++) { |
| if (ve[i][j] != ve[j][i]) { |
| res = false; |
| break; |
| } |
| } |
| |
| if (res == false) { |
| break; |
| } |
| } |
| |
| if (res) { |
| cout << "Yes!" << endl; |
| } |
| else { |
| cout << "No!" << endl; |
| } |
| |
| } |
| } |
# 最小年龄的 3 个职工
![1694174637186]()
| 5 |
| 501 Jack 6 |
| 102 Nathon 100 |
| 599 Lily 79 |
| 923 Lucy 15 |
| 814 Mickle 65 |
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <map> |
| #include <cstring> |
| #include <numeric> |
| using namespace std; |
| |
| struct Worker { |
| int num, age; |
| string name; |
| }; |
| |
| |
| bool cmp(Worker a, Worker b) { |
| if (a.age != b.age) { |
| return a.age < b.age; |
| } |
| else { |
| if (a.num != b.num) { |
| return a.num < b.num; |
| } |
| else { |
| return a.name < b.name; |
| } |
| } |
| } |
| |
| int main() { |
| int N; |
| |
| while (cin >> N) { |
| vector<Worker> ve; |
| |
| for (int i = 0; i < N; i++) { |
| Worker w; |
| cin >> w.num >> w.name >> w.age; |
| ve.push_back(w); |
| } |
| |
| sort(ve.begin(), ve.end(), cmp); |
| |
| for (int i = 0; i < 3 && i < N; i++) { |
| cout << ve[i].num << " " << ve[i].name << " " << ve[i].age << endl; |
| } |
| } |
| } |
# 打印日期
![1694178262972]()
| 2000 3 |
| 2000 31 |
| 2000 40 |
| 2000 60 |
| 2000 61 |
| 2001 60 |
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <map> |
| #include <cstring> |
| #include <numeric> |
| using namespace std; |
| |
| bool isLeapYear(int year) { |
| if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { |
| return true; |
| } |
| else { |
| return false; |
| } |
| } |
| |
| int leapYearMonth[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
| int nonleapYearMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
| |
| string toDate(int y, int m, int d) { |
| string y_str = to_string(y); |
| for (int i = 0; i < 4 - y_str.length(); i++) { |
| y_str = "0" + y_str; |
| } |
| |
| string m_str = to_string(m); |
| for (int i = 0; i < 2 - m_str.length(); i++) { |
| m_str = "0" + m_str; |
| } |
| |
| string d_str = to_string(d); |
| for (int i = 0; i < 2 - d_str.length(); i++) { |
| d_str = "0" + d_str; |
| } |
| |
| return y_str + "-" + m_str + "-" + d_str; |
| |
| } |
| |
| int main() { |
| vector<int> leapYearMonthSum = { 31 }; |
| vector<int> nonleapYearMonthSum = { 31 }; |
| |
| for (int i = 1; i < 12; i++) { |
| leapYearMonthSum.push_back(leapYearMonth[i] + leapYearMonthSum[i - 1]); |
| } |
| |
| |
| for (int i = 1; i < 12; i++) { |
| nonleapYearMonthSum.push_back(nonleapYearMonth[i] + nonleapYearMonthSum[i - 1]); |
| } |
| |
| |
| |
| int y, n; |
| |
| while (cin >> y >> n) { |
| int month = 0, d; |
| |
| if (isLeapYear(y)) { |
| |
| while (month < 12) { |
| if (leapYearMonthSum[month] == n) { |
| d = leapYearMonth[month]; |
| break; |
| } |
| else if (leapYearMonthSum[month] > n) { |
| if (month == 0) { |
| d = n; |
| } |
| else { |
| d = n - leapYearMonthSum[month - 1]; |
| } |
| break; |
| } |
| else { |
| month++; |
| } |
| } |
| |
| |
| |
| } |
| else { |
| while (month < 12) { |
| |
| if (nonleapYearMonthSum[month] == n) { |
| d = nonleapYearMonth[month]; |
| break; |
| } |
| else if (nonleapYearMonthSum[month] > n) { |
| if (month == 0) { |
| d = n; |
| } |
| else { |
| d = n - nonleapYearMonthSum[month - 1]; |
| } |
| break; |
| } |
| else { |
| month++; |
| } |
| } |
| |
| |
| } |
| month++; |
| |
| cout << toDate(y, month, d) << endl; |
| } |
| } |
# 奇偶校验
![1694182192824]()
| 3 |
| 3a |
| |
| |
| 10110011 |
| 10110011 |
| 01100001 |
用 atoi 可能会报错。
对字符进行奇校验,字符都是用 ASCII 码表示的(均为 8 位),并且这些 ASCII 码的最高位都是 0,所以校验位为最高位。
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <map> |
| #include <cstring> |
| #include <numeric> |
| using namespace std; |
| |
| |
| string odd(char ch) { |
| string res; |
| char temp = ch; |
| int count = 0; |
| for (int i = 0; i < 8; i++) { |
| if (((temp >> i) & 1) == 1) { |
| count++; |
| res = res + "1"; |
| } |
| else { |
| res = res + "0"; |
| } |
| } |
| if (count % 2 == 0) { |
| res[7] = '1'; |
| } |
| else { |
| res[7] = '0'; |
| } |
| |
| reverse(res.begin(), res.end()); |
| return res; |
| } |
| |
| |
| int main() { |
| |
| string s; |
| |
| while (cin >> s) { |
| for (int i = 0; i < s.length(); i++) { |
| cout << odd(s[i]) << endl; |
| } |
| } |
| |
| } |
# 二叉树遍历 2
![1694187306731]()
| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <sstream> |
| #include <map> |
| #include <cstring> |
| #include <numeric> |
| using namespace std; |
| |
| |
| |
| struct TreeNode |
| { |
| char val; |
| TreeNode* left, * right; |
| TreeNode(char _val): val(_val), left(NULL), right(NULL){} |
| }; |
| |
| string frontTravel, middleTravel; |
| |
| map<char, int> mp; |
| |
| |
| TreeNode* recur(int fl, int fr, int ml, int mr) { |
| if (fl > fr) |
| return NULL; |
| |
| char val = frontTravel[fl]; |
| |
| int index = mp[val]; |
| TreeNode* ptr = new TreeNode(val); |
| |
| int leftLen = index - ml; |
| int rightLen = mr - index; |
| |
| ptr->left = recur(fl + 1, fl + leftLen, ml, index - 1); |
| ptr->right = recur(fl + leftLen + 1, fr, index + 1, index + rightLen); |
| |
| return ptr; |
| } |
| |
| void post(TreeNode* root) { |
| if (root == NULL) |
| return; |
| |
| post(root->left); |
| post(root->right); |
| cout << root->val; |
| } |
| |
| void destroy(TreeNode* root) { |
| if (root == NULL) |
| return; |
| |
| destroy(root->left); |
| destroy(root->right); |
| delete root; |
| } |
| |
| int main() { |
| |
| while (cin >> frontTravel >> middleTravel) { |
| for (int i = 0; i < middleTravel.length(); i++) { |
| mp[middleTravel[i]] = i; |
| } |
| TreeNode* root = recur(0, frontTravel.size() - 1, 0, middleTravel.size() - 1); |
| post(root); |
| cout << endl; |
| destroy(root); |
| } |
| |
| } |