2022-4-13华为笔试

第一题:模拟 100%
package org.huawei;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=Integer.parseInt(sc.nextLine());
        node[] nums=new node[n];

        PriorityQueue<node> queue1=new PriorityQueue<>(new Comparator<node>() { 
                         public int compare(node o1, node o2) {
                if(o1.cpuCore!=o2.cpuCore){
                    return o1.cpuCore-o2.cpuCore;
                }else if(o1.mem!=o2.mem){
                    return o1.mem-o2.mem;
                }else{
                    return o1.id-o2.id;
                }
            }
        });

        PriorityQueue<node> queue2=new PriorityQueue<>(new Comparator<node>() {         
                        public int compare(node o1, node o2) {
                if(o1.mem!=o2.mem){
                    return o1.mem-o2.mem;
                }else if(o1.cpuCore!=o2.cpuCore){
                    return o1.cpuCore-o2.cpuCore;
                }else{
                    return o1.id-o2.id;
                }
            }
        });

        PriorityQueue<node> queue3=new PriorityQueue<>(new Comparator<node>() { 
                         public int compare(node o1, node o2) {
                return o1.id-o2.id;
            }
        });

        for(int i=0;i<n;i++){
            String[] s=sc.nextLine().split(",");
            nums[i]=new node(Integer.parseInt(s[0]),Integer.parseInt(s[1]),Integer.parseInt(s[2]),Integer.parseInt(s[3]),Integer.parseInt(s[4]));
        }
        String[] ss=sc.nextLine().split(" ");
        int nn=Integer.parseInt(ss[0]);int strategy=Integer.parseInt(ss[1]);int cpuCount=Integer.parseInt(ss[2]);
        int memSize=Integer.parseInt(ss[3]);int cpuArch=Integer.parseInt(ss[4]);int supportNp=Integer.parseInt(ss[5]);



        if(strategy==1){
//            cpu优先
            for(int i=0;i<n;i++){
                if((cpuArch==9||cpuArch==nums[i].cpu)&&(supportNp==2||supportNp==nums[i].isSpeed)&&(nums[i].cpuCore>=cpuCount)&&(nums[i].mem>=memSize)){
                    queue1.offer(nums[i]);
                }
            }
            int cnt=0;
            while (!queue1.isEmpty()) {
                if(cnt>=nn){
                    break;
                }
                node p=queue1.poll();
                queue3.offer(p);
                cnt++;
            }

        }else{
            for(int i=0;i<n;i++){
                if((cpuArch==9||cpuArch==nums[i].cpu)&&(supportNp==2||supportNp==nums[i].isSpeed)&&(nums[i].cpuCore>=cpuCount)&&(nums[i].mem>=memSize)){
                    queue2.offer(nums[i]);
                }
            }
            int cnt=0;
            while (!queue2.isEmpty()) {
                if(cnt>=nn){
                    break;
                }
                node p=queue2.poll();
                queue3.offer(p);
                cnt++;
            }
        }
        System.out.print(queue3.size());
        int[] res=new int[queue3.size()];
        int index=0;
        while (!queue3.isEmpty()) {
            node re=queue3.poll();
            res[index++]=re.id;
        }
        for(int i=0;i<index;i++){
            System.out.print(" "+res[i]);
        }
    }
}
class node{
    int id;
    int cpuCore;
    int mem;
    int cpu;
    int isSpeed;
    public node(int id,int cpuCore,int mem,int cpu,int isSpeed){
        this.id=id;
        this.cpuCore=cpuCore;
        this.mem=mem;
        this.cpu=cpu;
        this.isSpeed=isSpeed;
    }
}            

第二题  贪心吧,不知所云
第三题  乱搜  5%

package org.huawei;

import java.util.*;

public class Test01 {
    static List<int[]> res=new ArrayList<>();
    static List<int[]> rr=new ArrayList<>();
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[] nums=new int[n];
        int sum=0;
        for(int i=0;i<n;i++){
            nums[i]=sc.nextInt();
            sum+=nums[i];
        }
//        System.out.println("zui"+sum);
        if(sum%2!=0){
            System.out.println(-1);
        }else{
            int re=sum/2;
            boolean[] vis=new boolean[n];
            dfs(nums, re, 0, vis);
//            System.out.println("daxia"+res.size());
            if(res.size()==0){
                System.out.println(-1);
                return;
            }
            System.out.println(re);


            List<Integer> r=new ArrayList<>();
            Set<Integer> set=new HashSet<>();
            for(int i=0;i<res.size();i++){
                set.add(res.get(i)[1]);
            }
            for(int i=0;i<n;i++){
                if(!set.contains(i)){
                    r.add(nums[i]);
                }
            }

//            System.out.println("大小"+res.size());
            List<Integer> rrr=new ArrayList<>();
            for(int i=0;i<res.size();i++){
                rrr.add(res.get(i)[0]);
            }
            Collections.sort(rrr);
            Collections.sort(r);
            Collections.reverse(rrr);
            Collections.reverse(r);

            for(int i=0;i<r.size();i++){
                if(i==r.size()-1){
                    System.out.println(r.get(i));
                }else{
                    System.out.print(r.get(i)+" ");
                }
            }
            for(int i=0;i<rrr.size();i++){
                if(i==(rrr.size()-1)){
                    System.out.print(rrr.get(i));
                }else{
                    System.out.print(rrr.get(i)+" ");
                }
            }
//            System.out.println("========");

        }
    }
    public static void dfs(int[] nums, int re, int cur,boolean[] vis){
        if(cur==re){
            res=new ArrayList<>(rr);
            return  ;
        }
        if(cur>re){
            return;
        }
        //通过回溯
        for(int i=0;i<nums.length;i++){
            if(!vis[i]){
                vis[i]=true;
                cur+=nums[i];
                rr.add(new int[]{nums[i],i});
                dfs(nums,re,cur,vis);
                vis[i]=false;
                cur-=nums[i];
                rr.remove(rr.size()-1);
            }
        }
    }
}


