交流-华为优招第二批次程序分享【C++】
第一题:括号匹配
栈的经典应用。
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstring> #include <string> #include <map> #include <vector> #include <algorithm> using namespace std; bool isLeft(char a) { return (a == '(') || (a == '[') || (a == '{'); } bool isRight(char a) { return (a == ')') || (a == ']') || (a == '}'); } bool isMatch(char a, char b) { if(a == '('&&b == ')') { return true; } else if(a == '['&&b == ']') { return true; } else if(a == '{'&&b == '}') { return true; } return false; } int main() { #if 1 freopen("ini.txt", "r", stdin); //freopen("out.txt", "w", stdout); #endif string str; vector<char> cvec; cvec.reserve(200); while(cin >> str) { auto iter = str.begin(); for(; iter != str.end(); ++iter) { if(isLeft(*iter)) { cvec.push_back(*iter); } else if(isRight(*iter) && !cvec.empty()) { char c = cvec.back(); cvec.pop_back(); if(!isMatch(c, *iter)) { break; } } else if(isRight(*iter) && cvec.empty()) { break; } } if(iter != str.end() || !cvec.empty()) { cout << "false" << endl; } else { cout << "true" << endl; } } return 0; }
第二题:打印机任务
弱智了,看到了输出的‘,’,却忽略输入的’,‘ 郁闷,当我意识到这个问题,就只剩一分钟,加了一个char c;
一道对c++有利的题,就这么让我给放了。
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstring> #include <string> #include <map> #include <vector> #include <algorithm> using namespace std; #define MAX 500 int input[MAX]; struct QNode { int num; int idx; }; typedef struct Que { QNode* elem; int front; int rear; int len; int sz; }CyQueue; void initQueue(CyQueue& Q) { Q.front = Q.rear = 0; Q.len = Q.sz = 0; Q.elem = nullptr; } bool isEmpty(CyQueue Q) { return Q.len == 0; } int nextIdx(CyQueue Q, int cur) { return (cur + 1) % (Q.sz + 1); } //Q 非空 int getMax(CyQueue Q) { int maxNum = Q.front; int i = nextIdx(Q, Q.front); for(; i != Q.rear; i = nextIdx(Q, i)) { if(Q.elem[i].num > Q.elem[maxNum].num) { maxNum = i; } } return maxNum; } void Pop(CyQueue& Q) { Q.front = nextIdx(Q, Q.front); --Q.len; } QNode getFront(CyQueue& Q) { return Q.elem[Q.front]; } void Push(CyQueue& Q, QNode& q) { Q.elem[Q.rear] = q; Q.rear = nextIdx(Q, Q.rear); ++Q.len; } void printOrder(const int input[], int len, int output[]) { CyQueue Q; //vector<int> ivec; initQueue(Q); Q.len = len; Q.sz = len + 1; Q.elem = new QNode[len + 1]; for(int i = 0; i < len; ++i) { Q.elem[i].num = input[i]; Q.elem[i].idx = i; } Q.front = 0; Q.rear = Q.sz; int numOut = 0; while(!isEmpty(Q)) { int pos = getMax(Q); for(int i = Q.front; i != pos; i = nextIdx(Q, i)) { QNode q = getFront(Q); Pop(Q); Push(Q, q); } QNode q = getFront(Q); output[numOut++] = q.idx; //ivec.push_back(q.idx); Pop(Q); } /*for(auto i : ivec) { cout << i << " "; }*/ } int main() { #if 0 freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); #endif int len = 0; int num; char c; //提交的是while(cin>>num>>c){input[len++]=num;} while(cin >> num) { cin >> c; input[len++] = num; } //const int input[3] = {9, 3, 5}; //int len = 3; int output[MAX] = {0}; printOrder(input, len, output); for(int i = 0; i < len - 1; ++i) { cout << output[i] << ", "; } cout << output[len - 1]; return 0; }
第三题:平安果
用的递归,过了全部案例。预计过所有案例时,够呛。时间花在第二题,有啥思路立马上了。坑了,全部案例,递归栈肯定爆了;
#define _CRT_SECURE_NO_WARNINGS #include <vector> #include <iostream> using namespace std; int getAppleIn(vector<vector<int> >& ivec, int m, int n, int row, int col) { if(row >= m||col >= n) { return 0; } else if(row == m - 1) { int sum = 0; for(int i = col; i < n; ++i) { sum += ivec[row][i]; } return sum; } else if(col == n - 1) { int sum = 0; for(int i = row; i < m; ++i) { sum += ivec[i][col]; } return sum; } else { int sum = ivec[row][col]; int sum1 = getAppleIn(ivec, m, n, row + 1, col); int sum2 = getAppleIn(ivec, m, n, row, col + 1); return sum + (sum1 < sum2 ? sum2 : sum1); } } int getApple(vector<vector<int> >& ivec, int m, int n) { int sum = getAppleIn(ivec, m, n, 0, 0); return sum; } int main() { #if 0 freopen("in.txt", "r", stdin); #endif int m, n; vector<vector<int>> ivec; while(cin >> m >> n) { for(int i = 0; i < m; ++i) { vector<int> tem; for(int j = 0; j < n; ++j) { int num; cin >> num; tem.push_back(num); } ivec.push_back(tem); } int res = getApple(ivec, m, n); cout << res; } return 0; }
#华为##C++工程师#