2020/9/5 15:00 网易互娱自闭笔试题
//1.直接模拟
100%代码
int main() {
//freopen("in.txt", "r", stdin);
int N, M; cin >> N >> M;
vector<int>val(N + 1);
vector<stack<int> >now(N + 1);
for (int i = 1; i <= N; i++) cin >> val[i];
while (M--) {
int ki; cin >> ki;
int ans = 0;
map<string, int>mp;
mp["left"];
mp["right"];
string s1, s2;
int t;
while (ki--) {
cin >> s1 >> s2;
if (s2 == "keep") {
ans += mp[s1];
mp[s1] = 0;
}
else {
cin >> t;
if (s2 == "take") {
if (now[t].empty()) {
mp[s1] = val[t];
}
else {
mp[s1] = now[t].top();
now[t].pop();
}
}
if (s2 == "return") {
now[t].push(mp[s1]);
mp[s1] = 0;
}
}
}
ans = ans + mp["left"] + mp["right"];
cout << ans << endl;
}
return 0;
}
2不会
3有点思路,但是没做出来
//4.
找到规律就可以
输入的数据分两种,小于零和大于零的
可以发现:
9 -9 8 -8 7 -7
和
-9 9 -8 8 -7 7
这两种排列是符合题意的
1、针对小于0的数比较多的情况:
使用:-9 9 -8 8 -7 7 -3 -4 -5 -6
2、针对大于0的数比较多的情况:
使用: 9 -9 8 -8 7 -7 3 4 5 6
这么看就比较清晰
答案分两个部分。
100%代码
bool cmp(int a, int b) {
return abs(a) < abs(b);
}
int main() {
//freopen("in.txt", "r", stdin);
int n;
while (cin >> n) {
vector<int>xiao;
vector<int>da;
int t;
for (int i = 0; i < n; i++) {
cin >> t;
if (t > 0) da.emplace_back(t);
else xiao.emplace_back(t);
}
sort(xiao.begin(), xiao.end(), cmp);
sort(da.begin(), da.end(), cmp);
while (xiao.size() && da.size()) {
if (xiao.size() < da.size()) cout << da.back() << ' ' << xiao.back() << ' ';
else cout << xiao.back() << ' ' << da.back() << ' ';
da.pop_back();
xiao.pop_back();
}
if (xiao.size()) for (auto x : xiao) cout << x << ' ';
if (da.size()) for (auto x : da) cout << x << ' ';
cout << endl;
}
return 0;
}
这题目我是真不想做,描述那么长,看完之后发现没用。太蠢了,剩下的一个半小时一个题都没做出来。#网易互娱##笔试题目##C/C++#

