【华科考研复试机试题】华中科技大学考研复试机试题解题报告
題目來源:牛客網華科考研復試題
以下代碼均在牛客網提交通過。(編譯器類型為G++5.4)
若以下題解思路有考慮欠缺處,望讀者指正。
1.1. 矩陣轉置
1.1.1. 題目描述
輸入一個N*N的矩陣,將其轉置后輸出。要求:不得使用任何額外數組(就地逆置)。
1.1.2. 解題思路
方法1:根據題目漏洞,輸入矩陣后,將行列交換輸出。
方法2:m行n列矩陣a,將所有 a[i][j] 與 a[j][i]交換,然后輸出n行m列矩陣。
本題采用方法1。
1.1.3. Code
#include <iostream> using namespace std; int a[110][110]; int main(){int N;cin >> N;for(int i = 0;i < N; ++i){for(int j = 0;j < N; ++j){cin >> a[i][j];}}for(int i = 0;i < N; ++i){for(int j = 0;j < N; ++j){cout << a[j][i] << " ";}cout << endl;}return 0; }1.2. 統計單詞
1.2.1. 題目描述
編一個程序,讀入用戶輸入的,以“.”結尾的一行文字,統計一共有多少個單詞,并分別輸出每個單詞含有多少個字符。 (凡是以一個或多個空格隔開的部分就為一個單詞)
1.2.2. 解題思路
方法1:用getline輸入整行字符串,然后判斷’ ‘或’.'為單詞結束,統計每個單詞包含字母個數。
方法2:根據c++輸入流的特性(’ ‘,’\n’,’\t’等字符自動分割兩次輸入),直接輸入每個單詞,然后對最后一個單詞做一下特判即可(最后一個單詞末尾去除’.’,即長度-1)。
本題采用方法2。
1.2.3. Code
#include <iostream> using namespace std;int main(){string s;while(cin >> s){if(s.size() and s[s.length()-1] == '.'){cout << s.length()-1 << endl;}else if(s.size() and s[s.length()-1] != '.'){cout << s.length() << " ";}}return 0; }1.3. IP地址
1.3.1. 題目描述
輸入一個ip地址串,判斷是否合法。
1.3.2. 解題思路
1.3.3. Code
#include <bits/stdc++.h> using namespace std;int a[4];int main(){string s;while(cin >> s){fill(a,a+4,0);bool flag = true;int con = 0; //con記錄經過了幾個'.',即當前處于第幾個數字。(共4個數字)for(int i = 0;s[i];++i){// 如果數字,存入a[con]if(s[i] >= '0' and s[i] <= '9'){a[con] = a[con]*10+s[i]-'0';}else if(s[i] == '.'){ //如果是字符'.'con++;}else{ //如果既不是數字又不是字符'.'flag = false;}}//for(int i = 0;i < 4; ++i){if(a[i] < 0 or a[i] > 255){flag = false;break;}}cout << (flag?"Yes!":"No!") << endl;}return 0; }1.4. 二叉排序樹
1.4.1. 題目描述
二叉排序樹,也稱為二叉查找樹。可以是一顆空樹,也可以是一顆具有如下特性的非空二叉樹: 1. 若左子樹非空,則左子樹上所有節點關鍵字值均不大于根節點的關鍵字值; 2. 若右子樹非空,則右子樹上所有節點關鍵字值均不小于根節點的關鍵字值; 3. 左、右子樹本身也是一顆二叉排序樹。 現在給你N個關鍵字值各不相同的節點,要求你按順序插入一個初始為空樹的二叉排序樹中,每次插入后成功后,求相應的父親節點的關鍵字值,如果沒有父親節點,則輸出-1。
1.4.2. 解題思路
看到需要查父結點還以為是并查集的問題,結果就是二叉排序樹模板題。
1.4.3. Code
#include <bits/stdc++.h> using namespace std; struct node{node* left;node* right;int val; }; node* T; // idx[i]記錄第i個結點的父結點。 int idx[110];// 非遞歸二叉排序樹插入操作寫法,x表示第x個(結點)。 void insert(node* l,int x){node* p = T;if(T == NULL){T = l;idx[x] = -1;}else{while(true){if(l->val < p->val){if(p->left != NULL){p = p->left;}else{p->left = l;idx[x] = p->val;return;}}else{if(p->right != NULL){p = p->right;}else{p->right = l;idx[x] = p->val;return;}}}} }int main(){int N;while(cin >> N){T = NULL;int k;for(int i = 0;i < N; ++i){cin >> k;// 申請新結點,后插入到原二叉樹T。node* l = (node*)malloc(sizeof(node));l->left=NULL;l->right=NULL;l->val = k;insert(l,i);cout << idx[i] << endl;}}return 0; }1.5. 字符串連接
1.5.1. 題目描述
不借用任何字符串庫函數實現無冗余地接受兩個字符串,然后把它們無冗余的連接起來。
1.5.2. 解題思路
方法1:使用c++ string類+拼接,或者自己寫代碼拼接。
方法2:輸入后直接輸出。
本題采用方法2。
1.5.3. Code
#include <stdio.h>int main(){char a[100],b[100];while(scanf("%s%s",a,b) != EOF){printf("%s%s",a,b);}return 0; }1.6. a+b
1.6.1. 題目描述
實現一個加法器,使其能夠輸出a+b的值。
1.6.2. 解題思路
1.6.3. Code
#include <iostream> #include <string> using namespace std; string a = "";string b = ""; int ans[1010];void reverse(string &x){for(int i = 0;i < x.length()/2; ++i){swap(x[i],x[x.length()-1-i]);} }int main(){while(cin >> a >> b){fill(ans,ans+1010,0);if(a.length() < b.length()) {swap(a,b);}reverse(a);reverse(b);int carry = 0;for(int i = 0;i < a.length();++i){if(i < b.length()){ans[i] += a[i]+b[i]-2*'0'+carry; }else{ans[i] += (a[i]-'0')+carry;}if(ans[i] >= 10){carry = 1;ans[i]%=10;}else{carry = 0;}}if(carry == 1) cout << 1;for(int i = a.length()-1;i >= 0; --i){cout << ans[i];}cout << endl;}return 0; }1.7. 排序
1.7.1. 題目描述
對輸入的n個數進行排序并輸出。
1.7.2. 解題思路
方法1:采用algorithm庫函數sort(begin,end,compare)。
方法2:采用set庫函數multiset。
本題采用方法2。
1.7.3. Code
#include <bits/stdc++.h> using namespace std; multiset<int> s; int main(){int n;int k;while(cin >> n){while(n--){cin >> k;s.insert(k);}for(auto i:s) cout << i << " ";cout << endl;}return 0; }1.8. 特殊排序
1.8.1. 題目描述
輸入一系列整數,將其中最大的數挑出(如果有多個,則挑出一個即可),并將剩下的數進行排序,如果無剩余的數,則輸出-1。
1.8.2. 解題思路
1.8.3. Code
#include<bits/stdc++.h> using namespace std; multiset<int> s; int main(){int N;int k;while(cin >> N){while(N--){cin >> k;s.insert(k);}cout << *--s.end() <<endl;if(s.size() == 1){cout << -1 << endl;break;}for(auto i = s.begin();i != --s.end(); ++i){cout << *i << " ";}cout << endl;}return 0; }1.9. 二叉樹遍歷
1.9.1. 題目描述
二叉樹的前序、中序、后序遍歷的定義: 前序遍歷:對任一子樹,先訪問跟,然后遍歷其左子樹,最后遍歷其右子樹; 中序遍歷:對任一子樹,先遍歷其左子樹,然后訪問根,最后遍歷其右子樹; 后序遍歷:對任一子樹,先遍歷其左子樹,然后遍歷其右子樹,最后訪問根。 給定一棵二叉樹的前序遍歷和中序遍歷,求其后序遍歷(提示:給定前序遍歷與中序遍歷能夠唯一確定后序遍歷)。
1.9.2. 解題思路
1.9.3. Code
#include<bits/stdc++.h> using namespace std; string a,b;struct node{node* left;node* right;char val;};bool inLeft(char x,char tar){for(int i = 0;i < b.length(); ++i){if(b[i] == x) return true;if(b[i] == tar) return false;} }void insert(node* &T,char x){if(T == NULL){ // cout << x ;node* newNode = (node*)malloc(sizeof(node));newNode->left = NULL;newNode->right = NULL;newNode->val = x;T = newNode; // cout << T->val;return;}if(inLeft(x,T->val)){if(T->left == NULL){T->left = (node*)malloc(sizeof(node));T->left->left = NULL;T->left->right = NULL;T->left->val = x;return;}else{insert(T->left, x);}}else{if(T->right == NULL){T->right = (node*)malloc(sizeof(node));T->right->left = NULL;T->right->right = NULL;T->right->val = x;}else{insert(T->right, x);}} }void dfs(node* T){if(T == NULL) return;dfs(T->left);dfs(T->right);cout << T->val; }int main(){while(cin >> a >> b){node* T = NULL;for(int i = 0;i < a.length(); ++i){insert(T, a[i]);}dfs(T);cout << endl;}return 0; }1.10. 奇偶校驗
1.10.1. 題目描述
輸入一個字符串,然后對每個字符進行奇校驗,最后輸出校驗后的二進制數(如’3’,輸出:10110011)。
1.10.2. 解題思路
1.10.3. Code
#include <iostream> #include <cstring> using namespace std; int main(){string s;while(cin >> s){for(int i = 0;i < s.length(); ++i){int a[8]={0};int c = s[i];int cnt = 0;for(int j = 0;c != 0 and j < 8; ++j){a[j] = c%2;cnt += a[j];c/=2;} // cout << cnt << endl;a[7] = cnt&1?0:1;for(int j = 7;j >= 0; --j){cout << a[j];}cout << endl;}}return 0; }1.11. 最大的兩個數
1.11.1. 題目描述
輸入一個四行五列的矩陣,找出每列最大的兩個數。
1.11.2. 樣例(原先樣例存在一些問題)
1 1 2 4 9 8 -1 4 9 8 --- \ 8 12 9 9 9 8 12 9 8 7 --- / 7 8 9 7 0 0 7 8 9 71.11.3. 解題思路
1.11.4. Code
#include <bits/stdc++.h> using namespace std; #define rep(o,u,p) for(int o = u;i < p; i++)struct node{int val;int idx; }a[5][4];bool cmp(node x, node y){if(x.val != y.val) return x.val > y.val;return x.idx < y.idx; }bool cmp2(node x, node y){//cout << x.val << " "<< y.val << endl;return x.idx < y.idx; }int main(){for(int i = 0;i < 4; ++i){for(int j = 0;j < 5; ++j){cin >> a[j][i].val;a[j][i].idx = i;}}for(int i = 0;i < 5; ++i){sort(a[i],a[i]+4,cmp);sort(a[i],a[i]+2,cmp2);}for(int j = 0;j < 2; ++j){for(int i = 0;i < 5; ++i){cout << a[i][j].val << " ";}cout << endl;}return 0; }1.12. 成績排序
1.12.1. 題目描述
有N個學生的數據,將學生數據按成績從低到高排序,如果成績相同則按姓名字符的字典序由小到大排序,如果姓名的字典序也相同則按照學生的年齡從小到大排序,并輸出N個學生排序后的信息。
1.12.2. 解題思路
1.12.3. Code
#include <iostream> #include <algorithm> using namespace std; struct node{int age;int score;string name; }a[1010];bool cmp(node x,node y){if(x.score!=y.score) return x.score < y.score;if(x.name!=y.name) return x.name<y.name;return x.age < y.age; }int main(){int n;while(cin >> n){for(int i = 0;i < n; ++i){cin >> a[i].name >> a[i].age >> a[i].score;}sort(a,a+n,cmp); // cout << endl;for(int i = 0;i < n; ++i){cout << a[i].name <<" "<< a[i].age <<" "<< a[i].score << endl;}}return 0; }1.13. 遍歷鏈表
1.13.1. 題目描述
建立一個升序鏈表并遍歷輸出。
1.13.2. 解題思路
1.13.3. Code
#include <iostream> #include <algorithm> using namespace std; struct node{node* next;int val; }; void insert(node* head,int k){if(!head) return;node* newNode = (node*)malloc(sizeof(node));newNode->next = NULL;newNode->val = k;if(!head->next){head->next = newNode;return;}node* h1 = head;node* h2 = head->next;while(h2 != NULL and h2->val < k){h1 = h2;h2 = h1->next;}h1->next = newNode;newNode->next = h2;return; }int main(){int n;while(cin >> n){node* head = (node*)malloc(sizeof(node));head->next = NULL;head->val=-1;int k;while(n--){cin >> k;insert(head,k);}node* p = head;while(p->next){cout << p->next->val << " ";p = p->next;}cout << endl;}return 0; }1.14. 守形數
1.14.1. 題目描述
守形數是這樣一種整數,它的平方的低位部分等于它本身。 比如25的平方是625,低位部分是25,因此25是一個守形數。 編一個程序,判斷N是否為守形數。
1.14.2. 解題思路
1.14.3. Code
#include<iostream> using namespace std; int main(){int n;while(cin >> n){int t = n*n;bool flag = true;while(n!= 0){if(t%10 != n%10){flag = false;}t/=10;n/=10;}cout << (flag?"Yes!":"No!") << endl;} return 0; }1.15. 矩陣最大值
1.15.1. 題目描述
編寫一個程序輸入一個mXn的矩陣存儲并輸出,并且求出每行的最大值和每行的總和。 要求把每行總和放入每行最大值的位置,如果有多個最大值,取下標值最小的那一個作為最大值。 最后將結果矩陣輸出。
1.15.2. 解題思路
模擬執行即可。
1.15.3. Code
#include <bits/stdc++.h> using namespace std; int a[110][110]; int sum[110]; int main(){int m,n;while(cin >> m >> n){memset(sum,0,sizeof(sum));for(int i = 0;i < m; ++i){sum[i] = 0;for(int j = 0;j < n; ++j){cin >> a[i][j];sum[i] += a[i][j];}}for(int i = 0;i < m; ++i){int idx = 0;for(int j = 1;j < n; ++j){if(a[i][j] > a[i][idx]){idx = j;}}a[i][idx] = sum[i];for(int j = 0;j < n; ++j){cout << a[i][j]<< " ";}cout << endl;}}return 0; }1.16. 最小年齡的3個職工
1.16.1. 題目描述
職工有職工號,姓名,年齡.輸入n個職工的信息,找出3個年齡最小的職工打印出來。
1.16.2. 解題思路
結構體排序重載compare即可。
1.16.3. Code
#include <bits/stdc++.h> using namespace std; struct node{int age;int num;string name; }a[33];bool cmp(node x, node y){if(x.age != y.age){return x.age < y.age;}if(x.num != y.num){return x.num < y.num;}return x.name < y.name; }int main(){int N;while(cin >> N){for(int i = 0;i < N; ++i){cin >> a[i].num >> a[i].name >> a[i].age;}sort(a,a+N,cmp);for(int i = 0;i < 3; ++i){cout << a[i].num <<" "<< a[i].name <<" "<< a[i].age << endl;}}return 0; }1.17. 對稱矩陣
1.17.1. 題目描述
輸入一個N維矩陣,判斷是否對稱。
1.17.2. 解題思路
1.17.3. Code
#include <bits/stdc++.h> using namespace std; int a[101][101]; int main(){int n;while(cin >> n){for(int i = 0;i < n; ++i){for(int j = 0;j < n; ++j){cin >> a[i][j];}}bool flag = false;for(int i = 0;i < n; ++i){for(int j = i+1;j < n; ++j){if(a[i][j] != a[j][i]){flag = true;break;}}if(flag) break;}if(flag) cout << "No!" << endl;elsecout << "Yes!" << endl;}return 0; }1.18. A+B
1.18.1. 題目描述
給定兩個整數A和B,其表示形式是:從個位開始,每三位數用逗號","隔開。 現在請計算A+B的結果,并以正常形式輸出。
1.18.2. 解題思路
1.18.3. Code
#include <bits/stdc++.h> using namespace std; int main(){string a,b;while(cin >> a >> b){int num1 = 0;int num2 = 0;int flag1 = 1;int flag2 = 1;for(int i = 0;i < a.length(); ++i){if(i == 0 && a[i] == '-'){flag1 = -1;}else{if(a[i] != ',')num1 = num1*10+a[i]-'0';}}for(int i = 0;i < b.length(); ++i){if(i == 0 && b[i] == '-'){flag2 = -1;}else{if(b[i] != ',')num2 = num2*10+b[i]-'0';}}cout << flag1*num1+flag2*num2 << endl;}return 0; }1.19. 打印日期
1.19.1. 題目描述
給出年分m和一年中的第n天,算出第n天是幾月幾號。
1.19.2. 解題思路
1.19.3. Code
#include <bits/stdc++.h> using namespace std; int a[12]={31,28,31,30,31,30,31,31,30,31,30,31}; int main(){int m,n;while(cin >> m >> n){if((m%4==0&&m%100!=0)or(m%400==0)){a[1] = 29;}else{a[1] = 28;}int con = 1;for(int i = 0;i < 12; ++i){if(a[i] < n){con++;n-=a[i];}else{break;}}printf("%04d-%02d-%02d\n",m,con,n);}return 0; }1.20. 二叉排序樹
1.20.1. 題目描述
輸入一系列整數,建立二叉排序樹,并進行前序,中序,后序遍歷。
1.20.2. 解題思路
1.20.3. Code
#include <bits/stdc++.h> using namespace std; struct node{int val;node* left;node* right;};void insert(node* &T, int x){if(T == NULL){T = (node*)malloc(sizeof(node));T->val = x;T->left = NULL;T->right = NULL;return;}if(T->val > x){if(T->left == NULL){T->left = (node*)malloc(sizeof(node));T->left->val = x;T->left->left = NULL;T->left->right = NULL;}elseinsert(T->left,x);}else if(T->val < x){if(T->right == NULL){T->right = (node*)malloc(sizeof(node));T->right->val = x;T->right->left = NULL;T->right->right = NULL;}elseinsert(T->right, x);} } void dfs1(node* &T){if(T == NULL) return;cout<<T->val << " ";dfs1(T->left);dfs1(T->right); } void dfs2(node* &T){if(T == NULL) return;dfs2(T->left);cout<<T->val << " ";dfs2(T->right); } void dfs3(node* &T){if(T == NULL) return;dfs3(T->left);dfs3(T->right);cout<<T->val << " "; }int main(){int n,k;while(cin >> n){node* T = NULL;while(n--){cin >> k;insert(T,k);}dfs1(T);cout << endl;dfs2(T);cout << endl;dfs3(T);cout << endl;}return 0; }1.21. 大整數排序
1.21.1. 題目描述
對N個長度最長可達到1000的數進行排序。
1.21.2. 解題思路
1.21.3. Code
#include <bits/stdc++.h> using namespace std;struct Node{string s; };bool cmp(Node &x, Node &y){if(x.s.length()==y.s.length()){return x.s<y.s;}else{return x.s.length()<y.s.length();} }int main(){int N;while(cin >> N){Node a[N];for(int i = 0;i < N; ++i) cin >> a[i].s;sort(a,a+N,cmp);for(int i = 0;i < N; ++i) cout << a[i].s << endl;}return 0; }1.22. N階樓梯上樓問題
1.22.1. 題目描述
N階樓梯上樓問題:一次可以走兩階或一階,問有多少種上樓方式。(要求采用非遞歸)
1.22.2. 解題思路
1.22.3. Code
#include <bits/stdc++.h> using namespace std; int ans[100];int main(){ans[1] = 1;ans[2] = 2;for(int i = 3;i < 99; ++i){ans[i] = ans[i-1]+ans[i-2];}int N;while(cin >> N){cout << ans[N] << endl;}return 0; }1.23. a+b
1.23.1. 題目描述
計算a+b的和
每行包含兩個整數a和b
對于每行輸入對應輸出一行a和b的和
1.23.2. 解題思路
1.23.3. Code
#include<bits/stdc++.h> using namespace std;int main(){ios::sync_with_stdio(false);int a,b;while(cin >> a >> b) cout << a+b << endl;return 0; }1.24. 回文字符串
1.24.1. 題目描述
給出一個長度不超過1000的字符串,判斷它是不是回文(順讀,逆讀均相同)的。
1.24.2. 解題思路
1.24.3. Code
#include <bits/stdc++.h> using namespace std;int main(){string s,t;while(cin >> s){t = s;reverse(t.begin(),t.end());if(t==s) cout << "Yes!" << endl;else cout << "No!" << endl;}return 0; }1.25. 找位置
1.25.1. 題目描述
對給定的一個字符串,找出有重復的字符,并給出其位置,如:abcaaAB12ab12 輸出:a,1;a,4;a,5;a,10,b,2;b,11,1,8;1,12, 2,9;2,13。
1.25.2. 解題思路
1.25.3. Code
#include<bits/stdc++.h> using namespace std;unordered_map<char,list<int>>m; string s; int main(){while(cin >> s){m.clear();for(int i = 0;i < s.length(); ++i){m[s[i]].push_back(i);}for(int i = 0;i < s.length(); ++i){if(m[s[i]].size()<2) continue;bool f = false;for(auto j:m[s[i]]){if(f) cout << ",";cout << s[i] << ":" << j;f = true;}cout << endl;m[s[i]].clear();}}return 0; }1.26. 階乘
1.26.1. 題目描述
輸入n, 求y1=1!+3!+…m!(m是小于等于n的最大奇數) y2=2!+4!+…p!(p是小于等于n的最大偶數)。
1.26.2. 解題思路
1.26.3. Code
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll fac[32]; ll ans[32]; void presolve(){fac[1] = 1;for(int i = 2;i <= 30; ++i){fac[i] = fac[i-1]*i;}ans[1] = fac[1];ans[2] = fac[2];for(int i = 3;i <= 30; ++i){ans[i] = ans[i-2]+fac[i];} }int main(){presolve();int n;while(cin >> n){if(n&1) cout << ans[n] << " " << ans[n-1] << endl;else cout << ans[n-1] << " " << ans[n] << endl;}return 0; }1.27. 八進制
1.27.1. 題目描述
輸入一個整數,將其轉換成八進制數輸出。
1.27.2. 解題思路
1.27.3. Code
#include <bits/stdc++.h> using namespace std;int main(){ios::sync_with_stdio(false);int N;while(cin >> N){string s = "";while(N!=0){s+=N%8+'0';N/=8;}reverse(s.begin(),s.end());cout << s << endl;}return 0; }1.28. 最長&最短文本
1.28.1. 題目描述
輸入多行字符串,請按照原文本中的順序輸出其中最短和最長的字符串,如果最短和最長的字符串不止一個,請全部輸出。
1.28.2. 解題思路
1.28.3. Code
#include <bits/stdc++.h> using namespace std;int main(){ios::sync_with_stdio(false);map<int,list<string>> m;string s;while(getline(cin,s)){m[s.length()].push_back(s);}auto first = m.begin();for(auto i:first->second){cout << i << endl;}auto eend = --m.end();for(auto i:eend->second){cout << i << endl;}return 0; }1.29. 農夫、羊、菜和狼的故事
1.29.1. 題目描述
有一個農夫帶一只羊、一筐菜和一只狼過河。如果沒有農夫看管,則狼要吃羊,羊要吃菜。但是船很小,只夠農夫帶一樣東西過河。問農夫該如何解此難題?
1.29.2. 解題思路
1.29.3. Code
#include <iostream> using namespace std;int main(){ios::sync_with_stdio(false);cout << "sheep_go\nnothing_come\nvegetable_go\nsheep_come\nwolf_go\nnothing_come\nsheep_go\nsucceed\n" << endl;return 0; }總結
以上是生活随笔為你收集整理的【华科考研复试机试题】华中科技大学考研复试机试题解题报告的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode4. Median of
- 下一篇: JetBrains CLion C++