ccf
Posted highly
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ccf相关的知识,希望对你有一定的参考价值。
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 >CCF CSP 202109赛题练习