9.4 荣耀笔试,媒体算法,讨论帖
侵删
侵删
侵删
请大家评论区讨论~
第一题
切数字重组,递归就行了
题目不难,就是输入太费劲了,流下了菜鸡的泪水。。。
void test1(vector<vector<int>> arr, vector<int>& p_arr, vector<vector<int>>& out, int n){ for(int i = 0; i<arr.size(); i++){ vector<int> tmp; int& cur_p = p_arr[i]; // 每一行的指针 int cur_len = arr[i].size(); if(cur_p>=cur_len){ continue; } if(cur_p+n < cur_len){ tmp.assign(arr[i].begin()+cur_p, arr[i].begin()+cur_p+n); cur_p += n; } else{ tmp.assign(arr[i].begin()+cur_p, arr[i].end()); cur_p+=n; } out.push_back(tmp); } bool flag = true; for(int i = 0; i<p_arr.size(); i++){ if(p_arr[i]<arr[i].size()){ flag = false; } } if(flag){ return; } test1(arr, p_arr, out, n); } int main(){ int n = 0; cin >> n; vector<vector<int>> arr; string str_in; while(cin >> str_in){ vector<int> data; string tmp; std::stringstream input(str_in); while(getline(input, tmp, ',')){ // data.push_back(tmp); if(tmp!=","){ data.push_back(atoi(tmp.c_str())); } } // debug // for(const auto& ele:data){ // cout << ele << " "; // } // cout << endl; arr.push_back(data); } // int n = 3; // vector<vector<int>> arr = { // {2,5,6,7,9,5,7}, // {1,7,4,3,4} // }; vector<int> p_arr(arr.size(), 0); vector<vector<int>> out; test1(arr, p_arr, out, n); // out 是输出 vector<int> out_arr; for(const auto& ele:out){ for(const auto& e:ele){ // cout << e << ", "; out_arr.push_back(e); } // cout << endl; } // cout << "res :" << endl; int i = 0; for(const auto& ele: out_arr){ if(i==out_arr.size()-1){ cout << ele << endl; } else{ cout << ele << ","; i++; } } cout << endl; return 0; return 0; }
第二题
不会
区间排序?
求大佬评论区指教
第三题
解算括号,感觉是递归,或者dfs
我这个是不对的!!!
string test2_helper(string& str, int start_p, int& end_p){ // cout << start_p << endl; char num_str = str[start_p]; int num = atoi(&num_str); // cout << num << endl; string tmp_str; char flag = str[start_p+1]; // cout << flag << endl; char end_flag; if(flag=='('){ end_flag = ')'; } if(flag=='{'){ end_flag = '}'; } if(flag=='['){ end_flag = ']'; } int i = start_p+2; while( i<str.size() && str[i]!=end_flag){ tmp_str.push_back(str[i]); i++; } end_p = i; // cout << "end_p: " <<end_p <<endl; // cout << "tmp_str: " << tmp_str << endl; string out; for(int i = 0; i<num; i++){ // cout << "i: " << i << endl; out+=tmp_str; // cout << tm } // cout << "tmp_str: " << tmp_str << endl; return out; } string test2(string& str){ string res; for(int i = 0; i<str.size(); ){ if(str[i]>='0' && str[i] <='9'){ int end_p = 0; auto tmp_str = test2_helper(str, i, end_p); // cout << "tmp_str: " << tmp_str << endl; res+=tmp_str; i = end_p+1; } else{ res+=str[i]; i++; } } return res; }