20190908爱奇艺,BIGO,字节跳动

爱奇艺

1.

Leetcode原题

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define mod 1000000007
int n;
int dp[1001][1001];
bool flag[1001];
int main(){
    scanf("%d",&n);
    int s;
    for(int i=0;i=0;j--){
                dp[i+1][j]=cur=(cur+dp[i][j+1])%mod;
            }
        }
    }
    /*for(int i=0;i

2.

红球n个,蓝球m个,ABC三个人,从A开始依次拿球,AB拿到红球游戏结束,C是捣乱的,可以拿红球和蓝球,但是他拿到红球不算赢,如果他把红球拿光了,算B赢,计算A获胜的概率。

超时了。。。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int t;
int n,m;
double dfs(int abc,int r,int b){
    if(r<=0)
    return 0.0;
    if(abc==0){
        if(b==0)
        return (double)r/(r+b);
        return (double)r/(r+b)+(double)b/(r+b)*dfs(1,r,b-1);
    }
    else{
        if(abc==1){
            if(b==0)
            return 0.0;
            return (double)b/(r+b)*dfs(2,r,b-1);
        }
        else{
            if(b==0)
            return (double)r/(r+b)*dfs(0,r-1,b);
            return (double)r/(r+b)*dfs(0,r-1,b)+(double)b/(r+b)*dfs(0,r,b-1);
        }
    }
}
int main(){
    scanf("%d %d",&n,&m);
    //int sum=n+m;
    double ans=1.0;
    ans=dfs(0,n,m);
    printf("%.5lf\n",ans);
    //system("PAUSE");
    return 0;
}

 可能要用数组暂存优化

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
double f[1005][1005][3];
double dfs(int x, int y, int t) {
    if (f[x][y][t] != -1) return f[x][y][t];
    double ans = 0;
    if (x > 0) {
        if (t == 0) ans += (double)x / (double)(x+y);
        if (t == 2) {
            ans += (double)x / (double)(x+y) * dfs(x-1, y, (t+1)%3);
        }
    }

    if (y > 0) {
        ans += (double)y / (double)(x+y) * dfs(x, y-1, (t+1)%3);
    }
    //f[x][y][t] = ans;
    //printf("OOOO %d %d %d %f\n", x, y, t, f[x][y][t]);
    return f[x][y][t] = ans;
}

void work() {

    int n, m;
    scanf("%d%d", &n, &m);
    memset(f, 0, sizeof f);
    for (int i = 0; i <= n+1; i++)
        for (int j = 0; j <= m+1; j++)
            for (int k = 0; k < 3; k++)
                f[i][j][k] = -1;
    double ans = 0;
    ans = dfs(n, m, 0);
    printf("%.5f\n", ans);
}

int main() {
    work();
    return 0;
}

BIGO

1.

拿石子问题(博弈论)

2.

aligned_malloc

3.

爬n阶楼梯,一次可以爬1,2,3阶,求方法的总数

#include
#include
#include
#include
#include
#include
#include
using namespace std;
int n;
int dfs(int left){
    if(left==0)return 1;
    if(left<0) return 0;
    return dfs(left-1)+dfs(left-2)+dfs(left-3);
}
int climb_base(int n){
    int ans=dfs(n);
    return ans;
}
int main(){
    scanf("%d",&n);
    int ans;
    ans=climb_base(n);
    cout<

4.

求两个不相交的连续子数组的最大和

头条

1.

n = int(input().strip())
a = list(map(int, input().strip().split()))

res = [-1, 0]
i = n-1
stack = [[a[i], i]]
i -= 1
while stack and i >= 0:
	ans = 0
	while stack and a[i] >= stack[-1][0]:
		stack.pop(-1)
		ans += 1
	if ans >= res[1]:
		res = [a[i], ans]
	stack.append([a[i], i])
	i -= 1
print(res[0])

 2.倒水

20190908爱奇艺,BIGO,字节跳动_第1张图片

20190908爱奇艺,BIGO,字节跳动_第2张图片

 

20190908爱奇艺,BIGO,字节跳动_第3张图片


import java.util.*;

public class Main {
    public static class Water{
        public int l1=0;
        public int l2=0;
        public int l3=0;
        public Water(Water w){
            l1=w.l1;l2=w.l2;l3=w.l3;
        }
        public Water(){
            l1=0;l2=0;l3=0;
        }
        @Override
        public int hashCode(){
            String str=String.valueOf(l1)+String.valueOf(l2)+String.valueOf(l3);
            return  Integer.valueOf(str);
        }
        @Override
        public boolean equals(Object  obj){
            if(obj instanceof Water) {
                Water w = (Water) obj;
                return (w.l1 == l1 && w.l2 == l2 && w.l3 == l3);
            }
            return  false;
        }
    }
    public static void bfs(int kettle[],Water water,HashSet set,List queue){

        Water w=new Water(water);
        w.l1=kettle[0];
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }
        w=new Water(water);
        w.l2=kettle[1];
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }
        w=new Water(water);
        w.l3=kettle[2];
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }
        w=new Water(water);
        w.l1=0;
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }
        w=new Water(water);
        w.l2=0;
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }
        w=new Water(water);
        w.l3=0;
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }
        w=new Water(water);
        w.l1=(water.l1+water.l2-kettle[1])<0?0:(water.l1+water.l2-kettle[1]);
        w.l2=(water.l1+water.l2>kettle[1])?kettle[1]:(water.l1+water.l2);
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }

        w=new Water(water);
        w.l2=(water.l1+water.l2-kettle[0])<0?0:(water.l1+water.l2-kettle[0]);
        w.l1=(water.l1+water.l2>kettle[0])?kettle[0]:(water.l1+water.l2);
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }

        w=new Water(water);
        w.l1=(water.l1+water.l3-kettle[2])<0?0:(water.l1+water.l3-kettle[2]);
        w.l3=(water.l1+water.l3>kettle[2])?kettle[2]:(water.l1+water.l3);
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }

        w=new Water(water);
        w.l3=(water.l1+water.l3-kettle[0])<0?0:(water.l1+water.l3-kettle[0]);
        w.l1=(water.l1+water.l3>kettle[0])?kettle[0]:(water.l1+water.l3);
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }

        w=new Water(water);
        w.l2=(water.l2+water.l3-kettle[2])<0?0:(water.l2+water.l3-kettle[2]);
        w.l3=(water.l2+water.l3>kettle[2])?kettle[2]:(water.l2+water.l3);
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }

        w=new Water(water);
        w.l3=(water.l2+water.l3-kettle[1])<0?0:(water.l2+water.l3-kettle[1]);
        w.l2=(water.l2+water.l3>kettle[1])?kettle[1]:(water.l2+water.l3);
        if(!set.contains(w)){
            set.add(w);
            queue.add(w);
        }
    }
    public static int func(int kettle[],int target){
        Water water=new Water();
        HashSet set=new HashSet<>();
        List queue=new ArrayList<>();
        queue.add(water);
        set.add(water);
        int deep=0;
        while(!queue.isEmpty()){
            int size=queue.size();
            for(int i=0;i

3.

 

20190908爱奇艺,BIGO,字节跳动_第4张图片

20190908爱奇艺,BIGO,字节跳动_第5张图片

 

20190908爱奇艺,BIGO,字节跳动_第6张图片

20190908爱奇艺,BIGO,字节跳动_第7张图片

 

4.

20190908爱奇艺,BIGO,字节跳动_第8张图片

20190908爱奇艺,BIGO,字节跳动_第9张图片

 

20190908爱奇艺,BIGO,字节跳动_第10张图片

ch='0ABCDEFGHIJKLMNOPQRSTUVWXYZ'
s = str(input().strip())
n = len(s)
res=''
def dfs(i,l,res):
	if i==l :
		print(res)
	if l-i>=1:
		a=int(s[i])
		if a==0:
			return
		dfs(i+1,l,res+ch[a])
	if l-i >= 2 :
		a=int(s[i:i+2])
		if a<10 or a>=27:
			return
		dfs(i+2,l,res+ch[a])

dfs(0,n,res)

 

你可能感兴趣的:(笔试)