# 2021春招各个大厂笔试题（百度，滴滴，携程，友塔游戏等）

## 笔试题目描述忘记了，请见谅，有错误可以指出，有时间改

### 滴滴(通过笔试)

``````//滴滴笔试编程第一题 通过100%
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

struct node {
int a,b;
}s[50010];

int cmp(node A,node B) {
return A.a<B.a;
}

int main() {
int n;
cin >> n;
int ans = 0;
for (int i=0;i<n;i++)
cin >> s[i].a >> s[i].b;
sort(s,s+n,cmp);
ans = s[0].a+s[0].b;
for (int i=1;i<n;i++) {
if (ans>s[i].a)
ans += s[i].b;
else if (ans==s[i].a)
ans += s[i].b;
else if (ans<s[i].a)
ans = s[i].a+s[i].b;
}

cout << ans << endl;

return 0;
}
//滴滴笔试编程第二题  通过18%
#include <iostream>

using namespace std;

int main() {
int n,x;
int a[100000+10];
int b[100000+10];
cin >> n >> x;
int m = 0;
for (int i=0;i<n;i++){
cin >> a[i];
m = max(m,a[i]);
}
b[0] = 1;
for (int i=1;i<n;i++){
b[i] = b[i-1]+x;
}
if (m<b[n-1])
m = b[n-1];
int ans = 0;
int ans1 = 0x3f3f3f;
int rec = 2;
while (b[n-1]<=m) {
ans = 0;
for (int i=0;i<n;i++){
if (a[i]!=b[i])
ans++;
}
b[0] = rec;
for (int j=1;j<n;j++) {
b[j] = b[j-1]+x;
}
rec++;
ans1 = min(ans1,ans);
}
cout << ans1 << endl;

return 0;
}

### 好未来

``````//好未来 1 ac
#include <bits/stdc++.h>

using namespace std;

bool validBraces(string str) {
// write code here
char a[1000];
int top = -1;
for (int i=0;i<str.size();i++) {
if (str[i]=='(') {
a[++top] = '(';
} else if (str[i]==')') {
if (a[top]=='(')
top--;
else
a[++top] = ')';
}
if (str[i]=='{') {
a[++top] = '{';
} else if (str[i]=='}') {
if (a[top]=='{')
top--;
else
a[++top] = '}';
}
if (str[i]=='[') {
a[++top] = '[';
} else if (str[i]==']') {
if (a[top]=='[')
top--;
else
a[++top] = ']';
}
}
if (top==-1)
return true;
else
return false;
}

int main() {
cout << validBraces("(){}[]");

return 0;
}

//好未来编程题2  ac

#include <bits/stdc++.h>

using namespace std;

int insert(string &str,int index) {
str += 'c';
int i;
for (i=str.size()-2;i>=index;i--) {
str[i+1] = str[i];
}
str[i+1] = ',';

return i;
}

string convert(string str) {
// write code here
if (str.size()<=3)
return str;
else {
int len = str.size();
int index;
index = len-3;
while (index>0) {
index = insert(str,index);
index = index-2;
}
}
return str;
}

int main() {
cout << convert("12345678");

return 0;
}

### 百度

``````//百度 1 通过70%
#include <bits/stdc++.h>

using namespace std;

int a[2000000+10];

int main() {
int n,p,q;
cin >> n >> p;
q = 100-p;
for (int i=0;i<2*n;i++) {
cin >> a[i];
}
sort(a,a+2*n);
int ans = 0;
if (p>50) {
for (int i=0;i<n;i++) {
ans += q*a[i];
}
for (int i=n;i<2*n;i++) {
ans += p*a[i];
}
} else {
for (int i=0;i<2*n;i++) {
if ((i+1)%2) {
ans += a[i]*q;
}else {
ans += a[i]*p;
}
}
}
if (ans%100==0) {
cout << ans/100 << endl;
} else {
printf("%d%\n",ans);
}

return 0;
}

