8.27网易互娱笔试分享
共3道编程,都是模拟类题型,没有用到高级算法。借此锻炼下编码能力、数学分析和代码设计能力。
今年可太难了
#网易互娱笔试#
- 装饰新房
- 旧日宝藏
- 绘制密码
第一题做法:
void test01_decorate()
{
// ifstream f("../testcase_neteaseEnt1.txt");
// cin.rdbuf(f.rdbuf());
int groups;
cin >> groups;
while (groups--) {
int paperLen;
int wallLen;
cin >> paperLen >> wallLen;
vector<string> paper;
vector<string> wall(wallLen, string(wallLen, '\0'));
for (int i = 0; i < paperLen; ++i) {
string temp;
cin >> temp;
paper.emplace_back(temp);
}
// 核心代码,完美结合循环、对称和取余
int n = paperLen;
int m = wallLen;
for (int j = -m / 2; j <= m / 2; ++j) {
for (int i = -m / 2; i <= m / 2; ++i) {
wall[m/2+i][m/2+j] = paper[(n/2+i+n) % n][(n/2+j+n) % n];
}
}
// 打印结果
for (auto& x : wall) {
cout << x << '\n';
}
cout << endl;
}
} 第二题做法: using Pos = pair<int, int>; // P(x, y)
using Rectangle = pair<Pos, Pos>; // Rec(P1, P2)
int area(const Rectangle& rec)
{
Pos p2 = rec.second;
Pos p1 = rec.first;
int height = p2.second - p1.second;
int width = p2.first - p1.first;
return height * width;
}
// iou = 0 说明不相交
int iou(const Rectangle& rec1, const Rectangle& rec2)
{
Rectangle overlap;
Pos& overP1 = overlap.first;
Pos& overP2 = overlap.second;
overP1.first = max(rec1.first.first, rec2.first.first);
overP1.second = max(rec1.first.second, rec2.first.second);
overP2.first = min(rec1.second.first, rec2.second.first);
overP2.second = min(rec1.second.second, rec2.second.second);
if (overP1.first >= overP2.first || overP1.second >= overP2.second) {
return 0;
}
return area(overlap);
}
void test02_overlapArea()
{
// ifstream f("../testcase_neteaseEnt2.txt");
// cin.rdbuf(f.rdbuf());
int groups;
cin >> groups;
while (groups--) {
int numRec;
cin >> numRec;
vector<Rectangle> recs;
for (int i = 0; i < numRec; i++) {
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
recs.push_back(Rectangle({ x0, y0 }, { x1, y1 }));
}
set<int> occured;
int overlapArea = 0;
for (int i = 0; i < numRec; ++i) {
if (occured.count(i)) {
continue;
}
for (int j = i + 1; j < numRec; ++j) {
if (iou(recs[i], recs[j])) {
overlapArea += area(recs[i]) + area(recs[j])
- iou(recs[i], recs[j]);
occured.insert(j);
break;
}
}
}
cout << overlapArea << endl;
}
} 第三题懒了。今年可太难了
