拼多多服务器研发笔试编程题总AC1.9
第一题80%
第二题AC
第三题题太长了懒得看了
第四题试着输出个10得到10%
本人比较渣,代码仅供参考
第一题80%
(估计是使用treeset的原因,不能出线重复的糖果,当时想到了懒得改了)
import java.util.Comparator; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.TreeSet; class Bear{ public int key; public int hungry; } class Point{ public int index; public int key; public String toString(){ return "index"+index+"key"+key; } } public class Main { public static Comparator<Point> idComparator = new Comparator<Point>(){ @Override public int compare(Point c1, Point c2) { return (int) (c2.key - c1.key); } }; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n=in.nextInt(); int m=in.nextInt(); int[] candys=new int[m]; TreeSet<Integer> ts = new TreeSet<Integer>(); for(int i=0;i<m;i++){ ts.add(in.nextInt()); } Bear[] bears=new Bear[n]; Queue<Point> priQueue= new PriorityQueue<Point>(7, idComparator); for(int i=0;i<n;i++){ bears[i]=new Bear(); bears[i].key=in.nextInt(); bears[i].hungry=in.nextInt(); Point p=new Point(); p.key=bears[i].key; p.index=i; priQueue.add(p); } while(!priQueue.isEmpty()){ Point p=priQueue.poll(); int index=p.index; while(ts.floor(bears[index].hungry)!=null){ int h=ts.floor(bears[index].hungry); bears[index].hungry-=h; ts.remove(h); } } for(int i=0;i<n;i++){ System.out.println(bears[i].hungry); } } }
第二题AC
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; class TreeNode{ public String name; public List<TreeNode> childs=new ArrayList<TreeNode>(); } public class Main { public static Comparator<TreeNode> idComparator = new Comparator<TreeNode>(){ @Override public int compare(TreeNode s1, TreeNode s2) { return s1.name.compareTo(s2.name); } }; public static void print(TreeNode root,int floor,String pre){ if(root==null){ return; } Collections.sort(root.childs,idComparator); for(int i=0;i<root.childs.size();i++){ TreeNode node=root.childs.get(i); System.out.print(pre); if(i==root.childs.size()-1){ System.out.println("`-- "+node.name); print(node,floor+1,pre+" "); }else{ System.out.println("|-- "+node.name); print(node,floor+1,pre+"| "); } } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n=in.nextInt(); TreeNode[] nodes=new TreeNode[n]; for(int i=0;i<n;i++){ nodes[i]=new TreeNode(); } TreeNode root=null; for(int i=0;i<n;i++){ nodes[i].name=in.next(); int parIndex=in.nextInt(); if(parIndex==-1){ root=nodes[i]; }else{ nodes[parIndex].childs.add(nodes[i]); } } System.out.println(root.name); print(root,0,""); } }