//百度2
#include <bits/stdc++.h>

using namespace std;

int main() {
int n,m,p;
int a[5000][2];
int rec[110];
memset(rec,0.sizeof(rec));
for (int i=0;i<m;i++) {
cin >> a[i][0] >> a[i][1];
rec[a[i][0]]++;
}

return 0;
}

### 神策数据

`````` //神策数据5 ac
#include <bits/stdc++.h>

using namespace std;

void reverse(string &str) {
int l = 0;
int r = str.size()-1;
char rec;
while (l<r) {
rec = str[r];
str[r] = str[l];
str[l] = rec;
r--;
l++;
}
}
bool includes(string s1, string s2) {
// write code here
int len1 = s1.size();
int len2 = s2.size();
string s3;
if (len2>len1) {
;
} else {
s3 = s2;
reverse(s2);
if (len1==len2) {
int i;
for (i=0;i<len1;i++) {
if (s1[i]==s2[i])
;else
;
}
if (i==len1)
return true;
}else {
int j = 0;
int i = 0;
while (i!=len1&&j!=len2) {
if (s1[i]==s2[j]) {
i++;
j++;
}else {
i++;
}
}
if (j==len2)
return true;
else
;
}
}
if (len1==len2) {
int i;
for (i=0;i<len1;i++) {
if (s1[i]==s3[i])
;else
;
}
if (i==len1)
return true;
}else {
int j = 0;
int i = 0;
while (i!=len1&&j!=len2) {
if (s1[i]==s3[j]) {
i++;
j++;
}else {
i++;
}
}
if (j==len2)
return true;
else
;
}

return false;
}

int main() {
string s1,s2;
cin >> s1 >> s2;
bool ans = includes(s1,s2);
cout << ans << endl;

return 0;
}

神策数据

#include <bits/stdc++.h>

using namespace std;

int greatestCommonpisor(int a, int b) {
// write code here
int rec;
if (a>b) {
rec = a;
a = b;
b = rec;
}
if (b%a==0)
return a;
else {
for (int i=((int)sqrt(a)+1);i>0;i--) {
int an
if (a%i==0&&b%i==0)
return i;
}
}
return 1;
}

int main() {
int a,b;
cin >> a >> b;
cout << greatestCommonpisor(a,b) << endl;

return 0;
}
//神策数据
#include <bits/stdc++.h>

using namespace std;

int getTimes(vector<int>& array, int k) {
// write code here
sort(array.begin(),array.end());
//int j = 1;
int start = array[array.size()-1];
k--;
int ans = 0;
for (int i=array.size()-2;i>-1;i--){
if (k==0) {
if (array[i]==start)
ans++;
} else {
if (array[i]>start) {
k--;
start = array[i];
}
}
}
return ans;
}

int main() {
vector<int>a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(4);
a.push_back(4);
a.push_back(4);
cout << getTimes(a,1) << endl;
return 0;
}

### 涂鸦

``````//涂鸦  1
#include <bits/stdc++.h>

using namespace std;

vector<vector<int> > merge(vector<vector<int> >& intervals) {
// write code here
vector<vector<int> > ans;
int start = intervals[0][0];
int end = intervals[0][1];
int rec = 0;
for (int i=1;i<intervals.size();i++) {
if (intervals[i][0]<=end) {
end = intervals[i][1];
} else {
ans[rec].push_back(start);
ans[rec].push_back(end);
start = intervals[i][0];
end = intervals[i][1];
rec++;
}
}

return ans;
}

int main() {
vector<vector<int> > inter;
inter[0][0] = 1;

return 0;
}

### 携程（笔试通过)

``````//携程 1 83%
#include <bits/stdc++.h>

using namespace std;

