【笔试】百度编程题

来源:互联网 发布:山寨币交易平台源码 编辑:程序博客网 时间:2024/05/16 09:55

百度2017春招笔试真题编程题集合

1、找第三便宜的帽子。

这里写图片描述

#include <iostream>#include <vector>#include <algorithm>using namespace std;int main() {    int n;    cin >> n;    vector<int> result;    int price;    for (int i = 0; i < n; i++) {        cin >> price;        if (price > 1000 || price < 0) {            continue;//只存入在该范围内的价格,其余丢弃        }        result.push_back(price);//最终的size一定≤n    }    sort(result.begin(), result.end());    if (result.size() < 3 || result.size() > 50) {        cout << -1 << endl;        return 0;    }    else {        int count = 1;        for (int i = 1; i < result.size(); i++) {            if (result[i] != result[i-1]) {                count++;            }            if (count == 3) {                cout << result[i] << endl;                return 0;            }        }        cout << -1 << endl;//输入的都是同一个价格        return 0;    }}
//丢进set去重#include <bits/stdc++.h>using namespace std;int n;set<int> S;int main() {    int n;    cin >> n;    for(int i = 0; i < n; i++) {        int x; cin >> x;        S.insert(x);    }    int cnt = 0;    for(auto &x : S) {        cnt++;        if(cnt == 3) {            cout << x << endl;            break;        }    }    if(cnt < 3) cout << -1 << endl;    return 0;}

2、找颜色相同/不同的面积最大的三角形。

这里写图片描述

#include <iostream>#include <vector>#include <math.h>#include <iomanip>using namespace std;struct node {    double x;    double y;    double z;    char sample;    node() {        x = 0;        y = 0;        z = 0;        sample = NULL;    }};double getarea(node a, node b, node c) {    double l1, l2, l3;    l1 = sqrt(pow((a.x - b.x), 2) + pow((a.y - b.y), 2) + pow((a.z - b.z), 2));    l2 = sqrt(pow((c.x - b.x), 2) + pow((c.y - b.y), 2) + pow((c.z - b.z), 2));    l3 = sqrt(pow((a.x - c.x), 2) + pow((a.y - c.y), 2) + pow((a.z - c.z), 2));    double p = (l1 + l2 + l3) / 2.0;    double s = sqrt(p * (p - l1) *(p - l2) * (p - l3));    return s;}int main() {    int n;    cin >> n;    int a, b, c;    char sample;    vector<node> result;    double area = 0.00000;    for (int i = 0; i < n; i++) {        cin >> sample >> a >> b >> c;        node temp;        temp.x = a;        temp.y = b;        temp.z = c;        temp.sample = sample;        result.push_back(temp);    }    for (int i = 0; i < result.size() - 2; i++) {        for (int j = i + 1; j < result.size(); j++) {            for (int k = j + 1; k < result.size(); k++) {                if (result[i].sample == result[j].sample && result[j].sample == result[k].sample) {                    if (area < getarea(result[i], result[j], result[k])) {                        area = getarea(result[i], result[j], result[k]);                    }                }                if (result[i].sample != result[j].sample && result[j].sample != result[k].sample && result[i].sample != result[k].sample) {                    if (area < getarea(result[i], result[j], result[k])) {                        area = getarea(result[i], result[j], result[k]);                    }                }            }        }    }    cout << fixed << setprecision(5) << area << endl;    return 0;}
//暴力枚举,统计就好了。这里需要计算一个三角形面积,用叉积或者海伦公式都可以。char type[55];double x[55], y[55], z[55];int main() {    int n;    cin >> n;    for(int i = 0; i < n; i++) {        cin >> type[i] >> x[i] >> y[i] >> z[i];    }    double ans = 0.0;    for(int i = 0; i < n; i++) {        for(int j = 0; j < i; j++) {            for(int k = 0; k < j; k++) {                int ok = 0;                if(type[i] == type[j]) if(type[i] == type[k]) ok = 1;                if(type[i] != type[j]) if(type[i] != type[k]) if(type[j] != type[k]) ok = 1;                if(!ok) continue;                double ux = x[j] - x[i], uy = y[j] - y[i], uz = z[j] - z[i];                double vx = x[k] - x[i], vy = y[k] - y[i], vz = z[k] - z[i];                double area = sqrt( mul(ux * vy - vx * uy) + mul(uy * vz - vy * uz) + mul(uz * vx - ux * vz) );                ans = ans > area * 0.5 ? ans : area * 0.5;            }        }    }    printf("%.5lf\n", ans);    return 0;}

3、正确的不等式排列方案。

这里写图片描述

动态规划

dp[i][j]:表示前i个数字构成的数列中,恰好有j个“<”符号的方案个数。(“>”就有i-j-1个)
当已经有i-1个数时,此时插入元素i,那i将是最大的数,同时新增了一个坑位用于放不等式符号(来连接i与之前的不等式),那么将分两种情况,坑位放“<”或者“>”。
(1)放“<”:说明之前的方案数为dp[i - 1][j - 1],那么问题变成——如何将“< i”插入原队伍?(注意而不是“i<”,因为i是最大的)
答案:对于每一种原队伍,要么放在最右边,要么插入右边是“>”号的地方,故1+(i-j-1) = i-j。
(2)放“>”:说明之前的方案数为dp[i - 1][j ],那么问题变成——如何将“i >”插入原队伍?
答案:对于每一种原队伍,要么放在最左边,要么插入左边是“<”号的地方,故1+j。
dp[i][j] = dp[i - 1][j - 1] * (i - j) + dp[i - 1][j] * (j + 1)

#include <bits/stdc++.h>using namespace std;int n, k, ans;int dp[1005][1005];int main() {    cin >> n >> k;    for(int i = 1; i <= n; i++)        dp[i][0] = 1;//没有<号,只有降序排列一种方案    for(int i = 2; i <= n; i++)        for(int j = 1; j <= k; j++)            dp[i][j] = (dp[i - 1][j - 1] * (i - j) + dp[i - 1][j] * (j + 1)) % 2017;    cout << dp[n][k] % 2017 << endl;    return 0;}
0 0