去哪儿三道ac代码
发布于 2017-09-26 11:16 2522 次浏览 1 赞 来自 资源分享  

渣渣好不容易又一次全ac,希望有个面试机会吧。


第一题,贪心算法,每次去找尽量最大的钱

package qunaer;

import java.util.Scanner;
public class Main2 {
    static int[] money = {1, 5, 10, 50, 100, 500};
    static int[] count = new int[6];
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        for (int i=0; i<6; i++){
            count[i] = in.nextInt();
        }
        int allMoney = in.nextInt();

        int ans = 0;
        int index = 5;
        while (allMoney != 0 && index >= 0){
            if (allMoney >= money[index] && count[index]>0){
                allMoney -= money[index];
                count[index] = count[index] - 1;
                ans++;
            } else {
                index--;
            }
        }
        if (allMoney == 0){
            System.out.println(ans);
        } else {
            System.out.println("NOWAY");
        }
    }
}


第二题,邻居矩阵表示图,记录每个结点的入度和权重,每次输出入度为0的结点中权重最大的点,然后将该点连着的点的入度值都减一。

package qunaer;

import java.util.Scanner;
public class Main3 {
    static int[][] graph;
    static int[] countTo;   //记录入度
    static int[] weight;    //权重
    static int n,e;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        e = in.nextInt();

        graph = new int[n+1][n+1];
        weight = new int[n+1];
        countTo = new int[n+1];

        //输入权重
        int seq;
        for (int i=1; i<=n; i++){
            seq = in.nextInt();
            weight[seq] = in.nextInt();
        }

        //输入边,并累加入度
        int s,t;
        for (int i=1; i<=e; i++){
            s = in.nextInt();
            t = in.nextInt();
            graph[s][t] = 1;
            countTo[t] = countTo[t] + 1;
        }

        printAll();

    }
    static void printAll(){
        int printCount = 1;
        while (printCount <= n){
            int maxWeight = 0, index = 0;
            for (int i=1; i<=n; i++){
                if (countTo[i]==0 && weight[i]>=maxWeight){
                    index = i;
                    maxWeight = weight[i];
                }
            }
            if (printCount == n)    System.out.println(index);
            else System.out.print(index + " ");
            countTo[index] = -1;
            for (int i=1; i<=n; i++){
                if (graph[index][i] == 1){
                    countTo[i]--;
                }
            }

            printCount++;
        }
    }
}


第三题,模拟题,类似于自己实现LinkedHashMap的put,get操作。

package qunaer;

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();
        LRUList<String, String> lruList = new LRUList(m);
        String op, key, value;
        for (int i=0; i<n; i++){
            op = in.next().trim();
            if (op.equals("put")){
                key = in.next().trim();
                value = in.next().trim();
                lruList.put(key,value);
            } else {
                key = in.next().trim();
                System.out.println(lruList.get(key));
            }
        }
    }


}

class LRUList<K,V>{
    static class Entry<K, V>{
        K key;
        V value;
        Entry<K,V> next;

        public Entry(K key, V value, Entry<K, V> next) {
            this.key = key;
            this.value = value;
            this.next = next;
        }
    }

    int size = 0;
    int capacity;

    //链表头空节点
    Entry<K,V> first = new Entry<>(null,null,null);


    public LRUList(int capacity) {
        this.capacity = capacity;
    }

    void put(K key, V value){
        Entry<K,V> tmp = first.next;
        Entry<K,V> prev = first;

        while (tmp != null && !tmp.key.equals(key)){
            tmp = tmp.next;
            prev = prev.next;
        }

        //如果找到了相同的,就删除该结点并且把新的结点放到头结点之后
        if (tmp != null && tmp.key.equals(key)){
            prev.next = tmp.next;
            tmp = null;

            insertToFirst(key,value);
            return;
        }

        //如果没有找到
        //如果已经满了  size == capacity,就删除最后一个节点,然后再插入到头结点
        if (size == capacity){
            Entry t = first.next;
            Entry p = first;
            while (t.next != null){
                t = t.next;
                p = p.next;
            }
            p.next = null;
            t = null;

            insertToFirst(key, value);
        } else {//如果还没有满,就直接插入到头结点
            insertToFirst(key,value);
            size++;
        }


    }

    void insertToFirst(K key, V value){
        Entry e = new Entry(key, value, first.next);
        first.next = e;
    }

