1. 程式人生 > >觸寶科技2018校招筆試題

觸寶科技2018校招筆試題

這裡寫圖片描述
這裡寫圖片描述

#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;
}