拼多多-算法实习生笔试-20190403

1.给定一个长度为偶数的整数数组,两两配对并求和,问如何配对才能使这些和的最大值和最小值的差值最小,输出最小的差值

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
int num[10005];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&num[i]);
    }
    sort(num,num+n);
    vector<int> res;
	for(int i=0;i<n/2;i++){
		res.push_back(num[i]+num[n-1-i]);
		printf("%d ",res[i]);
	} 
	sort(res.begin(),res.end());
    printf("%d\n",abs(res[0]-res[n/2-1]));
    return 0;
}

2.现有数字0-9,已知0-9中每个数字的可用次数,另有两个整数A和B,A和B由这些数字组成,并且已知A和B各自的位数,两个数可以有一个或多个先导0,求A和B的最小值。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int shu[11];
int main(){
    int a,b;
    for(int i=0;i<10;i++){
        scanf("%d",&shu[i]);
    }
    scanf("%d %d",&a,&b);
    if(a>b){
        int t=b;
        b=a;
        a=t;
    }//让A是位数较少的那一个 
    if(shu[0]>=a){
        printf("0\n");
    }
    else{
        int aa=0,bb=0;
        while(shu[0]>0){
        	a--;
        	shu[0]--;
		} //把0都给A
		//剩下的数字,AB各一半 
        for(int i=1;i<10;i++){
            if(a==0&&b==0)
                break;
            int l=shu[i];
            int j=0;
            while(a>0&&shu[i]>0&&j<(l+1)/2){
                a--;
                shu[i]--;
                aa=aa*10+i;
                j++;
            }
            while(b>0&&shu[i]>0){
                b--;
                shu[i]--;
                bb=bb*10+i;
            }
        }
    	printf("%d %d ",bb,aa);
        printf("%d",bb*aa);
    }
    
    return 0;
}

3.你有一堆不同颜色的袜子,用一个数组S来描述,数组长度为L。每个数字代表袜子的颜色,如果两只袜子的颜色差值的绝对值小于等于你可以接受的色差d,那这两只袜子就可以作为一双袜子。随机抽取两只袜子,求它们可以被接受为一双袜子的概率。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int S[51];
int main(){
    char c;
    int n=0,d,l=0;
    while(scanf("%c",&c)&&c!='}'){
    	if(c<='9'&&c>='0'){
    		n=n*10+(c-'0'); 
		}
		else{
			if(c==','){
				S[l]=n;
				n=0;
				l++;
			}
		}
	}
	S[l]=n;
	n=0;
	l++;
	scanf("%d",&d);
	double p;
	int zong=l*(l-1)/2;
	int dui=0;
    for(int i=0;i<l;i++){
        for(int j=i+1;j<l;j++){
        	if(abs(S[i]-S[j])<=d)
        		dui++; 
		}
    }
    printf("%.6f",double(dui)/double(zong));
    return 0;
}

4.编辑距离

https://leetcode-cn.com/problems/edit-distance/

#include<iostream>
using namespace std;
int dp[101][101];
int mind(string word1,string word2,int start1,int start2){
    if(start1>=word1.length()){
        return word2.size()-start2;
    }
    if(start2>=word2.length())
        return word1.size()-start1;
    if(dp[start1][start2]!=-1){
        return dp[start1][start2]; 
    }
    if(word1[start1]==word2[start2]){
        dp[start1][start2]=mind(word1,word2,start1+1,start2+1);
        return dp[start1][start2];
    }
    int i=mind(word1,word2,start1,start2+1);
    int d=mind(word1,word2,start1+1,start2);
    int r=mind(word1,word2,start1+1,start2+1);
    int m=1+min(i,min(d,r));
    dp[start1][start2]=m;
    return dp[start1][start2];
}
int main(){
    string word1,word2;
    cin>>word1;
    cin>>word2;
    for(int i=0;i<word1.size();i++)
        for(int j=0;j<word2.size();j++){
            dp[i][j]=-1;
        }
    cout<<mind(word1,word2,0,0);
    return 0;
}

猜你喜欢

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