    V get(K key){
        Entry<K,V> tmp = first.next;
        while (tmp != null && !tmp.key.equals(key)){
            tmp = tmp.next;
        }
        if (tmp == null){
            return null;
        } else {
            return tmp.value;
        }
    }
}


4 条回复
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();	//缓存的长度
		int n = sc.nextInt();	//输入的行数
		sc.nextLine();
		
		List<String> temp = new ArrayList<String>();
		List<String> value = new ArrayList<String>();
		for(int i=0; i<n; i++) {
			String str = sc.nextLine();
			String[] strs = str.split(" ");
			
			if(strs[0].equals("put")) {
				if(!temp.contains(strs[1])) {
					if(temp.size() == m) {
						temp.remove(0);
						temp.add(strs[1]);
						value.remove(0);
						value.add(strs[2]);
					} else {
						temp.add(strs[1]);
						value.add(strs[2]);
					}
				} else {
					int index = temp.indexOf(strs[1]);
					temp.remove(index);
					temp.add(strs[1]);
					value.remove(index);
					value.add(strs[2]);
				}
			} else if(strs[0].equals("get")) {
				if(temp.contains(strs[1])) {
					int index = temp.indexOf(strs[1]);
					System.out.println(value.get(index));
				} else {
					System.out.println("null");
				}
			}
		}
		sc.close();
	}

}

上午做题的时候,第三题我也是这种方式做的,最后老是20%,一急躁就解决不了,现在重新看了一下,稍微一改,觉得就挺好的,唉,又浪费了一次机会。

2017-09-26 16:09
1
coder_ckVWDXlK 回复 coder_X23KMBCM

你get后也是重新使用,所以需要把读到的数据重新排在最后一个位置

2017-09-28 15:43
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class DAG {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n= in.nextInt();
		int e= in.nextInt();
		LinkedList<Node> nodes = new LinkedList<Node>();
		LinkedList<Edge> edges = new LinkedList<Edge>();
		for(int i=0;i<n;i++){
			nodes.add(new Node(in.nextInt(),in.nextInt()));
		}
		for(int i=0;i<e;i++){
			edges.add(new Edge(in.nextInt(),in.nextInt()));
		}
		ArrayList<Node> result = fun(edges,nodes);
		for(Node node:result){
			System.out.print(node.id+" ");
		}
		System.out.println();
	}	
	static ArrayList<Node> fun(LinkedList<Edge> lastEdges,LinkedList<Node> lastNode){
		ArrayList<Node> result = new ArrayList<Node>();
		while(!lastEdges.isEmpty()){
			ArrayList<Node> tempResult = next(lastEdges,lastNode);
			Node node = getMax(tempResult);
			result.add(node);
			remove(lastEdges, lastNode, node);
		}
		result.add(lastNode.getFirst());
		return result;
	}	
	static Node getMax(ArrayList<Node> tempResult){
		Collections.sort(tempResult, new Comparator<Node>(){

			public int compare(Node o1, Node o2) {
				// TODO Auto-generated method stub
				return o1.weight-o2.weight;
			}
			
		});
		return tempResult.get(tempResult.size()-1);
	}
	static void remove(LinkedList<Edge> lastEdges,LinkedList<Node> lastNode,Node node){
		lastNode.remove(node);
		ArrayList<Edge> result = new ArrayList<Edge>();
		for(Iterator<Edge> it1 = lastEdges.iterator();it1.hasNext();){
			Edge edge = it1.next();
			if(edge.from==node.id)result.add(edge);
		}
		lastEdges.removeAll(result);
	}	
	static ArrayList<Node> next(LinkedList<Edge> lastEdges,LinkedList<Node> lastNode){
		ArrayList<Node> result = new ArrayList<Node>();
		for(Iterator<Node> it = lastNode.iterator();it.hasNext();){
			Node node = it.next();
			boolean canRemove=true;
			for(Iterator<Edge> it1 = lastEdges.iterator();it1.hasNext();){
				Edge edge = it1.next();
				if(edge.to==node.id)canRemove=false;
			}
			if(canRemove)
				result.add(node);
		}
		return result;
	}
}
class Node{
	int weight;
	int id;
	Node(int id,int weight){
		this.id=id;
		this.weight=weight;
	}
}
class Edge{
	int from;
	int to;
	Edge(int from,int to){
		this.from=from;
		this.to=to;
	}
}


2017-09-28 16:25

题目呢?有链接吗?


2017-09-30 12:22
添加回复
回到顶部