全部评论
// we have defined the necessary header files here for this problem. // If additional header files are needed in your program, please import here. #include<bits/stdc++.h> using namespace std; int main() {     int n;     cin >> n;     int ans = 0;     vector<pair<int, int>> tmp;     for(int i = 0; i < n; ++i){         int a, b;         cin >> a >> b;         tmp.push_back(make_pair(a, b));     }     sort(tmp.begin(), tmp.end(), [](pair<int, int>& a, pair<int, int>& b){ return a.first == b.first ? a.second > b.second : a.first < b.first; });          int start = 0;     for(int i = 1; i <= 7 * 105 && start < n; ++i){         for(int j = start; j < n; ++j){             if(tmp[j].first >= i){                 ans += tmp[j].second;                 start = j + 1;                 break;             }         }     }     cout << ans;     return 0; } 第二题25%,不知道哪里有问题
1 回复 分享
发布于 2022-04-13 21:29
需要华为机试题库私聊
点赞 回复 分享
发布于 2022-05-08 22:13
第一题忘了最终的排序,寄了
点赞 回复 分享
发布于 2022-04-13 22:57
楼主第二题多少分呀,我感觉想法是对的,提交答案是0
点赞 回复 分享
发布于 2022-04-13 21:22

相关推荐

原来已经一年了,因为没有加任何实验室没有学长学姐带,再一次偶然的机会下刷到我们学校的牛肉哥,和他聊天之后发现他也没加实验室能进大厂,我就燃起了希望,去年大概&nbsp;4&nbsp;月份找好路线&nbsp;零基础&nbsp;开始学&nbsp;5&nbsp;月背八股和开始刷算法很难受&nbsp;7-8&nbsp;月焦虑躯体化害怕找不到实习&nbsp;9&nbsp;月找到一家像样的小厂去实习了&nbsp;4&nbsp;个月大三上期末考试结束之后&nbsp;1&nbsp;月份回来边实习边准备工作压力很大&nbsp;当时只有字节、百度、商汤的面试,字节三面挂了,百度&nbsp;oc,商汤&nbsp;二面挂(差评&nbsp;无效面试),之后来深圳百度实习之后还是觉得不甘心一直没把算法和八股扔下一直在准备,百度实习的时候&nbsp;mt&nbsp;交给我一个特别重要的工作数据库迁移(特别感谢&nbsp;mt&nbsp;,这个需求学到了很多东西处理了一堆线上问题),本来看着暑期他们面试都很困难,然后听说百度要涨实习薪资(然而&nbsp;5&nbsp;月并没有涨),就想着留在百度吧也懒得面试了,4&nbsp;月&nbsp;20&nbsp;多的时候字节&nbsp;hr&nbsp;打电话约面问我要不要尝试一下询问了&nbsp;1&nbsp;月份三面为啥会挂有没有学习&nbsp;ai&nbsp;知识(因为字节这边后端岗位偏&nbsp;ai),我来到百度之后全面拥抱&nbsp;AI&nbsp;也认识了我的好兄弟&nbsp;X&nbsp;哥,他在百度&nbsp;XX&nbsp;部门&nbsp;Agent&nbsp;实习,他属于是我&nbsp;Agent&nbsp;的启蒙老师,来百度之后一直在了解&nbsp;AI&nbsp;这一块,我就接受了字节的面试,一面的时候&nbsp;20&nbsp;分钟实习拷打然后突然说&nbsp;30&nbsp;分钟代码考核我心就凉了以为是&nbsp;kpi,算法题是手撕高并发安全下的令牌桶限流器,我写了整整&nbsp;80&nbsp;多行代码最后也写出来了,但是从来没看到过出这种题能&nbsp;oc&nbsp;的我也就不管了,后边面试也是很顺利但是流程有点长可能一直在横向吧总结结果是好的!!!感谢这一年努力的自己和遇到的各位互联网大佬分享的知识!!!ps&nbsp;图二纯感慨&nbsp;(觉得🍬请不要喷我)欢迎大家一起交流学习呀!!!!
点赞 评论 收藏
分享
评论
6
16
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务