首页 > 2021.8.6 网易雷火笔试 第一、二、四题代码
头像
Silviayun2021
编辑于 2021-08-06 22:14
+ 关注

2021.8.6 网易雷火笔试 第一、二、四题代码

四题完成度 100 100 0 100 🤣🤣
第一题
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int main(){
    int q1=0, q2=0 ,p=0;
    cin>>q1>>q2>>p;
    int pair_num=0, single_sum=0;
    cin>>pair_num;
    vector<int> pair1;
    vector<int> pair2;
    vector<int> single;
    int tmp=0;
    for(int i=0;i<pair_num;i++){
        cin>>tmp;
        pair1.push_back(tmp);
        cin>>tmp;
        pair2.push_back(tmp);
    }
    cin>>single_sum;
    for(int i=0;i<single_sum;i++){
        cin>>tmp;
        single.push_back(tmp);
    }
    int res=0;
    for(int i=0;i<pair_num;i++){
        if(abs(q1+q2-pair1[i]-pair2[i])<=p)
            res++;
    }
    for(int i=0;i<single_sum;i++)
        for(int j=i+1;j<single_sum;j++){
            if(abs(single[i]+single[j]-q1-q2)<=p)
                res++;
        }
    cout<<res<<endl;
    return 0;
}
第二题
#include<iostream>
#include<vector>
using namespace std;
int num_min(int a, int b, int c);
int main(){
    int n=0, t1=0, t2=0, t3=0, t4=0;
    cin>>n>>t1>>t2>>t3>>t4;
    vector<int> ground1;
    vector<int> ground2;
    int tmp=0;
    for(int i=0;i<n;i++){
        cin>>tmp;
        ground1.push_back(tmp);
        ground2.push_back(4-tmp);
    }
    vector<int> dp1;
    vector<int> dp2;
    dp1.push_back(0);
    dp2.push_back(t4);
    for(int i=0;i<n-1;i++){
        if(ground1[i+1]==ground1[i]){
            if(dp1[i]+t1<dp2[i]+t1+t4)
                dp1.push_back(dp1[i]+t1);
            else
                dp1.push_back(dp2[i]+t1+t4);
            if(dp2[i]+t1<dp1[i]+t1+t4)
                dp2.push_back(dp2[i]+t1);
            else
                dp2.push_back(dp1[i]+t1+t4);
        }    
        else{
            if(ground1[i+1]-ground1[i]>1){
                if(dp1[i]+t4+t3+t4<dp2[i]+t3+t4)
                    dp1.push_back(dp1[i]+t4+t3+t4);
                else
                    dp1.push_back(dp2[i]+t3+t4);
                if(dp1[i]+t4+t3<dp2[i]+t3)
                    dp2.push_back(dp1[i]+t4+t3);
                else
                    dp2.push_back(dp2[i]+t3);
            }
            else
                if(ground1[i+1]-ground1[i]<-1){
                    if(dp2[i]+t4+t3<dp1[i]+t3)
                        dp1.push_back(dp2[i]+t4+t3);
                    else
                        dp1.push_back(dp1[i]+t3);
                    if(dp2[i]+t4+t3+t4<dp1[i]+t3+t4)
                        dp2.push_back(dp2[i]+t4+t3+t4);
                    else
                        dp2.push_back(dp1[i]+t3+t4);
                }
                else{
                    int min=0;
                    if(ground1[i+1]>ground1[i]){
                        min=num_min(dp1[i]+t2, dp1[i]+t4+t3+t4, dp2[i]+t3+t4);
                        dp1.push_back(min);
                        min=num_min(dp2[i]+t3, dp2[i]+t4+t3+t4, dp1[i]+t2+t4);
                        dp2.push_back(min);
                    }
                    else{
                        min=num_min(dp1[i]+t3, dp1[i]+t4+t2+t4, dp2[i]+t2+t4);
                        dp1.push_back(min);
                        min=num_min(dp2[i]+t2, dp2[i]+t4+t3+t4, dp1[i]+t3+t4);
                        dp2.push_back(min);
                    }
                }
        }
    }
    if(dp1[n-1]<dp2[n-1])
        cout<<dp1[n-1]<<endl;
    else
        cout<<dp2[n-1]<<endl;
    return 0;
}
int num_min(int a, int b, int c){
    if(a<b)
        if(a<c)
            return a;
        else
            return c;
    else
        if(b<c)
            return b;
        else
            return c;
}
第四题
#include<iostream>
#include<vector>
#include<iomanip>
using namespace std;
double judge(double start, double speed, double my_start, double my_speed, double m, double l);
int main(){
    double m=0.0, l=0.0;
    int n=0;
    while(cin>>m>>n>>l){
        vector<double> start;
        vector<double> speed;
        double tmp=0.0;
        for(int i=0;i<n;i++){
            cin>>tmp;
            start.push_back(tmp);
        }
        for(int i=0;i<n;i++){
            cin>>tmp;
            speed.push_back(tmp);
        }
        double res=0;
        double res_tmp=0;
        if(l>=m/2)
            res=0;
        else
            for(int i=1;i<n;i++){
                res_tmp=judge(start[i], speed[i], start[0], speed[0], m, l);
                if(res_tmp>res)
                    res=res_tmp;
            }
        cout<<setiosflags(ios::fixed)<<setprecision(2)<<res<<endl;
    }
    return 0;
}
double judge(double start, double speed, double my_start, double my_speed, double m, double l){
    double tmp=0;
    double my_start_pair=my_start+m;
    if(my_start_pair>=2*m)
        my_start_pair-=2*m;
    
    double my_start_high=my_start+l;
    if(my_start_high>=2*m)
        my_start_high-=2*m;
    
    double my_start_low=my_start-l;
    if(my_start_low<0)
        my_start_low+=2*m;
    
    double my_start_pair_high=my_start_pair+l;
    if(my_start_pair_high>=2*m)
        my_start_pair_high-=2*m;
    
    double my_start_pair_low=my_start_pair-l;
    if(my_start_pair_low<0)
        my_start_pair_low+=2*m;
    
    if(my_start_high>my_start_low){
        if(my_start_high>=start && my_start_low<=start)
            return 0;
    }
    else{
        if(start<=my_start_high || start>=my_start_low)
            return 0;
    }
    
    if(my_start_pair_high>my_start_pair_low){
        if(my_start_pair_high>=start && my_start_pair_low<=start)
            return 0;
    }
    else{
        if(start<=my_start_pair_high || start>=my_start_pair_low)
            return 0;
    }
    
    if(my_speed>speed){
        if(start>my_start_high && start>my_start_pair_high){
            if(my_start_high>my_start_pair_high)
                tmp=(start-my_start_high)/(my_speed-speed);
            else
                tmp=(start-my_start_pair_high)/(my_speed-speed);
        }
        else{
            if(start<my_start_high && start<my_start_pair_high){
                if(my_start_high>my_start_pair_high)
                    tmp=(2*m-my_start_high+start)/(my_speed-speed);
                else
                    tmp=(2*m-my_start_pair_high+start)/(my_speed-speed);
            }
            else{
                if(my_start_high>my_start_pair_high)
                    tmp=(start-my_start_pair_high)/(my_speed-speed);
                else
                    tmp=(start-my_start_high)/(my_speed-speed);
            }
        }
    }
    else{
        if(start>my_start_low && start>my_start_pair_low){
            if(my_start_low>my_start_pair_low)
                tmp=(2*m-start+my_start_pair_low)/(speed-my_speed);
            else
                tmp=(2*m-start+my_start_low)/(speed-my_speed);
        }
        else{
            if(start<my_start_low && start<my_start_pair_low){
                if(my_start_low>my_start_pair_low)
                    tmp=(my_start_pair_low-start)/(speed-my_speed);
                else
                    tmp=(my_start_low-start)/(speed-my_speed);
            }
            else{
                if(my_start_low>my_start_pair_low)
                    tmp=(my_start_low-start)/(speed-my_speed);
                else
                    tmp=(my_start_pair_low-start)/(speed-my_speed);
            }
        }
    }
    return tmp;
}




全部评论

(4) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