華為2020.4.15機考
阿新 • • 發佈:2021-11-18
第一題:求獲勝者,如果票數相當,按照字典序排序 輸入: Tom,Lily,Tom,Lucy,Lucy,Jack 輸出: Lucy #include <iostream> #include <vector> #include <stack> #include <set> #include <string> #include <algorithm> using namespace std; struct Student { string name; int n = 1; }; bool cmp(const Student &s1, const Student& s2) { if (s1.n == s2.n)return s1.name < s2.name; else return s1.n > s2.n; } int myfind(string &t, vector<Student> &as) { for (int i = 0; i < as.size();i++) { if (as[i].name == t) { (as[i].n)++; return i; } } return -1; } int main(void) { string s; while (cin>>s) { vector<Student> as; string t; for (int i = 0; i <= s.size();i++) { if (s[i] == ','||s[i]=='\0') { if (myfind(t, as) == -1) { struct Student p = { t,1 }; as.push_back(p); } t.clear(); } else t += s[i]; } stable_sort(as.begin(), as.end(), cmp); for (int i = 0; i < as.size();i++) { cout << as[i].name << ':' << as[i].n << endl; } } return 0; } ========================================================================================================== 第二題:字串匹配(強行匹配,純手撕程式碼) 輸入: read read[addr=0x17,mask=0xff,val=0x7],read_his[addr=0xff,mask=0xff,val=0x1],read[addr=0xf0,mask=0xff,val=0x80] 輸出: 0x17 0xff 0x7 0xf0 0xff 0x80 #include <iostream> #include <vector> #include <stack> #include <set> #include <string> #include <algorithm> using namespace std; bool jug(string x) { string d = "0123456789abcdef"; for (int i = 0; i < x.size();i++) { if (d.find(x[i]) == -1)return 0; } return 1; } void handle(string s,string t) { vector<string> ans; if (t.find(s) == 0 && t.find('[') == s.size()) { t = t.substr(s.size()); } else return; if (t[0] == '[' && t[t.size() - 1] == ']')t = t.substr(1, t.size() - 2); //cout << t << ':'; string tmp; for (int i = 0; i <= t.size();i++) { if (t[i] == ',' || t[i] == '\0') { ans.push_back(tmp); tmp.clear(); } else tmp += t[i]; } if (ans[0].find("addr=0x") == 0) { ans[0] = ans[0].substr(7); } else return; if (ans[1].find("mask=0x") == 0) { ans[1] = ans[1].substr(7); } else return; if (ans[2].find("val=0x") == 0) { ans[2] = ans[2].substr(6); } else return; if (jug(ans[0]) && jug(ans[1]) && jug(ans[2]))cout << "0x"+ans[0] << ' ' << "0x" + ans[1] << ' ' << "0x" + ans[2] << endl; } int main(void) { string s,raw; cin >> s; while (cin>>raw) { string t; for (int i = 0; i <= raw.size(); i++) { if ((raw[i] == ','&&raw[i-1]==']')||raw[i]=='\0') { handle(s,t); //cout << t << ':'; t.clear(); } else { t += raw[i]; } } } return 0; }