华为机试三道编程题(2021-09-11)

通知:最新的秋招笔试编程题题目、思路以及参考代码已经全部整理好放在【TechGuide】了,私信公众号回复【华为】或者【百度】即可获得最实时的笔试题解啦!

通知:最新的秋招笔试编程题题目、思路以及参考代码已经全部整理好放在【TechGuide】了,私信公众号回复【华为】或者【百度】即可获得最实时的笔试题解啦!

通知:最新的秋招笔试编程题题目、思路以及参考代码已经全部整理好放在【TechGuide】了,私信公众号回复【华为】或者【百度】即可获得最实时的笔试题解啦!

通知:最新的秋招笔试编程题题目、思路以及参考代码已经全部整理好放在【TechGuide】了,私信公众号回复【华为】或者【百度】即可获得最实时的笔试题解啦!
在这里插入图片描述

【2021-09-04】美团秋招笔试五道编程题(附题目)
【2021-09-03】贝壳秋招笔试四道编程题(前三道ac)
【2021-09-01】阿里巴巴秋招笔试两道编程题
【2021-09-01】华为秋招机试三道编程题(附题目,后两题AC)
【2021-08-29】美团秋招笔试四道编程题
【2021-08-29】字节跳动秋招笔试四道编程题
【2021-08-26】腾讯音乐秋招笔试编程三道题
【2021-08-25】华为秋招机试三道编程题
【2021-08-23】阿里巴巴秋招笔试两道编程题
【2021-08-22】腾讯秋招笔试五道编程题
【2021-08-22】美团秋招笔试五道编程题(待更新)
【2021-08-21】网易秋招机试四道编程题(待更新)
【2021-08-14】荣耀秋招机试三道编程题(已更新)
【2021-08-18】华为秋招机试三道编程题(已更新)
【2021-08-18】阿里秋招笔试两道编程题
【2021-08-15】美团秋招笔试五道编程题(已更新)
【2021-08-12】携程秋招笔试三道编程题
【2021-08-11】华为秋招机试三道编程题(已更新)
【2021-08-09】阿里巴巴秋招笔试两道编程题
【2021-08-08】拼多多秋招笔试四道编程题
【2021-08-08】美团秋招笔试五道编程题
【2021-08-08】好未来秋招三道编程题
【2021-08-07】网易互娱秋招笔试三道编程题
【2021-08-04】华为秋招两道编程题

以下题目来自于口述,可能不准确,仅供参考,题解整理于牛客网,链接已附上,侵删。

第一道:神经网络的最短执行时间

题目描述

给出N个节点,每个节点的信息包含该节点执行时间、下一个节点列表,求神经网络的最短执行时间。

其实就是一个有向无环图求一个拓扑排序。

参考代码

CPP版本

#include <bits/stdc++.h>
using namespace std;
//邻接表建图
vector<vector<int>>g;
//构建cost
vector<int>cost;
vector<int>cnt;//记录指向每个节点的次数
vector<bool>visit;
int sum;
void process(int idx,string s){
 int i=0;
 while (s[i]==' ')i++;
 strin***hile (i<s.size()&&s[i]==' ')i++;
 while (i<s.size()&&s[i]!=' ')f+=s[i++];//获取第一个cost
 int c= stoi(f);
 cost[idx]=c;
 //获取指向的id
 while(i<s.size()){
  while (i<s.size()&&s[i]==' ')i++;//跳过空格
  f="";
  while (i<s.size()&&s[i]!=' ')f+=s[i++];//获取指向的节点
  int to= stoi(f);
  g[idx].push_back(to);
  cnt[to]++;
 }
}

void print(){
 for (int i = 0; i < g.size(); ++i) {
  for (int j = 0; j < g[i].size(); ++j) {
   cout<<g[i][j]<<' ';
  }
  cout<<endl;
 }
 cout<<"the cost is  "<<endl;
 for (int i = 0; i < cost.size(); ++i) {
  cout<<cost[i]<<" ";
 }
 cout<<endl;
 cout<<"the cnt is: ";
 for (int i = 0; i < cnt.size(); ++i) {
  cout<<cnt[i]<<" ";
 }
 cout<<endl;
}
int dfs(int idx){
// cout<<"the cur idx is: "<<idx<<endl;
 int ans=0;
 visit[idx]=true;
 for (int i = 0; i < g[idx].size(); ++i) {
  int nxt=g[idx][i];
  if (!visit[nxt])
   ans= max(ans, dfs(g[idx][i]));
 }
 visit[idx]=false;
 return ans+cost[idx];
}

