乐狗笔试
麻了,第二题显式输出格式不对,只过了66.67%,第一次在牛客上遇到这种错误。总体来说,比网易简单多了。
第一题,双人互打(100%)
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 输入参数 Ax 角色A的血量,Ay 角色A 的攻击力,Az A的攻击CD,Aw 角色A的恢复力
输入参数 Bx 角***的血量,By 角*** 的攻击力,Bz B的攻击CD,Bw 角***的恢复力
* @param Ax int整型 角色A的血量上限
* @param Ay int整型 角色A的攻击力
* @param Az int整型 A的攻击CD
* @param Aw int整型 角色A的每回合恢复血量值
* @param Bx int整型 角***的血量上限
* @param By int整型 角***的攻击力
* @param Bz int整型 B的攻击CD
* @param Bw int整型 角***的每回合恢复血量值
* @return int整型
*/
int PK(int Ax, int Ay, int Az, int Aw, int Bx, int By, int Bz, int Bw) {
// write code here
int startA = Ax;
int startB = Bx;
///第一轮攻击
Ax -= By;
Bx -= Ay;
++Az;
++Bz;
int curAz = Az, curBz = Bz;
while(Ax > 0 and Bx > 0)
{
int x = min(curAz, curBz);
Ax += Aw * x;
Bx += Bw * x;
curAz -= x;
curBz -= x;
if(curAz == 0)
{
Bx -= Ay;
curAz = Az;
}
if(curBz == 0)
{
Ax -= By;
curBz = Bz;
}
if(Ax <= 0&nbs***bsp;Bx <= 0)break;
if(Ax >= startA and Bx >= startB)break;
}
if(Ax <= 0 and Bx <= 0)return 3;
else if(Ax <= 0)return 2;
else if(Bx <= 0)return 1;
else return 4;
}
}; 第二题,类似于分割字符串(格式不对,只过了66%) class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 给定字符串 和 显示宽度。 按规则返回自动换行处理后的最后一行。 如不能则返回字符串"error"
* @param str string字符串 给定的字符串
* @param width int整型 显示的宽度
* @return string字符串
*/
string getLastLine(string str, int width) {
// write code here
int i = 0;
while(i < str.length() and str[i] == ' ')++i;
if(i == str.length())return "error";
vector<string>res;
while(i < str.length())
{
int j = i;
while(j < str.length() and str[j] != ' ')++j;
if((j - i) * 16 > width)return "error";
res.push_back(str.substr(i, j - i));
string tmp = "";
while(j < str.length() and str[j] == ' '){
++j;
tmp += " ";
}
if(j < str.length())res.push_back(tmp);
i = j;
}
string ans = "";
int t = 0;
while(t < res.size())
{
ans = res[t];
int k = t;
while(k + 2 < res.size() and (ans.length() + res[k + 1].length() + res[k + 2].length()) * 16 <= width)
{
ans += (res[k + 1] + res[k + 2]);
k += 2;
}
t = k + 2;
}
return ans;
}
};
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 抵达铁门时可能的最大生命值,如果不可达,则返回 -1。
* @param cave int整型vector<vector<>> 洞穴的信息
* @param hp int整型 小明的初始生命值
* @return int整型
*/
void dfs(vector<vector<int> >& cave, vector<vector<int> >&dp, int hp, int curx, int cury)
{
if(curx < 0&nbs***bsp;curx >= cave.size()&nbs***bsp;cury < 0&nbs***bsp;cury >= cave[0].size())return;
if(cave[curx][cury] == -1)return;
else if(cave[curx][cury] > 0)
{
hp -= cave[curx][cury];
if(hp <= 0)return;
}
if(hp <= dp[curx][cury])return;
dp[curx][cury] = hp;
if(cave[curx][cury] == -3)return;
dfs(cave, dp, hp, curx + 1, cury);
dfs(cave, dp, hp, curx - 1, cury);
dfs(cave, dp, hp, curx, cury + 1);
dfs(cave, dp, hp, curx, cury - 1);
}
int EscapeFromCave(vector<vector<int> >& cave, int hp) {
// write code here
int curx, cury, outx, outy;
int n = cave.size(), m = cave[0].size();
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < m; ++j)
{
if(cave[i][j] == -2)
{
curx = i;
cury = j;
}
else if(cave[i][j] == -3)
{
outx = i;
outy = j;
}
}
}
vector<vector<int>>dp(n, vector<int>(m, -1));
dfs(cave, dp, hp, curx, cury);
//return dp[curx][cury];
return dp[outx][outy];
}
}; 
查看4道真题和解析