int main() {
while (1) {
string str;
vector<int> a;
int data;
cin >> str;
for (int i=1;i<str.size();i++) {
if (str[i]=='-') {
a.push_back(-(str[i+1]-'0'));
i++;
} else if ('0'<=str[i]&&str[i]<='9') {
a.push_back(str[i]-'0');
}
}
if (a.size()==0){
printf("[]\n");
return 0;
}
int now = 0;
int max1 = -0x3f3f3f;
int start,end;
int recS;
int recMax = -10;
for (int i=0;i<a.size();i++) {
if (a[i] > recMax) {
start = end = i;
recMax = a[i];
}
}
if (recMax>0) {
recS = 0;
for (int i=0;i<a.size();i++) {
now += a[i];
if (now<0) {
now = 0;
recS = i+1;
}
if (now>max1) {
start = recS;
end = i;
}
max1 = max(now,max1);
}
}
printf("[%d,%d]\n",start,end);
}

return 0;
}

//携程2

#include <bits/stdc++.h>

using namespace std;

int main() {
int k,n;
vector<int> n1;
vector<vector<int> > time;
scanf("%d%d",&k,&n);
int rec;
for (int i=0;i<n;i++) {
scanf("%d",&rec);
n1.push_back(rec);
}
vector<int> rec1;
for (int i=0;i<k;i++) {
for (int j=0;j<n;j++) {
scanf("%d",&rec);
rec1.push_back(rec);
}
time.push_back(rec1);
rec1.clear();
}
vector<int> minTime;
for (int i=0;i<n;i++) {
int rec = 0x3f3f3f;
for (int j=0;j<k;j++) {
rec = min(rec,time[j][i]);
}
minTime.push_back(rec);
}
vector<int> rec2;
for (int i=0;i<n;i++) {
rec2.push_back(n1[i]*minTime[i]);
}
sort(rec2.begin(),rec2.end());
cout << rec2[0] << endl;

return 0;
}

### 京东

``````// 京东 1

#include <bits/stdc++.h>

using namespace std;

string yi(int num) {
if (num==1)
return "yiyi";
else if (num==2) {
return "eryi";
}
else if (num==3) {
return "sanyi";
}
else if (num==4) {
return "siyi";
}else if (num==5) {
return "wuyi";
}else if (num==6) {
return "liuyi";
}
else if (num==7) {
return "qiyi";
}
else if (num==8) {
return "bayi";
}
else if (num==9) {
return "jiuyi";
}

return "";
}

string judge(int num,int j) {
string rec;
if (num==0) {
return "ling";
}
else if (num==1) {
rec = "yi";
}
else if (num==2) {
rec = "er";
}
else if (num==3) {
rec = "san";
}
else if (num==4) {
rec = "si";
}else if (num==5) {
rec = "wu";
}else if (num==6) {
rec = "liu";
}
else if (num==7) {
rec = "qi";
}
else if (num==8) {
rec = "ba";
}
else if (num==9) {
rec = "jiu";
}
if (j==7)
rec += "qianwan";
else if (j==6) {
rec += "baiwan";
}else if (j==5) {
rec += "shiwan";
}else if (j==4) {
rec += "wan";
}else if (j==3) {
rec += "qian";
}else if (j==2) {
rec += "bai";
}else if (j==1) {
rec += "shi";
}

return rec;
}

string number2Hanyu(int num) {
// write code here
int a[10];
int i=0;
while (num!=0) {
a[i++] = num%10;
num = num/10;
}
string ans = "";
for (int j=(i-1);j>-1;j--) {
if (j==8) {
ans = ans + yi(a[j]);
}else{
ans += judge(a[j],j);
}
}

return ans;
}

int main() {
int num = -1;
cout << (-num) << endl;
while (cin >> num)
cout << number2Hanyu(num) << endl;

return 0;
}

### 博乐科技

``````  //1 博乐科技  通过 90.24%

#include <bits/stdc++.h>

