腾讯4月5日后端开发笔试题题解
第一题:求前n项的和,找规律,注意结果是 long long int
#include <iostream>
#include <vector>
#include <map>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
long long int a, b;
int main() {
ios::sync_with_stdio(false);
//freopen("input.txt", "r", stdin);
cin >> a >> b;
cout << a / (b * 2) * b * b <<endl;
return 0;
} 第二题:求组成K的可能性
使用组合数做好了。
假设A有x个,B需要(K-A*x)/B个
然后判断是否符合条件就行了
然后根据组合数来
#include <iostream>
#include <vector>
#include <map>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
#define mod 1000000007
int K, A, X, B, Y;
long long ans = 0;
int ta = 0, tb, ca, cb;
int comb[1005][1005];
void init(){
for(int i = 0; i < 1001; i ++){
comb[i][0] = comb[i][i] = 1;
for(int j = 1; j < i; j ++){
comb[i][j] = comb[i-1][j] + comb[i-1][j-1];
comb[i][j] %= mod;
}
}
}
bool check(int ta, int tb) {
if (ta % A != 0) return false;
if (tb % B != 0) return false;
if (ta / A > X) return false;
if (tb / B > Y) return false;
return true;
}
int get(int a, int b) {
int ca = a / A;
int cb = b / B;
return comb[X][ca] * comb[Y][cb];
}
int main() {
ios::sync_with_stdio(false);
//freopen("input.txt", "r", stdin);
init();
cin >> K >> A >> X >> B >> Y;
while(ta <= K) {
tb = K - ta;
if (check(ta, tb)) {
ans += get(ta, tb);
ans %= mod;
}
ta += A;
}
cout << ans << endl;
return 0;
} 第三题:求完成最多的工作和最高的分数
对工作进行排序,优先选择分数最高的工作,每次挑刚好能够完成工作的机器好了,模拟一遍。(我也不知道这样做对不对,反正是所有样例都过了)注意:结果也是 long long int
#include <iostream>
#include <vector>
#include <map>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
int n, m;
pair<int, int> xy[100005];
pair<int, int> zw[100005];
vector<int> jq[105];
bool vis[100005];
long long int rc, rs;
bool cmp2(pair<int, int> a, pair<int, int> b) { // 任务
int sa = 200 * a.first + 3 * a.second;
int sb = 200 * b.first + 3 * b.second;
if (sa != sb) return sa > sb;
if (a.second != b.second) return a.second > b.second;
return a.first > b.first;
//return (200 * a.first + 3 * a.second) > (200 * b.first + 3 * b.second);
}
bool solve(int le, int ti) {
vector<int>::iterator it;
for (int i = le; i <= 100; ++i) {
it = lower_bound(jq[i].begin(), jq[i].end(), ti);
if (it != jq[i].end()) {
jq[i].erase(it);
return true;
}
}
return false;
}
int main() {
ios::sync_with_stdio(false);
//freopen("input.txt", "r", stdin);
cin >> n >> m;
// 机器
for (int i = 0; i < n; ++i) {
cin >> xy[i].first >> xy[i].second;
jq[xy[i].second].push_back(xy[i].first);
}
// 任务
for (int i = 0; i < m; ++i) {
cin >> zw[i].first >> zw[i].second;
}
sort(zw, zw + m, cmp2);
for(int i = 0; i <= 100; ++i) {
sort(jq[i].begin(), jq[i].end());
}
rc = 0;
rs = 0;
for (int i = 0; i < m; ++i) {
int le = zw[i].second;
if (solve(le, zw[i].first)) {
rs += zw[i].first * 200 + zw[i].second * 3;
rc ++;
}
}
cout << rc << " " << rs << endl;
return 0;
} 最后给牛课报一个bug,做完题目还有半小时,写好了题解之后发现我还没有交卷,被提示跳出页面一次。。
就是最开始的那个选择题和编程题类型和题数的那个页面。那个页面应该不用算跳出吧,因为都看不到题目。
