8.20雷火游戏客户端笔试 过了2.9个题 贴下代码
第一题 签到题 猜数字
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> nums(n);
for(int i = 0; i < n; i++){
cin >> nums[i];
}
int ans = nums[n - 1];
int Range_min = 0;
int Range_max = 1001;
for(int i = 0; i < n; i++){
if(nums[i] <= Range_min || nums[i] >= Range_max){
cout << "Are you kidding me?" << endl;
continue;
}
if(nums[i] < ans){
cout << "It's too small, please keep guessing!" << endl;
Range_min = nums[i];
}
else if(nums[i] > ans){
cout << "It's too big, please keep guessing!" << endl;
Range_max = nums[i];
}
else{
cout << "Congratulations! You guessed it right!" << endl;
}
}
}
第二题 把A和B插入矩阵里 求期望 直接硬暴力做
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int n, m ,c;
double d;
int Anum = 0;
int Bnum = 0;
vector<vector<char>> matrix(6, vector<char>(6));
int solutionCount;
double ans;
vector<vector<int>> dir = {{1,0},{1,1},{1, -1}, {0, 1}, {0, -1},{-1,0}, {-1, 1},{-1,-1}};
void cal(){
double result = 0;
for(int row = 0; row < n; row++){
for(int col = 0; col < m; col++){
if(matrix[row][col] == 'B'){
int count = 0;
for(int k = 0; k < 8; k++){
int newrow = row + dir[k][0];
int newcol = col + dir[k][1];
if(newrow >= 0 && newrow < n && newcol >= 0 && newcol < m){
if(matrix[newrow][newcol] == 'A') count++;
}
}
double damage = c;
int i = 0;
while(i < count){
damage *= d;
i++;
}
result += damage;
}
}
}
ans += result;
solutionCount++;
}
void dfs(int row, int col, int ACount, int BCount){
if(ACount > 0){
matrix[row][col] = 'A';
if(col < m - 1){
dfs(row, col + 1, ACount - 1, BCount);
}
else{
dfs(row + 1, 0, ACount - 1, BCount);
}
}
if(BCount > 0){
matrix[row][col] = 'B';
if(col < m - 1){
dfs(row, col + 1, ACount, BCount - 1);
}
else{
dfs(row + 1, 0, ACount, BCount - 1);
}
}
if(ACount == 0 && BCount == 0){
cal();
}
}
int main(){
cin >> n >> m >> c >> d;
double bestAns = 0;
int bestAnum = 0;
int bestBnum = 0;
int All = n * m;
for(int i = 0; i <= All; i++){
Anum = i;
Bnum = All - i;
ans = 0;
solutionCount = 0;
dfs(0, 0, Anum, Bnum);
ans /= (1.0 * solutionCount);
if(ans > bestAns){
bestAns = ans;
bestAnum = Anum;
bestBnum = Bnum;
}
}
cout << bestAnum << " " << bestBnum << " ";
cout << fixed << setprecision(1) << bestAns << endl;
}
第三题 求匹配队伍,回溯, 记录失败状态,但是A了90 不知道哪里出bug了
#include <iostream>
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
int n, m, a, b;
struct Team{
int id;
vector<int> players;
};
vector<bool> TeamHasA;
vector<bool> TeamHasB;
unordered_set<string> st;
bool backtrack(vector<Team>& teams, vector<bool>& visited, int start, int currentSize, vector<int>& currentTeam, bool &hasA, bool& hasB){
if(hasA && hasB && currentSize == m){
for(int id : currentTeam){
cout << id + 1 << " ";
}
cout << endl;
return true;
}
//判断是否是失败状态
vector<int> all1;
for(int id: currentTeam){
for(int num : teams[id].players){
all1.push_back(num);
}
}
sort(all1.begin(),all1.end());
string s;
for(auto num: all1){
s += to_string(num);
}
if(st.find(s) != st.end()) return false;
//
for(size_t i = start; i < teams.size(); i++){
if(visited[i] == true) continue;
if(!visited[i] && currentSize + (int)teams[i].players.size() <= m){
bool teamHasA = TeamHasA[i];
bool teamHasB = TeamHasB[i];
if((hasA && teamHasA) || (hasB && teamHasB)) continue;
visited[i] = true;
currentTeam.push_back(i);
bool newHasA = hasA || teamHasA;
bool newHasB = hasB || teamHasB;
if(backtrack(teams, visited, i + 1, currentSize + (int)teams[i].players.size(),currentTeam, newHasA, newHasB)){
return true;
}
//记录失败状态
vector<int> all;
for(int id: currentTeam){
for(int num : teams[id].players){
all.push_back(num);
}
}
sort(all.begin(),all.end());
string s;
for(auto num: all){
s += to_string(num);
}
st.insert(s);
//
visited[i] = false;
currentTeam.pop_back();
}
}
return false;
}
int main() {
cin >> n >> m >> a >> b;
vector<Team> teams;
vector<bool> matched(n, false);
TeamHasA.resize(n);
TeamHasB.resize(n);
for(int i = 0; i < n; i++){
Team team;
int t;
cin >> t;
team.id = i;
for(int j = 0; j < t; j++){
int player;
cin >> player;
if(player == a) TeamHasA[i] = true;
if(player == b) TeamHasB[i] = true;
team.players.push_back(player);
}
teams.push_back(team);
}
for(int i = 0 ;i < n; i++){
if(matched[i] == true) continue;
vector<int> cur;
bool a = false;
bool b = false;
backtrack(teams, matched,i,0, cur, a, b);
}
return 0;
}
#秋招#

