拼多多服务器研发笔试编程题总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,"");
    }
}

全部评论
同print 10
点赞 回复 分享
发布于 2017-09-02 17:05
厉害了 老哥
点赞 回复 分享
发布于 2017-09-02 17:07
666  
点赞 回复 分享
发布于 2017-09-02 17:08
66666,第二题那个输出格式有毒,我硬是没拼出来。第三题很简单,可惜大量时间花在第二题上了,代码硬是没写完
点赞 回复 分享
发布于 2017-09-02 17:09
第一题AC代码:  import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in);      int bearNum=sc.nextInt();      int candyNum=sc.nextInt();      Integer[] candyEnergy=new Integer[candyNum];      for(int i=0;i<candyEnergy.length;i++){      candyEnergy[i]=sc.nextInt();      }      Bear[] bearArray =new Bear[bearNum];      for(int i=0;i<bearArray.length;i++){      int a=sc.nextInt();      int b=sc.nextInt();      bearArray[i]=new Bear(i,a,b);      }         Arrays.sort(bearArray,new Comparator<Bear>(){ @Override public int compare(Bear o1, Bear o2) { // TODO Auto-generated method stub return o2.war-o1.war; }                  });                Arrays.sort(candyEnergy,new Comparator<Integer>(){ @Override public int compare(Integer o1, Integer o2) { // TODO Auto-generated method stub return o2-o1; }                  });                        boolean[] flag=new boolean[candyNum];         for(int i=0;i<bearNum;i++){                               for(int j=0;j<candyNum;j++){              if(!flag[j]){                           if(bearArray[i].hungry>=candyEnergy[j]){              bearArray[i].hungry=bearArray[i].hungry-candyEnergy[j];              flag[j]=true;              }                           }             }                           }         Arrays.sort(bearArray,new Comparator<Bear>(){    @Override    public int compare(Bear o1, Bear o2) {    // TODO Auto-generated method stub    return o1.index-o2.index;    }                      });         for(int i=0;i<bearArray.length;i++){          System.out.println(bearArray[i].hungry);         } } static class Bear{    int index;    int war;    int hungry;    public Bear(int i,int a,int b){    index=i;    war=a;    hungry=b;    } } } 
点赞 回复 分享
发布于 2017-09-02 17:11
print 10厉害了。我把样例输出print 4  结果0%
点赞 回复 分享
发布于 2017-09-02 17:11
第一题没AC估计是因为我用treeset做的,不能有重复的糖果,当时想到了,但是不想改了。。。
点赞 回复 分享
发布于 2017-09-02 17:15
哈哈哈,我也是输出个10,从4 6 8 到10,一个个试过来,真是羞耻啊
点赞 回复 分享
发布于 2017-09-02 17:40
楼主第二题能用你的样例测一下这个代码么,我用你的代码跑了一下,和我的输出是相同的,不知道错了哪个 样例。 import java.util.*; class Node { String name; int parent; ArrayList<Node> children; Node() { this.children = new ArrayList<>(); } } public class Mainb { public static void dfs(Node nodes, String s) { if (nodes.children.size() == 0) return; for (int i = 0; i < nodes.children.size(); i++) { Node node = nodes.children.get(i); String s1; if (i != nodes.children.size() - 1) s1 = s + "|"; else s1 = s + "`"; System.out.println(s1 + "-- " + node.name); if (node.children.size() > 0 && i != nodes.children.size() - 1) { String ss = ""; if (s == "") ss = s + " |"; else ss = s + "| "; dfs(node, ss); } else if (node.children.size() > 0 && i == nodes.children.size() - 1) { String ss = ""; if (s == "") ss = s + " "; else ss = s + " "; dfs(node, ss); } } } public static void main(String [] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Node [] nodes = new Node[n]; Node root; HashMap<Integer, Node> map = new HashMap<>(); for (int i = 0; i < n; i++) { nodes[i] = new Node(); nodes[i].name = sc.next(); nodes[i].parent = sc.nextInt(); if (i == 0) root = nodes[i]; map.put(i, nodes[i]); } for (int i = 1; i < n; i++) { Node parent = map.get(nodes[i].parent); if (parent != null) parent.children.add(nodes[i]); } for (int i = 0; i < n; i++) { Collections.sort(nodes[i].children,new Comp()); } System.out.println(nodes[0].name); String s = ""; dfs(nodes[0], s); } } class Comp implements Comparator { public int compare(Object s11, Object s22) { Node node1 = (Node) s11; Node node2 = (Node) s22; String s1 = node1.name; String s2 = node2.name; int n1 = s1.length(); int n2 = s2.length(); int min = Math.min(n1, n2); for (int i = 0; i < min; i++) { char c1 = s1.charAt(i); char c2 = s2.charAt(i); if (c1 != c2) { c1 = Character.toUpperCase(c1); c2 = Character.toUpperCase(c2); if (c1 != c2) { c1 = Character.toLowerCase(c1); c2 = Character.toLowerCase(c2); if (c1 != c2) { // No overflow because of numeric promotion return c1 - c2; } } } } return n1 - n2; } } /* 10 my-app -1 src 0 main 1 java 2 resources 2 webapp 2 test 1 java 6 resources 6 pom.xml 0 my-app |-- pom.xml `-- src |-- main | |-- java | |-- resources | `-- webapp `-- test |-- java `-- resources */
点赞 回复 分享
发布于 2017-09-02 17:52

相关推荐

2024-11-11 09:31
香港中文大学 前台
Hello_WordN:咱就是说,除了生命其他都是小事,希望面试官平安,希望各位平时也多注意安全
点赞 评论 收藏
分享
怎么起名字:学历不足,
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务