腾讯笔试 9月5日

1. AC100%
#encode:utf-8
import sys
n,m = list(map(int,sys.stdin.readline().strip(' ').strip('\n').strip(' ').split(' ')))
ws = list(map(int,sys.stdin.readline().strip(' ').strip('\n').strip(' ').split(' ')))
As = list(map(int,sys.stdin.readline().strip(' ').strip('\n').strip(' ').split(' ')))
import math
record = {}
for i in range(n):
    for j in range(n):
        if i!=j:
            ta = ws[j]*math.log2(ws[i])
            sta = '%.9f'% ta
            record[sta] = [ws[i],ws[j]]

for a in As:
    ta = math.log2(a)
    sta = '%.9f' % ta
    if sta not in record:
        print("{} {}".format(-1,-1))
    else:
        print("{} {}".format(record[sta][0],record[sta][1]))

2. 二分+积分,AC100%
#encode:utf-8
import sys
k,b = list(map(int,sys.stdin.readline().strip(' ').strip('\n').strip(' ').split(' ')))

import math

l,r = 0.,b*1.0

while r-l>1e-8:
    mid = (l+r)/2
    y1= mid**k
    y2 = b-mid
    if y1>= y2:
        r = mid
    else:
        l= mid
x1=(l+r)/2
def fun1(x,k):
    return 1/(k+1)*(x**(k+1))
def fun2(x,b):
    return -1/2*(x*x)+b*x
print(fun2(b,b)-fun2(x1,b)+(fun1(x1,k)-fun1(0,k)))

3.搜索,注意Python一定超时,得用C++,AC100%
#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <math.h>
#include <queue>
using namespace std;

vector<string> graph;
const int maxn = 1005;
int book[maxn][maxn] ;
int main() {
    int n,m;
    cin>>n>>m;
    string t;
    for (int i = 0; i <n ; ++i) {
        cin>>t;
        graph.push_back(t);
    }
    int nex[8][2] =  {{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2}};
    int startx,starty;
    cin>>startx>>starty;
    startx--;
    starty--;
    queue<pair<int,int> > q ;
    book[startx][starty] =1 ;
    q.push(make_pair(startx,starty));
    int res=0;
    int x,y,nx,ny;
    char nflag ;
    while (q.size()){
        x= q.front().first;
        y= q.front().second;
        q.pop();
        res+=1;
        nflag='b';
        if (graph[x][y]=='b'){
            nflag='r';
        }
        for (int i = 0; i < 8; ++i) {
            nx=x+nex[i][0];
            ny=y+nex[i][1];
            if (0<=nx &&nx<n &&0<=ny &&ny<m && nflag==graph[nx][ny] &&book[nx][ny]==0){
                book[nx][ny]=1;
                q.push(make_pair(nx,ny));
            }
        }

    }

    cout<<res<<endl;
    return 0;
}
4. 动态规划AC100%
#encode:utf-8
import sys
from collections import deque
# sys.setrecursionlimit(1000000)
n =int(sys.stdin.readline().strip(' ').strip('\n').strip(' '))
s = sys.stdin.readline().strip(' ').strip('\n').strip(' ')
dp = [[1,0,0,0,0] for _ in range(n+2)]
res = 0

v2idx = {'S':1,'T':2,'A':3,'R':4}
for i in range(2,n+2):
    idx = v2idx[s[i-2]]

    for j in range(1,5):
        if j==idx:
            dp[i][j] = dp[i - 2][j-1] + dp[i - 1][j]
        else:
            dp[i][j] = dp[i-1][j]

print(dp[-1][-1])
5. 没做,贴一个大佬的代码吧:
#include <iostream>
using namespace std;
double ans;
int main()
{
    int n, m; cin >> n >> m;
    for(int i=1; i<=m; i++)
        ans += 2.0 + double(n) / double(i);
    printf("%.2lf\n", ans);
    return 0;
}



#腾讯笔试##腾讯##笔经#
全部评论
第四题 dp定义看不懂
点赞 回复 分享
发布于 2021-09-06 00:37
第一题能给个详细一点的思路吗?看大家都说是二分,但是我想不明白怎么分。
点赞 回复 分享
发布于 2021-09-07 11:30

相关推荐

2024-12-26 13:00
太原理工大学 Java
会飞的猿:简历没啥大问题啊,感觉是缺少了实习经历。多投投先找个中小厂过渡一下吧
点赞 评论 收藏
分享
评论
6
32
分享

创作者周榜

更多
牛客网
牛客企业服务