# 网易2019校招笔试题

## 1. 丰收

### 代码：

#include <bits/stdc++.h>

using namespace std;

int sum[100005];

int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
sum[i] = sum[i - 1] + a;
}
int q;
scanf("%d", &q);
while (q--) {
int d;
scanf("%d", &d);
int pos = lower_bound(sum, sum + n + 1, d) - sum;
printf("%d\n", pos);
}
return 0;
}

## 2. 瞌睡

### 代码：

#include <bits/stdc++.h>
using namespace std;

int main() {
int n, k;
cin >> n >> k;
vector<int> a(n), t(n);
for (int i = 0; i < n; i++)
cin >> a[i];
int now = 0;
for (int i = 0; i < n; i++)
cin >> t[i], now += t[i] * a[i];
int res = now;
for (int i = 0; i < n;) {
now += (!t[i]) * a[i];
if (++i >= k) {
res = max(res, now);
now -= (!t[i - k]) * a[i - k];
}
}
cout << res << endl;
return 0;
}

# 今日头条2019校招笔试题

## 1. 求矩形的最大连通域

### 思路：

bfs，参考leetcode 200. Number of Islands。要注意的是输入，用逗号隔开，解决方法是每次读取一个数后，都调用cin.get()来清除逗号

### 代码：

void bfs(vector<vector<int>>& grid, int i, int j, int& num)

{

if (i<0 || i>grid.size() - 1 || j<0 || j>grid[0].size() - 1 || grid[i][j] == 0)

return;

grid[i][j] = 0;

++num;

bfs(grid, i - 1, j, num);

bfs(grid, i + 1, j, num);

bfs(grid, i, j - 1, num);

bfs(grid, i, j + 1, num);

bfs(grid, i - 1, j - 1, num);

bfs(grid, i - 1, j + 1, num);

bfs(grid, i + 1, j - 1, num);

bfs(grid, i + 1, j + 1, num);

return;

}

int main()

{

int M, N;

cin >> M;

cin.get();

cin >> N;

vector<vector<int> > grid(M, vector<int>(N, 0));

for (int i = 0; i < M; ++i)

{

for (int j = 0; j < N; ++j)

{

cin >> grid[i][j];

cin.get();

}

}

int num = 0, maxNum = 0;

for (int i = 0; i != M; ++i)

{

for (int j = 0; j != N; ++j)

if (grid[i][j] == 1)

{

int tmpMax = 0;

bfs(grid, i, j, tmpMax);

++num;

if (tmpMax > maxNum)

maxNum = tmpMax;

}

}

cout << num << "," << maxNum << endl;

return 0;

}


## 2. 区间合并

### 代码：

#include <string>

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

struct Interval {

long long start;

long long end;

Interval() : start(0), end(0) {}

Interval(long long s, long long e) : start(s), end(e) {}

};

bool com(const Interval& a, const Interval& b)

{

if (a.start != b.start)

return a.start < b.start;

if (a.end != b.end)

return a.end < b.end;

return false;

}

void SplitString(const string& s, vector<string>& v, const string& c)

{

string::size_type pos1, pos2;

pos2 = s.find(c);

pos1 = 0;

while (string::npos != pos2)

{

v.push_back(s.substr(pos1, pos2 - pos1));

pos1 = pos2 + c.size();

pos2 = s.find(c, pos1);

}

if (pos1 != s.length())

v.push_back(s.substr(pos1));

}

int main()

{

int m;

cin >> m;

vector<Interval> intervals;

for (int i = 0; i < m; ++i)

{

string s;

cin >> s;

vector<string> v;

SplitString(s, v, ";");

int num = v.size();

for (int j = 0; j < num; ++j)

{

vector<string> v0;

SplitString(v[j], v0, ",");

Interval x;

x.start = stoi(v0[0]);

x.end = stoi(v0[1]);

intervals.push_back(x);

}

}

sort(intervals.begin(), intervals.end(), com);

long long len = intervals.size();

vector<Interval> res;

long long start(0);

long long end(0);

for (long long i = 0; i < len;)

{

start = intervals[i].start;

end = intervals[i].end;

long long j = i + 1;

for (; j < len; ++j)

{

if (end < intervals[j].start)

break;

end = max(end, intervals[j].end);

}

res.push_back(Interval(start, end));

i = j;

}

long long len2 = res.size();

for (long long i = 0; i < len2; ++i)

{

cout << res[i].start << ',' << res[i].end;

if (i != len2 - 1)

cout << ';';

}

}


# 搜狐2019校招笔试题

## 1. 亲密子串

### 代码：

class Solution {
public:
bool buddyStrings(string A, string B) {
if(A.empty() || B.empty()){
return false;
}
if(A.size() != B.size()){
return false;
}
vector<int> index;
set<char> seen;
bool res1=false;

for(int i=0; i<A.size(); i++){
if(seen.count(A[i])){
res1 = true;
}
else{
seen.insert(A[i]);
}

if(A[i] != B[i]){
index.push_back(i);
}
}

if(index.empty()){
return res1;
}
else{
if(index.size() != 2){
return false;
}
return (A[index[0]] == B[index[1]] && A[index[1]] == B[index[0]]);
}
}
};

## 2. 计算移动次数

### 代码：

class Solution {
public:
/**
* @param target: the destination
* @return: the minimum number of steps
*/
int reachNumber(int target) {
// Write your code here
target = abs(target);

int step = 1, pos = 0;
while (pos < target) {
pos += step;
step++;
}
step--;
if (pos == target) return step;
pos -= target;
if (pos % 2 == 0) {
return step;
} else if ((step + 1) % 2 == 1) {
return step + 1;
} else {
return step + 2;
}
}
};

## 3. 比较版本号大小

### 代码：

class Solution {
public:
int compareVersion(string version1, string version2) {
int n1 = version1.size(), n2 = version2.size();
int i = 0, j = 0, d1 = 0, d2 = 0;
while (i < n1 || j < n2) {
while (i < n1 && version1[i] != '.') {
d1 = d1 * 10 + version1[i++] - '0';
}
while (j < n2 && version2[j] != '.') {
d2 = d2 * 10 + version2[j++] - '0';
}
if (d1 > d2) return 1;
else if (d1 < d2) return -1;
d1 = d2 = 0;
++i; ++j;
}
return 0;
}
};

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120