• 2021.9.5 腾讯笔试AK贴(算法岗)


    第五题

    抽卡游戏,n张普通卡,m张ssr,抽到ssr花费2金币不放回,抽到普通卡1金币放回,求取完所有ssr的期望金币数

     1 #include <iostream>
     2 using namespace std;
     3 double ans;
     4 int main()
     5 {
     6     int n, m; cin >> n >> m;
     7     for(int i=1; i<=m; i++) 
     8         ans += 2.0 + double(n) / double(i);
     9     printf("%.2lf
    ", ans);
    10     return 0;
    11 }

    第四题

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const int N = 1e5 + 7;
     5 LL dp[N][4];
     6 int n;
     7 int main()
     8 {
     9     cin >> n;
    10     string str;
    11     cin >> str;
    12     for(int i=1; i<=n; i++) {
    13         for(int j=0; j<4; j++) dp[i][j] = dp[i-1][j];
    14 
    15         if(str[i-1] == 'S') dp[i][0] += 1;
    16         else if(str[i-1] == 'T') {
    17             if(i>=2) dp[i][1] += dp[i-2][0];
    18         }
    19         else if(str[i-1] == 'A') {
    20             if(i>=2) dp[i][2] += dp[i-2][1];
    21         }
    22         else if(str[i-1] == 'R') {
    23             if(i>=2) dp[i][3] += dp[i-2][2];
    24         }
    25 
    26     }
    27     cout << dp[n][3] << endl;
    28     return 0;
    29 }

    第三题  dfs爆栈 bfs

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e3 + 7;
    struct node {
        int x, y;
    };
    int dx[] = {1, 2, 2, 1, -1, -2, -2, -1};
    int dy[] = {-2, -1, 1, 2, 2, 1, -1, -2};
    int n, m;
    string mp[N];
    bool vis[N][N];
    queue<node>
    int ans;
    bool isok(int x, int y) {
        return x>=0 && x<n && y>=0 && y<m;
    }
    void bfs(int x, int y) {
    
        node start = {x, y};
        q.push(start); vis[x][y] = 1;
        ans += 1;
    
        while(!q.empty()) {
            node now = q.front(); q.pop();
            x = now.x;
            y = now.y;
            for(int i=0; i<8; i++) {
                int tx = x + dx[i];
                int ty = y + dy[i];
                if(isok(tx, ty) && mp[x][y] != mp[tx][ty] && !vis[tx][ty]) {
                    node tmp = {tx, ty};
                    q.push(tmp); vis[tx][ty] = 1;
                    ans += 1;
                }
            } 
        }
    }
    int main()
    {
        cin >> n >> m;
        for(int i=0; i<n; i++)
            cin >> mp[i];
        int x, y;
        cin >> x >> y;
        dfs(x-1, y-1);
        cout << ans << endl;
        return 0;
    }
    

      

    第二题 二分

    x^k = y

    x + y = b

    (k>0, b>0) 求两条曲线在第一象限的相交面积 

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int k, b;
     4 double binary_search(int k, int b) {
     5     double l = 0, r = b;
     6     while(l<=r) {
     7         double mid = (l+r) * 0.5;
     8         if(k * log(mid) > log(1e6)) {
     9             r = mid;
    10             continue;
    11         }
    12         if(abs(mid + pow(mid, k)  - b) < 1e-9) return mid;
    13         else if(mid + pow(mid, k) > b)  r = mid;
    14         else                            l = mid;
    15     }
    16     return l;
    17 }
    18 int main()
    19 {
    20     cin >> k >> b;
    21     double x = binary_search(k, b);
    22     // printf("%lf %lf
    ", x, x + pow(x, k));
    23     double area = pow(x, k+1) / (k+1) + pow(x, k) * pow(x, k) * 0.5;
    24     printf("%.6lf
    ", area);
    25     return 0;
    26 }

    第一题 二分

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    struct node {
    	double val;
    	int x, y;
    	bool operator<(const node& a) const {
    		return val < a.val;
    	}
    };
    LL w[507];
    vector<node> num;
    int n, m;
    int binary_search(LL x) {
    	double key = log(x);
    	int l = 0, r = num.size() - 1;
    	while(l<=r) {
    		int mid = (l+r) / 2;
    		if(abs(num[mid].val - key) <= 1e-9) return mid;
    		else if(num[mid].val > key) r = mid - 1;
    		else                        l = mid + 1;
    	}
    	return -1;
    }
    int main()
    {
    	cin >> n >> m;
    	for(int i=0; i<n; i++) cin >> w[i];
    	for(int i=0; i<n; i++)
    		for(int j=i+1; j<n; j++) {
    			double val = log(w[i]) * w[j];
    			node tmp = {val, i, j};
    			num.push_back(tmp); 
    
    			val = log(w[j]) * w[i];
    			node ttmp = {val, j, i};
    			num.push_back(ttmp); 
    		}
    	sort(num.begin(), num.end());
    	for(int i=0; i<m; i++) {
    		LL x; cin >> x;
    		int pos = binary_search(x);
    		if(pos == -1) cout << "-1 -1
    ";
    		else 		  cout << w[num[pos].x] << " " << w[num[pos].y] << endl; 
    	}
    	return 0;
    }
    

      

    抓住青春的尾巴。。。
  • 相关阅读:
    录音和朗诵的实现
    树型控件的处理(完整版)
    蜘蛛爬虫
    百度公司面试题
    一名程序员的杂想
    javascript语法
    HTML标签
    Winform中保存当前控件的记录
    hdu3079 Vowel Counting (strlwr(将字符串中的字母转换为小写);strupr(转换为大写))
    hdu 1860 统计字符 (水)
  • 原文地址:https://www.cnblogs.com/xidian-mao/p/15231379.html
Copyright © 2020-2023  润新知