2022.9.20思科笔试c++
选择题蒙的就不说了
一开始有想到用c++的list但后面还是直接拿vector做了,大概就是用另一个数组记录当前位置有没有被访问过,然后每次从输入的数组中找到最大的下标然后左右两边分别看位置有没有被访问过,没有的就改成a或者b,直到下标越界或者修改的次数为0,然后如果左右挑选次数为0的话就特判一下
基本就纯模拟,大概是这么写的。
int main() { int n, m; cin >> n >> m; vector<int> players(n); vector<char> res(n, '*'); string ans; for ( int i = 0; i < n; i++ ) { int a; cin >> a; players[i] = a; } int cnt = n; bool turn = true; while ( cnt ) { auto iter = max_elmement(players.begin(), players.end()); int pos = iter - players.begin(); players[pos] = -1; cnt--; int q = m; if ( turn ) { res[pos] = 'A'; if ( m == 0 ) { turn = !turn; continue; } for ( int i = pos - 1; i >= 0; i-- ) { if (res[i] == '*') { res[i] = 'A'; player[i] = -1; if (--q == 0) break; } } q = m; for ( int i = pos + 1; i < n; i++ ) { if (res[i] == '*') { res[i] = 'A'; player[i] = -1; if (--q == 0) break; } } } else { res[pos] = 'B'; if ( m == 0 ) { turn = !turn; continue; } for ( int i = pos - 1; i >= 0; i-- ) { if (res[i] == '*') { res[i] = 'B'; player[i] = -1; if (--q == 0) break; } } q = m; for ( int i = pos + 1; i < n; i++ ) { if (res[i] == '*') { res[i] = 'B'; player[i] = -1; if (--q == 0) break; } } } turn = !turn; } for ( auto ch : ans ) ans += ch; cout << ans << endl; }