基于C++的拼多多算法在線筆試題示例
本文實例講述了基于C++的拼多多算法在線筆試題。分享給大家供大家參考,具體如下:
最近在狼廠實習中,很久沒做題了。秋招第一發(fā), 拼多多。。。 四個簡單題,看到有些人竟然覺得難? 我來降一發(fā)自己的RP,這題目覺得難的,如果你拿到比我好的Offer,我是不服氣的。。
四個題。。。其實我也就寫了40分鐘吧。。不過最后也沒有滿分, 390/400, 第三題不知道為嘛一直有10分過不了。。
更一下, 剛剛好像發(fā)現(xiàn)第三題。。。這個>號, 我寫的是>= ....? 可是我看題目好像是 >= 呀。。。
第一題:
要求時間復雜度O(n), 空間復雜度O(1)。
那么其實答案有兩種情況,最大的三個數(shù)相乘 || 最小的兩個數(shù) * 最大的數(shù)。 時間復雜度O(n),瞬間想到時間復雜度O(n)求k大的經典算法,分治法!
#include <cstdio> #include <iostream> #include <string> #include <algorithm> using namespace std; const int N = 1e6 + 10; long long a[N]; int k; int partition(int l,int r) { while(l != r) { while(a[r] >= a[l] && r > l) r--; if(l == r) break; swap(a[r],a[l]); while(a[l] < a[r] && r > l) l++; if(l < r) swap(a[r],a[l]); } return l; } long long solve(int l,int r) { int now = partition(l,r); if(k < now) return solve(l,now-1); else if(k > now) return solve(now+1,r); else return a[now]; } int main() { int n; while(~scanf("%d", &n)) { for(int i = 0; i < n; ++i) { scanf("%lld", &a[i]); } k = n - 1; long long x1 = solve(0, n-1); k = n - 2; long long x2 = solve(0, n-2); k = n - 3; long long x3 = solve(0, n-3); long long Ans = x1 * x2 * x3; if(n > 3) { k = 0; long long y1 = solve(0, n-1); k = 1; long long y2 = solve(0, n-2); Ans = max(Ans, y1*y2*x1); } printf("%lld\n", Ans); } return 0; }
第二題:
#include <iostream> #include <cstring> #include <string> #include <algorithm> using namespace std; const int N = 1e5 + 10; string c1, c2; int a[N], b[N], r[N]; void solve(int a[], int b[], int la, int lb) { int i, j; for(i = 0; i != N; i++) r[i] = 0; for(i = 0; i != la; i++) { for(j = 0; j != lb; j++) { int k = i + j; r[k] += a[i] * b[j]; while(r[k] > 9) { r[k + 1] += r[k] / 10; r[k] %= 10; k++; } } } int l = la + lb - 1; while(r[l] == 0 && l > 0) l--; for(int i = l; i >= 0; i--) cout << r[i]; cout << endl; } int main() { while(cin >> c1 >> c2) { int la = c1.size(), lb = c2.size(); for(int i = 0; i != la; i++) a[i] = (int)(c1[la - i - 1] - '0'); for(int i = 0; i != lb; i++) b[i] = (int)(c2[lb - i - 1] - '0'); solve(a, b, la, lb); } return 0; }
第三題:
貪心啊, 我是按照 盡量滿足最小人的需求來貪心的。。。一直90%? 有人是用盡量使用掉最大的巧克力來貪的,100%, 來個反例好不好?
#include <cstdio> #include <iostream> #include <string> #include <algorithm> using namespace std; const int N = 3e6 + 10; long long w[N], h[N]; int main() { int n, m; while(~scanf("%d", &n)) { for(int i = 0; i < n; ++i) { scanf("%lld", &h[i]); } scanf("%d", &m); for(int i = 0; i < m; ++i) { scanf("%lld", &w[i]); } sort(h, h + n); sort(w, w + m); int Ans = 0; for(int i = 0, j = 0; i < n && j < m; ) { if(w[j] >= h[i]) { ++Ans; ++i, ++j; } else { ++j; } } printf("%d\n", Ans); } return 0; }
第四題:
迷宮問題, 有趣的是多了一把鑰匙。。。 一看門不超過10個。。。M, N <=100...想了想狀態(tài)數(shù)。。。直接狀態(tài)壓縮吧。。 之后就是一個非常暴力可恥的狀態(tài)壓縮bfs。。。然后就一發(fā)AC了。。
#include <cstdio> #include <iostream> #include <string> #include <queue> #include <map> #include <algorithm> using namespace std; const int N = 110; char mz[N][N]; bool vis[N][N][N*10]; int fx[4] = {0, 0, 1, -1}; int fy[4] = {1, -1, 0, 0}; int m, n; map<char, int> key; struct node { int x, y, cnt, sta; node():cnt(0), sta(0) {} }; queue<node> que; int bfs(int sx, int sy, int ex, int ey) { while(!que.empty()) que.pop(); node tmp; tmp.x = sx, tmp.y = sy; que.push(tmp); while(!que.empty()) { node p = que.front(); if(p.x == ex && p.y == ey) { return p.cnt; } que.pop(); for(int i = 0; i < 4; ++i) { int newx = p.x + fx[i]; int newy = p.y + fy[i]; if(newx < 0 || newx >= m || newy < 0 || newy >= n) continue; if(mz[newx][newy] == '0') continue; int sta = p.sta; if(mz[p.x][p.y] >= 'a' && mz[p.x][p.y] <= 'z') { sta |= (1<<key[mz[p.x][p.y]]); } if(vis[newx][newy][sta]) continue; if(mz[newx][newy] >= 'A' && mz[newx][newy] <= 'Z') { if((sta & (1<<(key[mz[newx][newy] - 'A' + 'a'])))== 0) { continue; } } vis[newx][newy][sta] = true; tmp.x = newx, tmp.y = newy, tmp.cnt = p.cnt + 1, tmp.sta = sta; que.push(tmp); } } return -1; } int main() { while(~scanf("%d %d", &m, &n)) { int sx, sy, ex, ey; int cnt = 0; for(int i = 0; i < m; ++i) { scanf("%s", mz[i]); for(int j = 0; j < n; ++j) { if(mz[i][j] == '2') { sx = i, sy = j; } if(mz[i][j] == '3') { ex = i, ey = j; } if(mz[i][j] >= 'a' && mz[i][j] <= 'z') { key[mz[i][j]] = cnt++; } } } for(int i = 0; i < m; ++i) { for(int j = 0; j < n; ++j) { for(int s = 0; s < (1<<cnt); ++s) { vis[i][j][s] = false; } } } int Ans = bfs(sx, sy, ex, ey); printf("%d\n", Ans); } return 0; }
希望本文所述對大家C++程序設計有所幫助。
您可能感興趣的文章
- 04-02c語言的正則匹配函數(shù) c語言正則表達式函數(shù)庫
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10c語言求1+2+...+n的解決方法
- 01-10求子數(shù)組最大和的解決方法詳解
- 01-10深入理解約瑟夫環(huán)的數(shù)學優(yōu)化方法
- 01-10深入二叉樹兩個結點的最低共同父結點的詳解
- 01-10數(shù)據結構課程設計- 解析最少換車次數(shù)的問題詳解
- 01-10c語言 跳臺階問題的解決方法


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10C#中split用法實例總結
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文