int main() {
 int n;sum=0;
 cin>>n;
 g=vector<vector<int>>(n);
 cost=vector<int>(n);
 cnt=vector<int>(n);
 visit=vector<bool>(n, false);
 string s;
 for (int i = 0; i < n; ++i) {
  cin>>s;
  string ss;
  getline(cin,ss);
  process(i,ss);
 }
// print();
//要是这个网络中的点是孤点 既不 也不 那就单独算
 int ans=0;
 queue<int>q;
 for (int i = 0; i < n; ++i) {
  if (cnt[i]==0)q.push(i);
 }
 while (!q.empty()){
  int i=q.front();
  ans= max(ans, dfs(i));
  q.pop();
 }
 cout<<ans;
 return 0;
}
// 关注TechGuide! 大厂笔经面经闪电速递!

Python版本

N=int(input())
degree={}
matrix={}
op_time={}
father={}
finish_time=[0]*N
for op_idx in range(N):
    matrix[op_idx]=[]
    degree[op_idx]=0
    father[op_idx]=[]

for op_idx in range(N):
    op=input().split()
    op_time[op_idx]=int(op[1])
    for next_op in op[2:]:
        next_op=int(next_op)
        degree[next_op]+=1
        matrix[op_idx].append(next_op)
        father[next_op].append(op_idx)

queue=[]
for op_idx in range(N):
    if degree[op_idx]==0:
        queue.append(op_idx)

tuopu=[]
while len(queue):
    cur_node=queue.pop(0)
    tuopu.append(cur_node)
    for next_node in matrix[cur_node]:
        degree[next_node]-=1
        if degree[next_node]==0:
            queue.append(next_node)

for op_idx in tuopu:
    max_time=0
    for pre_op in father[op_idx]:
        if finish_time[pre_op]>max_time:
            max_time=finish_time[pre_op]
    finish_time[op_idx]=max_time+op_time[op_idx]

print(max(finish_time))
# 关注TechGuide! 大厂笔经面经闪电速递!

第二道: 剩余的最大内存块(100%)

题目描述

给N个内存块,M个申请列表,求最后最多能剩下多少没被使用的内存块。

因为N和M比较小,可以DFS暴力搜索。

参考代码:

CPP版本

#include<iostream>
using namespace std;
int n,m;
int num[55],need[20],sum[40000];
int f[55][40000];
int rec[55][40000];
int top,stop;
int search(int x,int y)
{
    if (x==top+1) 
    {
        if (y!=stop) return -0x7fffffff/3;
        return 0;
    }
    if (rec[x][y]) return f[x][y];
    int &ans = f[x][y];
    rec[x][y] = 1;
    ans = search(x+1,y)+num[x];
    int s = stop-y;

 for (int i =s;i;i =(i-1)&s)
       {    
  if (sum[i]<=num[x])
                 {    
   ans = max(ans,search(x+1,y|i));
  }
 }
 return ans;
}
int main()
{
    cin >>n;
    for (int i = 0 ;i<n;i++)
    {
        int a ,b;
        cin>> a >> b;
        for (int j = 0 ; j<a;j++)
            num[++top] = b;
    }
    cin >>m;
    for (int i = 1 ;i<=m;i++)
     cin >> need[i];

 stop = (1<<(m))-1;
    for (int i = 1;i<=stop;i++)
    {
        for (int j = m;j>=1;j--)
            if (i&(1<<(j-1)))
            {
                sum[i] = sum[i-(1<<(j-1))]+need[j];
                break;
            }
    }
    int ans = search(1,0);
    if (ans<0)
        printf("-1\n");
    else printf("%d\n",ans);
    return 0;
}
// 关注TechGuide! 大厂笔经面经闪电速递!

Python版本

class Solution(object):
    """docstring for Solution"""
    def __init__(self, blocks, nums):
        self.blocks = blocks
        self.ans=-1
        self.use=[False]*len(blocks)
        self.nums=nums
        self.nums_len=len(nums)
        self.cur_ans=sum(self.blocks)

    def dfs(self, idx):
        if idx==self.nums_len:
            ans=0
            for i in range(len(self.blocks)):
                if not self.use[i]:
                    ans+=self.blocks[i]
            self.ans=max(self.ans, ans)
            return

        for i in range(len(self.blocks)):
            if self.blocks[i]>=self.nums[idx]:
                if not self.use[i] and self.cur_ans-self.blocks[i]<=self.ans:
                    continue
                pre_state=self.use[i]
                if not pre_state:
                    self.cur_ans-=self.blocks[i] 
                self.blocks[i]-=self.nums[idx]
                self.use[i]=True
                self.dfs(idx+1)
                self.use[i]=pre_state
                self.blocks[i]+=self.nums[idx]
                if not pre_state:
                    self.cur_ans+=self.blocks[i]

    def solve(self):
        self.dfs(0)
        if self.ans%1==0:
            print(int(self.ans))
        else:
            print(self.ans)

