import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; public class MST { public static void kruskal(int []V,Edge[] E) { Arrays.sort(E); ArrayList<HashSet> sets=new ArrayList<HashSet>(); for(int i=0;i<V.length;i++) { HashSet set=new HashSet(); set.add(V[i]); sets.add(set); } int sum_w=0; for(int i=0;i<E.length;i++) { int start=E[i].i; int end=E[i].j; int w=E[i].w; int set_start=-1;int set_end=-2; for(int j=0;j<sets.size();j++) { HashSet set=sets.get(j); if(set.contains(start)) set_start=j; if(set.contains(end)) set_end=j; } if(set_start!=set_end) { sum_w+=E[i].w; HashSet set=sets.get(set_end); sets.remove(set_end); HashSet set1=sets.get(set_start); sets.remove(set_start); set1.addAll(set); sets.add(set1); } } System.out.println(sum_w); } public static void main(String[] args) { // TODO Auto-generated method stub int []V= {1,2,3,4,5,6}; Edge[]E=new Edge[10]; E[0]=new Edge(1,2,6); E[1]=new Edge(1,3,1); E[2]=new Edge(1,4,5); E[3]=new Edge(2,3,5); E[4]=new Edge(2,5,3); E[5]=new Edge(3,4,5); E[6]=new Edge(3,5,6); E[7]=new Edge(3,6,4); E[8]=new Edge(4,6,2); E[9]=new Edge(5,6,6); kruskal(V,E); } public static class Edge implements Comparable{ public int i,j,w; public Edge(int i,int j,int w) { this.i=i; this.j=j; this.w=w; } @Override public int compareTo(Object o) { Edge to=(Edge) o; if(this.w>to.w)return 1; else if(this.w==to.w)return 0; else return -1; } @Override public String toString() { return "start="+i+"||end="+j+"||weight="+w; } } }
public class ShortestPath { public static void shortestPath(int graph[][], int start, int n) { int[] d = new int[n]; int pre[] = new int[n]; boolean[] finish = new boolean[n]; int noEdge = Integer.MAX_VALUE; int noPre = -1; int noLink = 0; // 初始化 for (int i = 0; i < n; i++) { if (graph[start][i] == noLink) { d[i] = noEdge; pre[i] = noPre; } else { d[i] = graph[start][i]; pre[i] = start; } finish[i] = false; } finish[start] = true; pre[start] = noPre; d[start] = noEdge; for (int i = 0; i < n; i++) { int minj = -1, minValue = Integer.MAX_VALUE; // 找最小的d值 for (int j = 0; j < n; j++) { if (!finish[j]) { if (d[j] < minValue) { minj = j; minValue = d[j]; } } } if (minj != -1) { int count = minj; // 输出最短路径 System.out.print(minj + 1); while (pre[count] != noPre) { System.out.print(" " + (pre[count] + 1)); count = pre[count]; } System.out.println(); } else { // System.out.println("不再存在从v0可到达的最短路径"); break; } // 更新d pre finish finish[minj] = true; for (int j = 0; j < n; j++) { if (!finish[j]) { if (graph[minj][j] != noLink && (d[minj] + graph[minj][j] < d[j])) { pre[j] = minj; d[j] = d[minj] + graph[minj][j]; } } } } } public static void main(String[] args) { // TODO Auto-generated method stub int[][] graph = { { 0, 2, 4, 3 }, { 2, 0, 2, 0 }, { 4, 2, 0, 1 }, { 3, 0, 1, 0 } }; shortestPath(graph, 0, 4); } }
ccf201709
0901打酱油(100分)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int money=scan.nextInt(); scan.close(); int num_buy=0; if (money%50==0) { num_buy=money/50*7; } else { num_buy=money%50/30+money%50/10+money/50*7; } System.out.println(num_buy); } }
0902公共钥匙盒(100分)
import java.util.Arrays; import java.util.Scanner; public class Main_02 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int k = scan.nextInt(); int[] key = new int[n]; for (int i = 0; i < n; i++) { key[i] = i + 1; } int[][] tea_use = new int[k][4]; for (int i = 0; i < k; i++) { tea_use[i][0] = scan.nextInt(); tea_use[i][1] = scan.nextInt(); tea_use[i][2] = scan.nextInt(); tea_use[i][3] = tea_use[i][1] + tea_use[i][2]; } int time = 0; int max = 0; for (int i = 0; i < k; i++) { if (tea_use[i][3] > max) { max = tea_use[i][3]; } } int[] ret = new int[k]; while (time <= max) { for (int i = 0; i < k; i++) { ret[i] = 0; } int m = 0; for (int i = 0; i < k; i++) { if (tea_use[i][3] == time) { ret[m++] = tea_use[i][0]; } } Arrays.sort(ret); for (int i = 0; i < k; i++) { if (ret[i] != 0) { for (int j = 0; j < n; j++) { if (key[j] == 0) { key[j] = ret[i]; ret[i] = 0; break; } } } } for (int i = 0; i < k; i++) { if (tea_use[i][1] == time) { for (int j = 0; j < n; j++) { if (key[j] == tea_use[i][0]) { key[j] = 0; } } } } time++; } for (int i = 0; i < n; i++) { System.out.print(key[i] + " "); } } }
0903Json查询()
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main_03 { static Map<String,String> Json=new HashMap<String,String>(); static String keyVal=""; static boolean key=false; public static void handle(String line) { for(int i=0;i<line.length();i++) { char c=line.charAt(i); switch(c) { case '{': Json.put(keyVal, "OBJECT"); key=true; break; case '}': if(!keyVal.equals("")) { int j; for(j=keyVal.length()-1;j>=0;j--) { if(keyVal.charAt(j)=='.') { break; } } if(j<0) keyVal=""; else keyVal=keyVal.substring(0,j); } break; case '"': String temp=""; for(i=i+1;i<line.length();i++) { if(line.charAt(i)=='\') { i++; temp+=line.charAt(i); } else if(line.charAt(i)=='"') break; else temp+=line.charAt(i); } if(key) { if(!keyVal.equals("")) keyVal+="."; keyVal+=temp; } else { Json.put(new String(keyVal), "STRING "+temp); int j=keyVal.lastIndexOf("."); if(j<0) keyVal=""; else keyVal=keyVal.substring(0,j); } break; case ':': key=false; break; case ',': key=true; break; } } } public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan=new Scanner(System.in); int n=scan.nextInt(); int m=scan.nextInt(); scan.nextLine(); for(int i=0;i<n;i++) { handle(scan.nextLine().replace(" ", "")); } for(int i=0;i<m;i++) { String query=scan.nextLine(); if(!Json.containsKey(query)) System.out.println("NOTEXIST"); else System.out.println(Json.get(query)); } } }
0904通信网络(100分)
import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Scanner; public class Main_04 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int m = scan.nextInt(); List[] send = new ArrayList[n + 1]; List[] receive = new ArrayList[n + 1]; for (int i = 0; i < n + 1; i++) { send[i] = new ArrayList(); receive[i] = new ArrayList(); } for (int j = 0; j < m; j++) { int s = scan.nextInt(); int t = scan.nextInt(); send[s].add(t); receive[t].add(s); } boolean all[][] = new boolean[n + 1][n + 1]; for (int i = 0; i < n + 1; i++) { all[i][i] = true; } Queue q = new LinkedList(); boolean[] flags; for (int i = 1; i < n + 1; i++) { q.add(i); flags = new boolean[n + 1]; while (!q.isEmpty()) { int temp = (int) q.poll(); if (!flags[temp]) { for (int j = 0; j < send[temp].size(); j++) { int next = (int) send[temp].get(j); q.add(next); } flags[temp] = true; all[i][temp] = true; } } } for (int i = 1; i < n + 1; i++) { q.add(i); flags = new boolean[n + 1]; while (!q.isEmpty()) { int temp = (int) q.poll(); if (!flags[temp]) { for (int j = 0; j < receive[temp].size(); j++) { int next = (int) receive[temp].get(j); q.add(next); } flags[temp] = true; all[i][temp] = true; } } } int num = 0; for (int i = 1; i < n + 1; i++) { boolean knowAll = true; for (int j = 1; j < n + 1; j++) { if (!all[i][j]) { knowAll = false; break; } } if (knowAll) { num++; } } System.out.println(num); } }
0905除法(30分)
import java.util.Scanner; public class Main_05 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int m = scan.nextInt(); int input1[] = new int[n + 1]; for (int i = 1; i < n + 1; i++) { input1[i] = scan.nextInt(); } int result[] = new int[m]; int k = 0; for (int i = 0; i < m; i++) { int opt = scan.nextInt(); int l = scan.nextInt(); int r = scan.nextInt(); if (opt == 1) { int v = scan.nextInt(); for (int j = l; j < r + 1; j++) { if (input1[j] % v == 0) { input1[j] /= v; } } } if (opt == 2) { int sum = 0; for (int j = l; j < r + 1; j++) { sum += input1[j]; } System.out.println(sum); } } } }
ccf201703
0301分蛋糕(100分)
package test_201703; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan=new Scanner(System.in); int n=scan.nextInt(); int k=scan.nextInt(); int weights[]=new int[n]; for(int i=0;i<n;i++) { weights[i]=scan.nextInt(); } int people=0; int sum=0; for(int i=0;i<n;i++) { sum+=weights[i]; if(sum>=k||i==n-1) { people++; sum=0; } } System.out.println(people); } }
0302学生排队(100分)
package test_201703; import java.util.Scanner; public class Main_02 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int n=scan.nextInt(); int opt_num=scan.nextInt(); int student[]=new int[n+1]; int pos[]=new int[n+1]; for(int i=1;i<n+1;i++) { student[i]=i; pos[i]=i; } for(int i=0;i<opt_num;i++) { int stu=scan.nextInt(); int dis=scan.nextInt(); //向后移 if(dis>0) { int temp=student[pos[stu]]; int j=0; for(j=pos[stu];j<pos[stu]+dis;j++) { student[j]=student[j+1]; pos[student[j+1]]-=1; } pos[stu]+=dis; student[j]=temp; } //向前移 else { int temp=student[pos[stu]]; int j=0; for(j=pos[stu];j>pos[stu]+dis;j--) { student[j]=student[j-1]; pos[student[j-1]]+=1; } pos[stu]+=dis; student[j]=temp; } } for(int i=1;i<n+1;i++) { System.out.print(student[i]+" "); } } }
0304地铁修建(80分)运行超时。借鉴http://blog.csdn.net/eternity666/article/details/68974954
import java.util.ArrayList; import java.util.PriorityQueue; import java.util.Scanner; public class Main_04 { static int maxN = 100001; static int maxValue = Integer.MAX_VALUE; static int costo[] = new int[maxN]; static ArrayList<Edge>[] G = new ArrayList[maxN]; static boolean vis[] = new boolean[maxN]; public static void shortestPath(int start, int n) { PriorityQueue<Node> pq = new PriorityQueue<Node>(); for (int i = 0; i <= n; i++) { costo[i] = maxValue; vis[i] = false; } vis[0] = true; costo[start] = 0; pq.add(new Node(start, 0)); Node temp; while (!pq.isEmpty()) { temp = pq.poll(); int v = temp.v; if (vis[v]) continue; vis[v] = true; for (int i = 0; i < G[v].size(); i++) { int target = G[v].get(i).target; int cost = G[v].get(i).cost; int maxCost = Math.max(cost, costo[v]); if (!vis[target] && costo[target] > maxCost) { costo[target] = maxCost; pq.add(new Node(target, costo[target])); } } } System.out.println(costo[n]); } public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 0; i < maxN; i++) { G[i] = new ArrayList<Edge>(); } Scanner s = new Scanner(System.in); int n = s.nextInt(); int m = s.nextInt(); for (int i = 0; i < m; i++) { int a = s.nextInt(); int b = s.nextInt(); int c = s.nextInt(); G[a].add(new Edge(b, c)); G[b].add(new Edge(a, c)); } shortestPath(1, n); } public static class Node implements Comparable { public int v, dis; public Node(int v, int dis) { this.v = v; this.dis = dis; } @Override public int compareTo(Object o) { // TODO Auto-generated method stub Node n = (Node) o; if (this.dis > n.dis) return 1; else if (this.dis == n.dis) return 0; else return -1; } } public static class Edge { public int target, cost; public Edge(int target, int cost) { this.target = target; this.cost = cost; } } }
也可借鉴https://www.cnblogs.com/freinds/p/6742618.html
ccf201612
1201中间数(100分)
package ccf_201612; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s=new Scanner(System.in); int n=s.nextInt(); List l=new ArrayList(); for(int i=0;i<n;i++) { l.add(s.nextInt()); } Collections.sort(l); if(n==1) { System.out.println(l.get(0)); return; } if(l.size()%2==0) { int mid=(int) l.size()/2-1; int mid_num=(int) l.get(mid); int num=0; for(int j=0;j<l.size();j++) { if((int)l.get(j)==mid_num) num++; } if(num%2==0) { System.out.println(l.get(mid)); } else System.out.println(-1); } if(l.size()%2==1) { int mid=(int) (l.size()+1)/2-1; int mid_num=(int) l.get(mid); int num=0; for(int j=0;j<l.size();j++) { if((int)l.get(j)==mid_num) num++; } if(num%2==1) { System.out.println(l.get(mid)); } else System.out.println(-1); } } }
1202工资计算(100分)
package ccf_201612; import java.util.Scanner; public class Main_02 { public static int algoth(int all) { int after=0; if(all<=3500) { after=all; return after; } all-=3500; if(all<=1500) { after= (int) (all*0.97); } if(1500<all&&all<=4500) { after=(int) (all-(all-1500)*0.1-1500*0.03); } if(4500<all&&all<=9000) { after=(int) (all-(all-4500)*0.2-3000*0.1-1500*0.03); } if(9000<all&&all<=35000) { after=(int) (all-(all-9000)*0.25-4500*0.2-3000*0.1-1500*0.03); } if(35000<all&&all<=55000) { after=(int) (all-(all-35000)*0.3-26000*0.25-4500*0.2-3000*0.1-1500*0.03); } if(55000<all&&all<=80000) { after=(int) (all-(all-55000)*0.35-20000*0.3-26000*0.25-4500*0.2-3000*0.1-1500*0.03); } if(all>80000) { after=(int) (all-(all-80000)*0.45-25000*0.35-20000*0.3-26000*0.25-4500*0.2-3000*0.1-1500*0.03); } return after+3500; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner s=new Scanner(System.in); int money=s.nextInt(); int after[]=new int[100001]; for(int i=0;i<100001;i++) { after[i]=0; } for(int i=1;i<=100000;i++) { int aft=algoth(i); if(i%100==0) { after[aft]=i; } } System.out.println(after[money]); } }
ccf201609
0901最大波动(100分)
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s=new Scanner(System.in); int n=s.nextInt(); List l=new ArrayList(); int input[]=new int[n]; int first=s.nextInt(); for(int i=0;i<n-1;i++) { int second=s.nextInt(); int abs=Math.abs(first-second); l.add(abs); first=second; } Collections.sort(l); int max=(int) l.get(l.size()-1); System.out.println(max); } }
0902火车购票(90分)
90分
import java.util.Scanner; public class Main_02 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s = new Scanner(System.in); int n = s.nextInt(); int seat[][] = new int[20][5]; int remain[] = new int[20]; for (int i = 0; i < 20; i++) { seat[i][0] = i * 5 + 1; seat[i][1] = i * 5 + 2; seat[i][2] = i * 5 + 3; seat[i][3] = i * 5 + 4; seat[i][4] = i * 5 + 5; } for (int i = 0; i < 20; i++) { remain[i] = 5; } int ticket[] = new int[n]; for (int i = 0; i < n; i++) { ticket[i] = s.nextInt(); } for (int i = 0; i < n; i++) { int num = ticket[i]; boolean flag = false; for (int j = 0; j < 20; j++) { if (remain[j] >= num) { for (int k = 0; k < num; k++) { System.out.print(seat[j][5 - remain[j]] + " "); remain[j] -= 1; } System.out.println(); flag = true; break; } } if (!flag) { for (int j = 0; j < 20; j++) { for (int k = 0; k < remain[j]; k++) { System.out.print(seat[j][5 - remain[j]] + " "); remain[j] -= 1; num--; } if (num == 0) { System.out.println(); break; } } } } } }
测试用例如下时出错:
输入: 21 4 4 3 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 4 输出: 1 2 3 4 6 7 8 9 11 12 13 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 5 10 14 最后一行应为5 10 14 15
出错代码:
for (int k = 0; k < remain[j]; k++) { System.out.print(seat[j][5 - remain[j]] + " "); //此处改变了remain[j] remain[j] -= 1; num--; if (num == 0) { System.out.println(); flag=true; break; } }
java满分代码:
import java.util.Scanner; public class Main_02 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s = new Scanner(System.in); int n = s.nextInt(); int seat[][] = new int[20][5]; int remain[] = new int[20]; for (int i = 0; i < 20; i++) { seat[i][0] = i * 5 + 1; seat[i][1] = i * 5 + 2; seat[i][2] = i * 5 + 3; seat[i][3] = i * 5 + 4; seat[i][4] = i * 5 + 5; } for (int i = 0; i < 20; i++) { remain[i] = 5; } int ticket[] = new int[n]; for (int i = 0; i < n; i++) { ticket[i] = s.nextInt(); } for (int i = 0; i < n; i++) { int num = ticket[i]; boolean flag = false; for (int j = 0; j < 20; j++) { if (remain[j] >= num) { for (int k = 0; k < num; k++) { System.out.print(seat[j][5 - remain[j]] + " "); remain[j] -= 1; } System.out.println(); flag = true; break; } } if (!flag) { for (int j = 0; j < 20; j++) { int r=remain[j]; for (int k = 0; k < r; k++) { System.out.print(seat[j][5 - remain[j]] + " "); remain[j] -= 1; num--; if (num == 0) { System.out.println(); flag=true; break; } } if(flag) break; } } } } }
0904:交通规划 (100分) 借鉴 ccf交通规划 利用dijkstra和优先队列 c++
(借鉴 java
http://blog.csdn.net/moilk_nepho/article/details/52950546
)
import java.util.ArrayList; import java.util.PriorityQueue; import java.util.Scanner; public class Main_4 { static int maxN = 10001; static int maxValue = Integer.MAX_VALUE; static ArrayList<Edge> G[] = new ArrayList[maxN]; static boolean marked[] = new boolean[maxN]; static int disto[] = new int[maxN]; static int costo[] = new int[maxN]; public static void dijkstra(int start, int n) { for (int i = 0; i <= n; i++) { costo[i] = disto[i] = maxValue; marked[i] = false; } disto[start] = 0; costo[start] = 0; PriorityQueue<Node> pq = new PriorityQueue<Node>(); pq.add(new Node(start, 0)); marked[0] = true; Node temp; while (!pq.isEmpty()) { temp = pq.poll(); int v = temp.v; if (!marked[v]) { marked[v] = true; int len = G[v].size(); for (int i = 0; i < len; i++) { int target = G[v].get(i).target; if (marked[target]) continue; int cost = G[v].get(i).cost; int newDist = disto[v] + cost; if (disto[target] > newDist) { disto[target] = newDist; costo[target] = cost; pq.add(new Node(target, disto[target])); } if (disto[target] == newDist) { costo[target] = min(costo[target], cost); } } } } int sum = 0; for (int i = 2; i <= n; i++) { sum += costo[i]; } System.out.println(sum); } private static int min(int i, int j) { // TODO Auto-generated method stub if (i < j) return i; else return j; } public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 0; i < maxN; i++) { G[i] = new ArrayList(); } Scanner s = new Scanner(System.in); int numCity = s.nextInt(); int numRoad = s.nextInt(); for (int i = 0; i < numRoad; i++) { int source = s.nextInt(); int target = s.nextInt(); int w = s.nextInt(); G[source].add(new Edge(target, w)); G[target].add(new Edge(source, w)); } dijkstra(1, numCity); } public static class Node implements Comparable { public int v; int dis; public Node(int v, int dis) { this.v = v; this.dis = dis; } public int compareTo(Object o) { Node n = (Node) o; if (this.dis > n.dis) return 1; else if (this.dis == n.dis) return 0; else return -1; } } public static class Edge { public int target, cost; public Edge(int target, int cost) { this.target = target; this.cost = cost; } } }
ccf201604
0401折点计数(100)
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s=new Scanner(System.in); int n=s.nextInt(); int n1=s.nextInt(); //要注意边界值的处理!!! if(n==1) { System.out.println(0); return; } int n2=s.nextInt(); int num=0; for(int i=0;i<n-2;i++) { int n3=s.nextInt(); if((n2<n1&&n2<n3)||(n2>n1&&n2>n3)) { num++; } n1=n2; n2=n3; } System.out.println(num); } }
0404游戏(100分)(借鉴 http://blog.csdn.net/zjj582984208/article/details/55223889)
import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Scanner; public class Main_04 { public static class Node{ int i=0,j=0,t=0; Node(int i1,int j1,int t1){ i=i1; j=j1; t=t1; } } public static void main(String[] args) { // TODO Auto-generated method stub Scanner s = new Scanner(System.in); int n = s.nextInt(); int m = s.nextInt(); int t = s.nextInt(); int danger[][][]=new int[n][m][2]; for(int i=0;i<t;i++) { int r=s.nextInt(); int c=s.nextInt(); int a=s.nextInt(); int b=s.nextInt(); danger[r-1][c-1][0]=a; danger[r-1][c-1][1]=b; } int dir[][]= { {1,0}, {0,1}, {-1,0}, {0,-1} }; Queue<Node> q=new LinkedList<>(); int seen[][][]=new int[n][m][301]; q.add(new Node(0,0,0)); while(!q.isEmpty()) { Node node=q.poll(); if(node.i==n-1&&node.j==m-1) { System.out.println(node.t); break; } for(int i=0;i<4;i++) { int next_i=node.i+dir[i][0]; int next_j=node.j+dir[i][1]; if(next_i>=0&&next_i<n&&next_j>=0&&next_j<m&&node.t+1<300&& (node.t+1<danger[next_i][next_j][0]||node.t+1>danger[next_i][next_j][1]) &&seen[next_i][next_j][node.t+1]==0) { q.add(new Node(next_i,next_j,node.t+1)); seen[next_i][next_j][node.t+1]=1; } } } } }