Java编程

华为软件测试工程师笔试牛客网

第一题,计算星期几,easy

#include cstdio #include cstdlib #include iostream #include vector #include algorithm #include string #include map using namespace std;int main() { int month, day; cin month day; int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int mlen = month – 1; int dlen = 0; for(int i = 0; i mlen; i++) { dlen += days[i]; } dlen += day; int date = dlen % 7; cout (date == 0 ? 7 : date) endl; return 0;}

华为软件测试工程师笔试牛客网

第三题:图论,删除冗余,计算扇出,easy,DFS就好了

#include cstdio #include cstdlib #include iostream #include vector #include algorithm #include string #include sstream #include set #include map #include unordered_map using namespace std;unordered_map string, bool seen;unordered_map string, vector string e;unordered_map string, vector string pre;set string ids;void DFS(string u) { seen[u] = true; for(auto v : e[u]) if(!seen[v]) DFS(v);}int main() { int K; scanf(“%d”, &K); getchar(); while(K–) { string str, token, fid; getline(cin, str); int i = 0; stringstream ss(str); while(getline(ss, token, ‘ ‘)) { ids.insert(token); if(i == 0) { fid = token; i++; } else { e[fid].push_back(token); pre[token].push_back(fid); } } } DFS(“0”); for(auto iter = ids.begin(); iter != ids.end(); iter++) { if(seen[*iter]) { int fan = 0; for(auto id : pre[*iter]) if(seen[id]) fan++; printf(“%s %d\n”, iter- c_str(), fan); } } return 0;}

第二题,报文数据删除,A了50%,感觉思路没有考虑的很周到

#include cstdio #include cstdlib #include iostream #include vector #include algorithm #include string #include sstream #include set #include map #include unordered_map using namespace std;struct Node { int offset; int len; Node(int _offset, int _len): offset(_offset), len(_len) {}};int main() { int plen, offset, len; scanf(“%d\n%d,%d”, &plen, &offset, &len); string str, token; vector Node v; while(true) { cin str; if(str[0] == ‘e’) break; stringstream ss(str); int i = 0, nlen, noffset; while(getline(ss, token, ‘,’)) { if(i == 0) noffset = stoi(token); if(i == 1) nlen = stoi(token); i++; } v.push_back(Node(noffset, nlen)); } int id = offset / plen; int from = offset – id*plen; if(from = v[id].offset && from + len plen && (from + len) = (v[id].offset + v[id].len)) { int left = from – v[id].offset; int right = (v[id].offset + v[id].len) – (from + len); if(left right) { v[id].offset += len; v[id].len -= len; } else { v[id].len -= len; } } else if(from = v[id].offset && from + len plen && (from + len) = (v[id].offset + v[id].len)) { int r = v[id].offset – from; v[id].offset = from + len; v[id].len -= (len – r); } else if(from = v[id].offset && from + len plen && (from + len) (v[id].offset + v[id].len)) { v[id].len = from – v[id].offset; } else if(from = v[id].offset && from + len plen) { v[id].len = from – v[id].offset; if(id + 1 v.size()) { v[id+1].offset += ((from + len) – plen); v[id+1].len -= ((from + len) – plen); } } for(auto iter : v) printf(“%d,%d\n”, iter.offset, iter.len); return 0;

Similar Posts

发表评论

邮箱地址不会被公开。 必填项已用*标注