2022.8.22腾讯技术研究笔试满分代码

第一题 求期望:

#include <bits/stdc++.h>

using namespace std;
double input[505][505];
double a[505];
int main()
{
    int n, m;
    cin>>n>>m;
    double ans=0;
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            scanf("%lf", &input[i][j]);
        }
    }
    for(int j=0; j<m; j++){
        for(int i=0; i<n; i++){
            a[i] = input[i][j];
        }
        sort(a, a+n);
        double sum = 0;
        for(int i=0; i<n; i++){
            sum += a[i];
            ans += sum;
        }
    }
    printf("%.7f\n", ans/n);
    return 0;
}

第二题 求转移概率:

#include <bits/stdc++.h>

using namespace std;
double edg[4][4];
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        double p[4];
        for(int i=0; i<3; i++)scanf("%lf", &p[i]);
        for(int i=0; i<3; i++){
            for(int j=0; j<3; j++){
                scanf("%lf", &edg[i][j]);
            }
        }
        for(int it=0; it<n; it++){
            double new_p[4];
            new_p[0]=new_p[1]=new_p[2]=0;
            for(int i=0; i<3; i++){
                for(int j=0; j<3; j++){
                    new_p[i] += p[j]*edg[i][j];
                }
            }
            for(int i=0; i<3; i++)p[i]=new_p[i];
        }
        if(p[2]>0.5)printf("1\n");
        else printf("0\n");
    }
    return 0;
}

第三题 求车队数量:

    #include <bits/stdc++.h>

    using namespace std;
    const int maxn = 1e5+5;
    int speed[maxn];
    int main()
    {
        int n;
        cin>>n;
        for(int i=0; i<n; i++)scanf("%d", &speed[i]);
        sort(speed, speed+n);
        int l=0, r=0, ans=0;
        for(r=0; r<n; r++){
            while(speed[r]-speed[l] > 10){
                l++;
            }
            ans = max(ans, r-l+1);
        }
        cout<<ans<<endl;
        return 0;
    }

第四题 求流满的水站数量:

#include <bits/stdc++.h>

using namespace std;
double f[11][11];
int main()
{
    int n, t;
    cin>>n>>t;
    f[0][0]=t;
    int ans = 0;
    if(t>=1)ans++;
    for(int i=1; i<n; i++){
        for(int j=0; j<i+1; j++){
            if(j==0){
                f[i][j]=(f[i-1][j]-1)/2;
            }
            else if(j==i){
                f[i][j]=(f[i-1][j-1]-1)/2;
            }
            else{
                f[i][j]=(f[i-1][j]-1+f[i-1][j-1]-1)/2;
            }
            ans += (f[i][j]>=1 || abs(f[i][j]-1)<0.001);
//            printf("%.3f ", f[i][j]);
        }
//        cout<<endl;
    }
    cout<<ans<<endl;
    return 0;
}

第五题 求被围住的区域:

#include <bits/stdc++.h>

using namespace std;
int n;
int a[1005][1005];
int book[1005][1005];
int out;
int dx[5]={0, 1, 0, -1};
int dy[5]={1, 0, -1, 0};
void dfs(int x, int y){
//    printf("%d %d\n", x, y);
    book[x][y] = 1;
    for(int i=0; i<4; i++){
        int nowx = x + dx[i];
        int nowy = y + dy[i];
        if(nowx<0 || nowx>=n || nowy<0 || nowy>=n){
            out = 1;
            continue;
        }
        if(a[nowx][nowy]==0 && book[nowx][nowy]==0){
            dfs(nowx, nowy);
        }
    }
//    book[x][y] = out; //找到出口则无需再次搜索
}
void rs(int x, int y){
//     printf("%d %d\n", x, y);
     a[x][y] = 2;
     for(int i=0; i<4; i++){
        int nowx = x + dx[i];
        int nowy = y + dy[i];
        if(nowx<0 || nowx>= n || nowy<0 || nowy>=n){
            continue;
        }
//        printf("%d %d\n", nowx, nowy);
        if(a[nowx][nowy]==0) {
            rs(nowx, nowy);
        }
    }
}
int main()
{
    cin>>n;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            scanf("%d", &a[i][j]);
        }
    }
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            out = 0;
            if(a[i][j]==0 && book[i][j]==0){
//                book[i][j]=1;
                dfs(i, j);
                if(out==0){
                    rs(i, j);
                }
            }
        }
    }
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            printf(j==n-1?"%d\n":"%d ", a[i][j]);
        }
    }
    return 0;
}

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值