using namespace std;
//
int partition(int arr[],int l,int r) {
int pri = arr[l];
while (l<r) {
while (arr[r]>pri&&l!=r) {
r--;
}
if (l==r)
break;
arr[l] = arr[r];
while (arr[l]<pri&&l!=r) {
l++;
}
if (l==r)
break;
arr[r] = arr[l];
}
arr[l] = pri;

return l;
}

void quickSort(int arr[],int left,int right) {
if (left<right) {
int pri = partition(arr,left,right);
quickSort(arr,left,pri-1);
quickSort(arr,pri+1,right);
}
}

int main() {
int a[5] = {-41,7,20,-46,38};
quickSort(a,0,4);
for (int i=0;i<5;i++) {
cout << a[i] << " ";
}
cout << endl;

return 0;
}

//2 ac
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
*
* 输入字符串， 返回三进制值的字符串
* @param sixString string 16进制数
* @return string
*/
long long int toTwo(string sixString) {
int len = sixString.size();
long long int ans = 0;
int j = 0;
int rec;
for (int i=len-1;i>1;i--) {
if (sixString[i]=='A') {
rec = 10;
}else if (sixString[i]=='B') {
rec = 11;
}else if (sixString[i]=='C') {
rec = 12;
}else if (sixString[i]=='D') {
rec = 13;
}else if (sixString[i]=='E') {
rec = 14;
}else if (sixString[i]=='F') {
rec = 15;
} else {
rec = sixString[i]-'0';
}
ans += rec*(long long)pow(16,j);
j++;
}

return ans;
}

string sixToThree(string sixString) {
write code here
long long int rec;
rec = toTwo(sixString);
string ans = "";
while (rec!=0) {
ans += (char)((rec%3)+'0');
rec = rec/3;
}
int l = 0;
int r = ans.size()-1;
char recC;
while (l<r) {
recC =ans[l];
ans[l] = ans[r];
ans[r] = recC;
l++;
r--;
}
return ans;
}
};

//3

#include <bits/stdc++.h>

using namespace std;

int vis[100][100];
int next[4][2] {1,0,-1,0,0,1,0,-1};
bool check(int x,int y,int gridR,int gridC) {
if (x<0||y<0||x>=gridR||y>=gridC)
return false;
return true;
}

int dfs(int** grid,int x,int y,int lenx,int leny) {
int ans = 0;
if (grid[x][y]) {
ans += 1;
}
for (int i=0;i<4;i++) {
int nX = x+next[i][0];
int nY = y+next[i][1];
if (check(nX,nY,lenx,leny)&&grid[nX][nY]&&!vis[nX][nY]) {
ans += dfs(grid,nX,nY,lenx,leny);
}
}
}

int maxAreaOfIsland(int** grid, int gridRowLen, int gridColLen) {
// write code here
memset(vis,0,sizeof(vis));
int ans = 0;
int ans1 = -2;
int nextX;
int nextY;
for (int i=0;i<gridRowLen;i++) {
for (int j=0;j<gridColLen;j++) {
ans = 0;
if (grid[i][j]&&!vis[i][j]) {
ans += 1;
for (int s=0;s<4;s++) {
nextX = i+next[s][0];
nextY = j+next[s][1];
if (check(nextX,nextY,gridRowLen,gridColLen)&&!vis[nextX][nextY]) {
vis[nextX][nextY] = 1;
ans += dfs(grid,nextX,nextY,gridRowLen,gridColLen);
}
}
}
grid[i][j] = 0;
ans1 = max(ans1,ans);
}
}
return ans1;
}

int main() {
return 0;
}

### 游塔游戏

``````

#include <bits/stdc++.h>

using namespace std;

