深信服春招开发笔试
第一题结合当下疫情,是个分配救灾物资的题目,暴力解通过率90%。代码:
#include <iostream> using namespace std; struct Thing { string t_name; int num; }; struct Need { string h_name; Thing thing; }; int main() { int N; while(cin >> N) { Thing give[N]; Thing need_thing[N]; for(int i = 0; i < N; i++) { cin >> give[i].t_name >> give[i].num; need_thing[i].t_name = give[i].t_name; need_thing[i].num = 0; } int M; cin >> M; Need need[M]; for(int i = 0; i < M; i++) { cin >> need[i].h_name >> need[i].thing.t_name >> need[i].thing.num; for(int j = 0; j < N; j++) { if(need_thing[j].t_name == need[i].thing.t_name) { need_thing[j].num += need[i].thing.num; } } } for(int i = 0; i < M; i++) { cout << need[i].h_name << " " << need[i].thing.t_name << " "; int sum_need = 0; int sum_have = 0; int a = 0, b = 0; for(int j = 0; j < N; j++) { if(need[i].thing.t_name == need_thing[j].t_name) { sum_need = need_thing[j].num; a = j; } if(give[j].t_name == need[i].thing.t_name) { sum_have = give[j].num; b = j; } } int out = float(need[i].thing.num) / sum_need * sum_have; cout << out << endl; need_thing[a].num -= need[i].thing.num; give[b].num -= out; } } return 0; }
第二题是给一个有空格的字符串,让你把空格都去掉,单词间用逗号隔开。我在本地怎么测都没问题,可提交上去通过率就是80%。。。很疑惑。放上代码:
#include <iostream> using namespace std; int main() { string str; while(getline(cin, str)) { string res = ""; int l = 0, r = 0; while(l < str.size()) { while(l < str.size() && str[l] == ' ') { l++; } r = l; if(res.size() > 0 && r < str.size() && str[r] != ' ') { res += ','; } while(r < str.size() && str[r] != ' ') { res += str[r]; r++; } l = r; } cout << res << endl; } return 0; }
第三题是给一个加减乘除后缀表达式转中缀,没做完,我的思路是先根据后缀建树,然后中序遍历,也不知道能不能行。
#深信服##C++工程师##笔试题目#