拼多多 笔试

第一题:
#include <bits/stdc++.h>
int main(){
    int n;
    priority_queue<int> q;
    priority_queue<int> q1;
    while(true){
        int x;
        cin >> x;
        if (x %2 == 0){
            q.push(x);
        }
        else {
            q1.push(x);
        }
        char c = getchar();
        if (c == ','){
            continue;
        }
        else if(c == ';'){
            cin >> n;
            break;
        }
    }
    vector<int> res;
    while(!q.empty()){
        auto c = q.top();
        res.push_back(c);
        q.pop();
    }
    while(!q1.empty()){
        auto c = q1.top();
        res.push_back(c);
        q1.pop();
    }
    cout<<res[0];
    for(int i=1;i<n;i++){
        cout<<","<<res[i];
    }
    return 0;
}
第二题:
#include <bits/stdc++.h>
using namespace std;
 
void dfs(vector<char>& res_one, vector<string>& res, string s1, string s2, string& s1_new, int N)
{
    if (s1_new == s2 && s1.size() == 0 && res_one.size() == N)
    {
        string ans;
        for (int i = 0; i < res_one.size(); i++)
        {
            ans += res_one[i];
        }
        res.push_back(ans);
        return;
    }

    while (s1.size())
    {
        char tmp = s1[0];
        s1 = s1.substr(1);

        res_one.push_back('d');
        dfs(res_one, res, s1, s2, s1_new, N);
        res_one.pop_back();

        res_one.push_back('l');
        s1_new = tmp + s1_new;
        dfs(res_one, res, s1, s2, s1_new, N);
        s1_new = s1_new.substr(1);
        res_one.pop_back();

        res_one.push_back('r');
        s1_new =  s1_new + tmp;
        dfs(res_one, res, s1, s2, s1_new, N);
        s1_new = s1_new.substr(0, s1_new.length() - 1);
        res_one.pop_back();

    }

    return;
}


int main()
{
    int T;
    cin >> T;

    string s1;
    string s2;
    string s1_new;

    while (T--)
    {
        cin >> s1;
        cin >> s2;
        vector<char> res_one;
        vector<string> res;

        dfs(res_one, res, s1, s2, s1_new, s1.length());
        
        sort(res.begin(), res.end());

        cout << "{" << endl;

        for (int i = 0; i < res.size(); i++)
        {
            for (int j = 0; j < s1.length(); j++)
            {
                cout << res[i][j] << " ";

                
            }
            cout << "\n";
        }

        cout << "}" << endl;

    }

    return 0;
}
第三题:
#include <bits/stdc++.h>
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mem(a,x)  memset(a,x,sizeof a)

using namespace std;
const int maxN=50;
const int maxK=50;
//int K[maxk+5];
double P[maxK+5];
int cnt[maxK+5];
int sum[maxK+5];
int n,maxk;

double solve(int x)
{
//    num_lt=sum[x-1];
//    num_gt=sum[maxk]-sum[x];
//    num_eq=cnt[x];
    int num_ge=sum[maxk]-sum[x-1];
    if(!num_ge) return 0;

    double p=1;

    for(int i=x;i<=maxk;i++) if(cnt[i])
    {
           p*= pow(1.0*x/i, cnt[i]);
    }

    double p2=1;
    for(int i=x;i<=maxk;i++) if(cnt[i])
    {
         p2*= pow(1.0*(x-1)/i,cnt[i] );
    }

    return  p-p2;

}
int main()
{
    std::ios::sync_with_stdio(false);
    int x;
    while(cin>>n)
    {
        mem(cnt,0);maxk=0;

        for1(i,n)  {cin>>x;maxk=max(maxk,x);cnt[x]++;}

        sum[0]=0; for1(i,maxk)  sum[i]=sum[i-1]+cnt[i];


        for1(i,maxk)  P[i]=solve(i);

        double ans=0; for1(i,maxk)  ans+=P[i]*i; printf("%.2f\n",ans);

    }

    return 0;
}
第四题:
上次少写了一行,造成误会抱歉
#include <bits/stdc++.h>
using namespace std;
int main(){
    int lo = 1, hi = m * n + 1;
    k = n*m-k+1;
    int mid, count;
    while (lo < hi) {
        mid = lo + (hi - lo) / 2;
        count = 0;
        for (int i = 1; i <= m; i++) {
            count += (mid/i > n ? n : mid/i);
        }
        if (count >= k) hi = mid;
        else lo = mid + 1;
    }
    cout<<lo<<endl;
    return 0;
}



#拼多多##笔试题目##题解#
全部评论
点赞 回复 分享
发布于 2019-09-01 19:15
第二题小梅那个是不是抽出的牌中可能有重复的?
点赞 回复 分享
发布于 2019-09-01 19:16
为啥一些符号不见了。。。。。
点赞 回复 分享
发布于 2019-09-01 19:30
点赞 回复 分享
发布于 2019-09-01 20:10
第四题是怎么回事,这个不是第k小的吗,不是第k大的啊
点赞 回复 分享
发布于 2019-09-01 22:36
很多符号不见了
点赞 回复 分享
发布于 2019-09-02 07:20
四题都是ac代码吗??
点赞 回复 分享
发布于 2019-09-02 08:58
膜拜大佬,同时建议重发一下代码,很多符号没显示
点赞 回复 分享
发布于 2019-09-02 10:53
你好,请问能不能解释一下第二题你的解题思路啊?谢谢分享
点赞 回复 分享
发布于 2019-09-02 19:19
hi为什么初始化要加1
点赞 回复 分享
发布于 2019-09-06 11:38
第二题为啥要while (s1.size()),第一次执行里面三种操作,调用下一层dfs可以理解,但后面的操作是在干啥。。?
点赞 回复 分享
发布于 2019-09-07 16:40

相关推荐

09-03 14:50
长春大学 Java
牛客407945238号:这环境…怎么看都像是低配版的电诈园区
点赞 评论 收藏
分享
4 38 评论
分享
牛客网
牛客企业服务