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%,一急躁就解决不了,现在重新看了一下,稍微一改,觉得就挺好的,唉,又浪费了一次机会。
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; } }
添加回复