N=int(input())
blocks=[]
for i in range(N):
    num, block=[float(x) for x in input().split()]
    for j in range(int(num)):
        blocks.append(block)
M=int(input())
nums=[float(x) for x in input().split()]
s=Solution(blocks, nums)
s.solve()

第三道:最长的子数组(100%)

题目描述

两个递增数组s1和s2,求出最长的子数组ss1和ss2,使得对任意i,有ss1[i+1] - ss1[i] == ss2[i+1] - ss2[i]。

其实就是求出,2个数组里相同公差的最长等差数列。

参考代码

CPP版本

#include<iostream>
#include<map>
#include<vector>
#include<sstream>
using namespace std;
/*
1 2 3 4 5
2 4 6 8
输出:
3
*/
int get_max_seq(vector<int>& s1, vector<int>& s2){
    map<int,int> cnt_s1;
    map<int,int> cnt_s2;
    vector<int> nums;
    for(int i=0;i<s1.size();++i){
        for(int j=i+1;j<s1.size();++j){
            cnt_s1[s1[j]-s1[i]]+=1;
        }
    }
    for(int i=0;i<s2.size();++i){
        for(int j=i+1;j<s2.size();++j){
            cnt_s2[s2[j]-s2[i]]+=1;
        }
    }
    int ans=0;
    for(auto iter:cnt_s1){
        map<int,int>::iterator iter2=cnt_s2.find(iter.first);
        if(iter2!=cnt_s2.end()){
            ans=max(ans,min(iter.second,iter2->second));
        }
    }

    cout<<"s1"<<endl;
    for(auto iter:cnt_s1){
        cout<<iter.first<<" "<<iter.second<<endl;
    }
    cout<<"s2"<<endl;

    for(auto iter:cnt_s2){
        cout<<iter.first<<" "<<iter.second<<endl;
    }
    return ans;
}
int main(){
    vector<int> s1;
    vector<int> s2;
    string tmp;
    int tmp_i;
    getline(cin,tmp);
    istringstream input1(tmp);
    // cout<<tmp<<endl;

    while(input1>>tmp_i){
        s1.emplace_back(tmp_i);
    }
    getline(cin,tmp);
    // cout<<tmp<<endl;
    istringstream input2(tmp);

    while(input2>>tmp_i){
        s2.emplace_back(tmp_i);
    }
    int ans=0;
    ans=get_max_seq(s1,s2);
    cout<<ans<<endl;
    return 0;
}
// 关注TechGuide! 大厂笔经面经闪电速递!

Python版本

arr1 = list(map(int, input().strip().split()))
arr2 = list(map(int, input().strip().split()))
beginIndex1 = 0
beginIndex2 = 0
ss1 = []
ss2 = []
flag = [[True for _ in range(len(arr2))] for _ in range(len(arr1))]
for beginIndex1 in range(len(arr1)):
    for beginIndex2 in range(len(arr2)):
        if not flag[beginIndex1][beginIndex2]:
            continue
        newSS1 = [arr1[beginIndex1]]
        newSS2 = [arr2[beginIndex2]]
        index1 = beginIndex1+1
        index2 = beginIndex2+1
        while index1<len(arr1) and index2<len(arr2):
            while index1 < len(arr1) and arr1[index1]-arr1[beginIndex1]<arr2[index2]-arr2[beginIndex2]:
                index1 += 1
            if index1==len(arr1):
                break
            if arr1[index1]-arr1[beginIndex1]==arr2[index2]-arr2[beginIndex2]:
                newSS1.append(arr1[index1])
                newSS2.append(arr2[index2])
                index1 += 1
                index2 += 1
            if index1==len(arr1):
                break
            while index2 < len(arr2) and arr1[index1]-arr1[beginIndex1]>arr2[index2]-arr2[beginIndex2]:
                index2 += 1
            if index2==len(arr2):
                break
            if arr1[index1]-arr1[beginIndex1]==arr2[index2]-arr2[beginIndex2]:
                newSS1.append(arr1[index1])
                newSS2.append(arr2[index2])
                flag[index1][index2] = False
                index1 += 1
                index2 += 1
        if len(newSS1)>len(ss1):
            ss1 = newSS1[:]
            ss2 = newSS2[:]
if len(ss1)<=1:
    print(0)
else:
    print(len(ss1))
    print(' '.join(str(i) for i in ss1))
    print(' '.join(str(i) for i in ss2))

最新的秋招笔试编程题题目、思路以及参考代码已经全部整理好放在【TechGuide】了,私信公众号回复【华为】或者【百度】即可获得最实时的笔试题解啦!

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值