触宝科技2018校招笔试题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ProgramVAE/article/details/77857919

这里写图片描述
这里写图片描述

#include <iostream>
#include <string>
#include <vector>
using namespace std;

void find(vector<string>& ve,vector<string>& ve1)
{
        for(int i = 0; i < ve1.size(); ++i)
        {
                int count = 0;
                for(int j = 0; j < ve.size(); ++j)
                {
                        size_t fi = ve[j].find(ve1[i]);
                        if(fi != ve[j].npos)
                        {
                                count++;
                        }
                }
                cout<<count<<endl;
        }
}

int main()
{
        int n;

        while(cin>>n)
        {
                vector<string> ve;
                for(int i = 0; i < n; ++i)
                {
                        string temp;
                        cin>>temp;
                        ve.push_back(temp);
                }
                               int m;
                cin>>m;
                vector<string> ve1;
                for(int i = 0; i < m; ++i)
                {
                        string temp;
                        cin>>temp;
                        ve1.push_back(temp);
                }
                find(ve,ve1);
        }
        return 0;
}

这里写图片描述

大概的思路就是 默认这个距离(最大两点的距离)函数是时间变量的一个凸函数(我猜的,具体证明不会),凸函数找最大值,用三分法就可以了。 然后就三分时间则可以求得答案

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

const int MX = 308;
const double eps = 0.00001;
struct V{
    double _x, _y;
    V(){}
    V(double x, double y):_x(x),_y(y){}
    V operator *(double mul) const{
        return V(mul*_x, mul*_y);
    }
    V operator +(const V & v1) const{
        return V(_x+v1._x, _y+v1._y);
    }
    double dis(const V & v1){
        double sum1 = _x - v1._x; sum1 *= sum1;
        double sum2 = _y - v1._y; sum2 *= sum2;
        return sqrt(sum1+sum2);
    }
    void pf(){
        printf("point :%lf %lf\n", _x, _y);
    }
};
int n;
V point[MX];
V speed[MX];
V now[MX];
double get_dis(double tim){
    for(int i = 0; i < n; i++){
        now[i] = point[i] + speed[i]*tim;
    }
    double tmax = 0.0;
    for(int i = 0; i < n; i++){
        for(int j = i+1; j < n; j++){
            tmax = max(tmax, now[i].dis(now[j]));
        }
    }
    return tmax;

}
int main(){
    while(cin>>n){
        for(int i = 0;i < n;i++){
            double x,y;scanf("%lf%lf",&x,&y);
            point[i] = V(x,y);
            double sx,sy;scanf("%lf%lf",&sx,&sy);
            speed[i] = V(sx,sy);
        }
        double res = get_dis(0.00);
        double flag_time = 0.00;
        double l = 0.00;
        double r = 1000000000.00;
        while(l < r - eps){
            double mid1 = (l+r)*0.5;
            double mid2 = (l+mid1)*0.5;
            double res1 = get_dis(mid1);
            double res2 = get_dis(mid2);
            if(res1 < res2){
                l = mid2;
                if(res1 < res){
                    flag_time = mid1;
                    res = res1;
                }
            }
            else{
                r = mid1;
                if(res2 < res){
                    flag_time = mid1;
                    res = res1;
                }
            }
        }
        printf("%.2lf %.2lf\n",flag_time, res);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ProgramVAE/article/details/77857919