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