拼多多 算法工程师-20180921

1.数数看

import re
from collections import Counter
#i buy an apple watch from the aPple store, by the way I also like eating apple.
if __name__ == '__main__':
    duan=input()
    duan=duan.lower()
    wordlist=re.split('[!-\?,;:\.\s]',duan)
    words=[word for word in wordlist if word is not '']
    tongji=Counter(words)
    #print(tongji.most_common())    
    tongji_words = tongji.most_common()
    n=len(tongji_words)
    for i in range(n):
        if i==n-1 or tongji_words[i][1]!=tongji_words[i+1][1]:
            break
    most_cnt_words = tongji.most_common(i+1)
    res = [t[0] for t in most_cnt_words]
    res.sort()
    ans = " ".join(res)
    print(ans)

2.缓存问题

#include<stdio.h>
#include<stdlib.h>
#include<string.h> 
int add[10001];
int cun[1001];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&add[i]);
	}
	int res=-1;
	double max=0;
	for(int j=1;j<1000;j++){
		memset(cun,-1,sizeof(cun));
		int cnt=0;
		for(int i=0;i<n;i++){
			if(cun[add[i]%j]==add[i]){
				cnt++; 
			}
			else{
				cun[add[i]%j]=add[i];
			}
		}
		if(double(cnt)/double(n)>max){
			max=double(cnt)/double(n);
			res=j;
		}
	}
	printf("%d\n",res);
	//system("PAUSE");
} 
//0 1 1 0 2 3 2 3

3.优惠券问题

4.拆宝箱拿金币

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<vector>
using namespace std;
int main(){
	int M,A,R;
	cin >> M >> A >> R;
	vector<double> dp(A+R+1,0);
	dp[0]=1;
	for(int i=0;i<A;i++){
		for(int j=1;j<=R;j++){
			dp[i+j]=dp[i+j]+dp[i]/R;
		}
	}
	double res=0;
	for(int i=A;i<=M;i++){
		res+=dp[i];
	}
	printf("%.5f",res);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/LXQ1071717521/article/details/82839585