int main() {
int n;
int a[5000+10];
int t[5000+10][5000+10];
cin >> n;
for (int i=0;i<n;i++) {
cin >> a[i];
}
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++)
cin >> t[i][j];
}
for (int i=0;i<5;i++) {
for (int j=0;j<5;j++) {
if (t[i][j]>0)
t[i][j] += a[i];
}
}
int ans = 0x3f3f3f;
for (int i=0;i<n;i++) {
if (t[i][0]>0&&t[i][n-1]>0) {
ans = min(ans,t[i][0]+t[i][n-1]);
}
}
for (int i=0;i<n;i++) {
if (t[0][i]>0&&t[n-1][i]>0)
ans = min(ans,t[0][i]+t[n-1][i]);
}
cout << ans << endl;

return 0;
}

#include <bits/stdc++.h>

using namespace std;

struct node{
int cost,power;
}a[510];

int cmp(node A,node B) {
return A.power*1.0/A.cost > B.power*1.0/B.cost;
}

int cmp1(node A,node B) {
return A.power > B.power;
}

int main() {
long long n,max_count,max_cost;
cin >> n >> max_count >> max_cost;
for (int i=0;i<n;i++) {
cin >> a[i].cost >> a[i].power;
}
sort(a,a+n,cmp);
long long ans = 0;
for (int i=0;i<max_count;i++)
ans += a[i].power;
sort(a,a+n,cmp1);
long long ans1 = 0;
int j=0;
int i = 0;
while (j!=max_count) {
if ((max_cost-a[i].cost)>0) {
ans1 += a[j].power;
max_cost -= a[j].cost;
j++;
}
i++;
}
ans = max(ans,ans1);
cout << ans << endl;

return 0;
}

#include <bits/stdc++.h>

using namespace std;

int vis[5];

int solve(vector<int> a,int index) {
for (int i=0;i<a.size();i++) {
vis[index] = 1;
for (int i=0;i<4;i++) {
if (i==0) {

}
}
}
}

int main() {
int n;
vector<int> a;
int rec;
long long ans = 0;
while (n--) {
while (~scanf("%d",&rec) {
a.push_back(rec);
}
ans += solve(a,0);
}
cout << ans << endl;

return 0;
}

int maxSubmatrixSum(std::vector<std::vector<int>> matrix,int n, int m) {
int base_sum = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
base_sum += matrix[i][j];
}
}

int result = 0;
for (int i = 0; i + n < matrix.size(); i++) {
if(i  > 0){
for (int y = 0; y < m; y++){
base_sum += matrix[i + n][y] - matrix[i - 1][y];
}
}
int real_sum = base_sum;
if (real_sum  > result) {
result = real_sum;
}
for (int j = 0; j + m < matrix.size(); j++) {
for (int x = 0; x < n; x++) {
real_sum += matrix[x][j + m] - matrix[x][j - 1];
}
if (real_sum > result) {
result = real_sum;
}
}
}

return result;
}

#include <bits/stdc++.h>

using namespace std;

int judge(string a) {
if (a=="+"||a=="-"||a=="*"||a=="/")
return 0;
else
return 1;
}

int toNum(string num) {
int ans = 0;
for (int i=0;i<num.size();i++)
ans = ans*10+(num[i]-'0');
return ans;
}

int evalRPN(vector<string>& tokens) {
int stack[100];
int top = 0;
for (int i=0;i<tokens.size();i++) {
if (judge(tokens[i])) {
int num = toNum(tokens[i]);
stack[top++] = num;
} else {
int next;
int firstNum = stack[--top];
int secondNum = stack[--top];
if (tokens[i]=="*") {
next = firstNum*secondNum;
}
else if (tokens[i]=="-") {
next = secondNum-firstNum;
}
else if (tokens[i]=="+")
next = secondNum+firstNum;
else if (tokens[i]=="/")
next = secondNum/firstNum;
stack[top++] = next;
}
}
return stack[top-1];
}

int main() {
vector<string>a;
a.push_back("2");
a.push_back("1");
a.push_back("+");
a.push_back("3");
a.push_back("*");
int ans = evalRPN(a);
cout << ans << endl;

return 0;
}