拼多多 笔试
第一题:
#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; }