1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.HashSet;
  4. public class MST {
  5. public static void kruskal(int []V,Edge[] E) {
  6. Arrays.sort(E);
  7. ArrayList<HashSet> sets=new ArrayList<HashSet>();
  8. for(int i=0;i<V.length;i++) {
  9. HashSet set=new HashSet();
  10. set.add(V[i]);
  11. sets.add(set);
  12. }
  13. int sum_w=0;
  14. for(int i=0;i<E.length;i++) {
  15. int start=E[i].i;
  16. int end=E[i].j;
  17. int w=E[i].w;
  18. int set_start=-1;int set_end=-2;
  19. for(int j=0;j<sets.size();j++) {
  20. HashSet set=sets.get(j);
  21. if(set.contains(start))
  22. set_start=j;
  23. if(set.contains(end))
  24. set_end=j;
  25. }
  26. if(set_start!=set_end) {
  27. sum_w+=E[i].w;
  28. HashSet set=sets.get(set_end);
  29. sets.remove(set_end);
  30. HashSet set1=sets.get(set_start);
  31. sets.remove(set_start);
  32. set1.addAll(set);
  33. sets.add(set1);
  34. }
  35.  
  36. }
  37. System.out.println(sum_w);
  38.  
  39. }
  40.  
  41. public static void main(String[] args) {
  42. // TODO Auto-generated method stub
  43. int []V= {1,2,3,4,5,6};
  44. Edge[]E=new Edge[10];
  45. E[0]=new Edge(1,2,6);
  46. E[1]=new Edge(1,3,1);
  47. E[2]=new Edge(1,4,5);
  48. E[3]=new Edge(2,3,5);
  49. E[4]=new Edge(2,5,3);
  50. E[5]=new Edge(3,4,5);
  51. E[6]=new Edge(3,5,6);
  52. E[7]=new Edge(3,6,4);
  53. E[8]=new Edge(4,6,2);
  54. E[9]=new Edge(5,6,6);
  55. kruskal(V,E);
  56. }
  57.  
  58. public static class Edge implements Comparable{
  59. public int i,j,w;
  60. public Edge(int i,int j,int w) {
  61. this.i=i;
  62. this.j=j;
  63. this.w=w;
  64. }
  65. @Override
  66. public int compareTo(Object o) {
  67. Edge to=(Edge) o;
  68. if(this.w>to.w)return 1;
  69. else if(this.w==to.w)return 0;
  70. else return -1;
  71. }
  72. @Override
  73. public String toString() {
  74. return "start="+i+"||end="+j+"||weight="+w;
  75. }
  76.  
  77. }
  78. }
  1. public class ShortestPath {
  2. public static void shortestPath(int graph[][], int start, int n) {
  3. int[] d = new int[n];
  4. int pre[] = new int[n];
  5. boolean[] finish = new boolean[n];
  6. int noEdge = Integer.MAX_VALUE;
  7. int noPre = -1;
  8. int noLink = 0;
  9. // 初始化
  10. for (int i = 0; i < n; i++) {
  11. if (graph[start][i] == noLink) {
  12. d[i] = noEdge;
  13. pre[i] = noPre;
  14. } else {
  15. d[i] = graph[start][i];
  16. pre[i] = start;
  17. }
  18. finish[i] = false;
  19. }
  20.  
  21. finish[start] = true;
  22. pre[start] = noPre;
  23. d[start] = noEdge;
  24. for (int i = 0; i < n; i++) {
  25. int minj = -1, minValue = Integer.MAX_VALUE;
  26. // 找最小的d值
  27. for (int j = 0; j < n; j++) {
  28. if (!finish[j]) {
  29. if (d[j] < minValue) {
  30. minj = j;
  31. minValue = d[j];
  32. }
  33. }
  34. }
  35. if (minj != -1) {
  36. int count = minj;
  37. // 输出最短路径
  38. System.out.print(minj + 1);
  39. while (pre[count] != noPre) {
  40. System.out.print(" " + (pre[count] + 1));
  41. count = pre[count];
  42. }
  43. System.out.println();
  44. } else {
  45. // System.out.println("不再存在从v0可到达的最短路径");
  46. break;
  47. }
  48. // 更新d pre finish
  49. finish[minj] = true;
  50. for (int j = 0; j < n; j++) {
  51. if (!finish[j]) {
  52. if (graph[minj][j] != noLink && (d[minj] + graph[minj][j] < d[j])) {
  53. pre[j] = minj;
  54. d[j] = d[minj] + graph[minj][j];
  55. }
  56. }
  57. }
  58. }
  59.  
  60. }
  61. public static void main(String[] args) {
  62. // TODO Auto-generated method stub
  63. int[][] graph = { { 0, 2, 4, 3 }, { 2, 0, 2, 0 }, { 4, 2, 0, 1 }, { 3, 0, 1, 0 } };
  64.  
  65. shortestPath(graph, 0, 4);
  66. }
  67. }

ccf201709

0901打酱油(100分)

  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. Scanner scan=new Scanner(System.in);
  7.  
  8. int money=scan.nextInt();
  9. scan.close();
  10. int num_buy=0;
  11. if (money%50==0)
  12. {
  13. num_buy=money/50*7;
  14. }
  15. else {
  16. num_buy=money%50/30+money%50/10+money/50*7;
  17. }
  18. System.out.println(num_buy);
  19.  
  20. }
  21.  
  22. }

0902公共钥匙盒(100分)

  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class Main_02 {
  5.  
  6. public static void main(String[] args) {
  7. // TODO Auto-generated method stub
  8. Scanner scan = new Scanner(System.in);
  9. int n = scan.nextInt();
  10. int k = scan.nextInt();
  11.  
  12. int[] key = new int[n];
  13. for (int i = 0; i < n; i++) {
  14. key[i] = i + 1;
  15. }
  16.  
  17. int[][] tea_use = new int[k][4];
  18. for (int i = 0; i < k; i++) {
  19. tea_use[i][0] = scan.nextInt();
  20. tea_use[i][1] = scan.nextInt();
  21. tea_use[i][2] = scan.nextInt();
  22. tea_use[i][3] = tea_use[i][1] + tea_use[i][2];
  23. }
  24.  
  25. int time = 0;
  26. int max = 0;
  27. for (int i = 0; i < k; i++) {
  28. if (tea_use[i][3] > max) {
  29. max = tea_use[i][3];
  30. }
  31. }
  32.  
  33. int[] ret = new int[k];
  34. while (time <= max) {
  35. for (int i = 0; i < k; i++) {
  36. ret[i] = 0;
  37. }
  38. int m = 0;
  39. for (int i = 0; i < k; i++) {
  40. if (tea_use[i][3] == time) {
  41. ret[m++] = tea_use[i][0];
  42. }
  43. }
  44. Arrays.sort(ret);
  45. for (int i = 0; i < k; i++) {
  46. if (ret[i] != 0) {
  47. for (int j = 0; j < n; j++) {
  48. if (key[j] == 0) {
  49. key[j] = ret[i];
  50. ret[i] = 0;
  51. break;
  52. }
  53. }
  54. }
  55. }
  56.  
  57. for (int i = 0; i < k; i++) {
  58. if (tea_use[i][1] == time) {
  59. for (int j = 0; j < n; j++) {
  60. if (key[j] == tea_use[i][0]) {
  61. key[j] = 0;
  62. }
  63. }
  64. }
  65. }
  66. time++;
  67. }
  68.  
  69. for (int i = 0; i < n; i++) {
  70. System.out.print(key[i] + " ");
  71. }
  72.  
  73. }
  74.  
  75. }

0903Json查询()

借鉴 CCF 201709-3 Json查询 Java

  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5. public class Main_03 {
  6. static Map<String,String> Json=new HashMap<String,String>();
  7. static String keyVal="";
  8. static boolean key=false;
  9.  
  10. public static void handle(String line) {
  11. for(int i=0;i<line.length();i++) {
  12. char c=line.charAt(i);
  13. switch(c) {
  14. case '{':
  15. Json.put(keyVal, "OBJECT");
  16. key=true;
  17. break;
  18.  
  19. case '}':
  20. if(!keyVal.equals("")) {
  21. int j;
  22. for(j=keyVal.length()-1;j>=0;j--) {
  23. if(keyVal.charAt(j)=='.') {
  24. break;
  25. }
  26. }
  27. if(j<0)
  28. keyVal="";
  29. else
  30. keyVal=keyVal.substring(0,j);
  31. }
  32. break;
  33.  
  34. case '"':
  35. String temp="";
  36. for(i=i+1;i<line.length();i++) {
  37. if(line.charAt(i)=='\\') {
  38. i++;
  39. temp+=line.charAt(i);
  40. }
  41. else if(line.charAt(i)=='"')
  42. break;
  43. else
  44. temp+=line.charAt(i);
  45. }
  46. if(key) {
  47. if(!keyVal.equals(""))
  48. keyVal+=".";
  49. keyVal+=temp;
  50. }
  51. else {
  52. Json.put(new String(keyVal), "STRING "+temp);
  53. int j=keyVal.lastIndexOf(".");
  54. if(j<0)
  55. keyVal="";
  56. else
  57. keyVal=keyVal.substring(0,j);
  58. }
  59. break;
  60.  
  61. case ':':
  62. key=false;
  63. break;
  64.  
  65. case ',':
  66. key=true;
  67. break;
  68. }
  69. }
  70. }
  71. public static void main(String[] args) {
  72. // TODO Auto-generated method stub
  73. Scanner scan=new Scanner(System.in);
  74. int n=scan.nextInt();
  75. int m=scan.nextInt();
  76. scan.nextLine();
  77.  
  78. for(int i=0;i<n;i++) {
  79. handle(scan.nextLine().replace(" ", ""));
  80. }
  81.  
  82. for(int i=0;i<m;i++) {
  83. String query=scan.nextLine();
  84. if(!Json.containsKey(query))
  85. System.out.println("NOTEXIST");
  86. else
  87. System.out.println(Json.get(query));
  88. }
  89.  
  90. }
  91. }

0904通信网络(100分)

  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Queue;
  6. import java.util.Scanner;
  7.  
  8. public class Main_04 {
  9.  
  10. public static void main(String[] args) {
  11. // TODO Auto-generated method stub
  12. Scanner scan = new Scanner(System.in);
  13. int n = scan.nextInt();
  14. int m = scan.nextInt();
  15. List[] send = new ArrayList[n + 1];
  16. List[] receive = new ArrayList[n + 1];
  17. for (int i = 0; i < n + 1; i++) {
  18. send[i] = new ArrayList();
  19. receive[i] = new ArrayList();
  20. }
  21.  
  22. for (int j = 0; j < m; j++) {
  23. int s = scan.nextInt();
  24. int t = scan.nextInt();
  25. send[s].add(t);
  26. receive[t].add(s);
  27. }
  28.  
  29. boolean all[][] = new boolean[n + 1][n + 1];
  30. for (int i = 0; i < n + 1; i++) {
  31. all[i][i] = true;
  32. }
  33.  
  34. Queue q = new LinkedList();
  35. boolean[] flags;
  36. for (int i = 1; i < n + 1; i++) {
  37. q.add(i);
  38. flags = new boolean[n + 1];
  39. while (!q.isEmpty()) {
  40. int temp = (int) q.poll();
  41. if (!flags[temp]) {
  42. for (int j = 0; j < send[temp].size(); j++) {
  43. int next = (int) send[temp].get(j);
  44. q.add(next);
  45. }
  46. flags[temp] = true;
  47. all[i][temp] = true;
  48. }
  49. }
  50. }
  51.  
  52. for (int i = 1; i < n + 1; i++) {
  53. q.add(i);
  54. flags = new boolean[n + 1];
  55. while (!q.isEmpty()) {
  56. int temp = (int) q.poll();
  57. if (!flags[temp]) {
  58. for (int j = 0; j < receive[temp].size(); j++) {
  59. int next = (int) receive[temp].get(j);
  60. q.add(next);
  61. }
  62. flags[temp] = true;
  63. all[i][temp] = true;
  64. }
  65. }
  66. }
  67. int num = 0;
  68. for (int i = 1; i < n + 1; i++) {
  69. boolean knowAll = true;
  70. for (int j = 1; j < n + 1; j++) {
  71. if (!all[i][j]) {
  72. knowAll = false;
  73. break;
  74. }
  75. }
  76. if (knowAll) {
  77. num++;
  78. }
  79. }
  80.  
  81. System.out.println(num);
  82.  
  83. }
  84.  
  85. }

0905除法(30分)

  1. import java.util.Scanner;
  2.  
  3. public class Main_05 {
  4.  
  5. public static void main(String[] args) {
  6. // TODO Auto-generated method stub
  7. Scanner scan = new Scanner(System.in);
  8. int n = scan.nextInt();
  9. int m = scan.nextInt();
  10.  
  11. int input1[] = new int[n + 1];
  12. for (int i = 1; i < n + 1; i++) {
  13. input1[i] = scan.nextInt();
  14. }
  15. int result[] = new int[m];
  16. int k = 0;
  17. for (int i = 0; i < m; i++) {
  18. int opt = scan.nextInt();
  19. int l = scan.nextInt();
  20. int r = scan.nextInt();
  21. if (opt == 1) {
  22. int v = scan.nextInt();
  23. for (int j = l; j < r + 1; j++) {
  24. if (input1[j] % v == 0) {
  25. input1[j] /= v;
  26. }
  27. }
  28. }
  29. if (opt == 2) {
  30. int sum = 0;
  31. for (int j = l; j < r + 1; j++) {
  32. sum += input1[j];
  33. }
  34. System.out.println(sum);
  35. }
  36.  
  37. }
  38. }
  39.  
  40. }

ccf201703

0301分蛋糕(100分)

  1. package test_201703;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[] args) {
  8. // TODO Auto-generated method stub
  9. Scanner scan=new Scanner(System.in);
  10. int n=scan.nextInt();
  11. int k=scan.nextInt();
  12. int weights[]=new int[n];
  13. for(int i=0;i<n;i++) {
  14. weights[i]=scan.nextInt();
  15. }
  16. int people=0;
  17. int sum=0;
  18. for(int i=0;i<n;i++) {
  19. sum+=weights[i];
  20. if(sum>=k||i==n-1) {
  21. people++;
  22. sum=0;
  23. }
  24. }
  25. System.out.println(people);
  26. }
  27. }

0302学生排队(100分)

  1. package test_201703;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main_02 {
  6.  
  7. public static void main(String[] args) {
  8. // TODO Auto-generated method stub
  9.  
  10. Scanner scan = new Scanner(System.in);
  11. int n=scan.nextInt();
  12. int opt_num=scan.nextInt();
  13.  
  14. int student[]=new int[n+1];
  15. int pos[]=new int[n+1];
  16. for(int i=1;i<n+1;i++) {
  17. student[i]=i;
  18. pos[i]=i;
  19. }
  20.  
  21. for(int i=0;i<opt_num;i++) {
  22. int stu=scan.nextInt();
  23. int dis=scan.nextInt();
  24. //向后移
  25. if(dis>0) {
  26. int temp=student[pos[stu]];
  27. int j=0;
  28. for(j=pos[stu];j<pos[stu]+dis;j++) {
  29. student[j]=student[j+1];
  30. pos[student[j+1]]-=1;
  31. }
  32. pos[stu]+=dis;
  33. student[j]=temp;
  34. }
  35. //向前移
  36. else {
  37. int temp=student[pos[stu]];
  38. int j=0;
  39. for(j=pos[stu];j>pos[stu]+dis;j--) {
  40. student[j]=student[j-1];
  41. pos[student[j-1]]+=1;
  42. }
  43. pos[stu]+=dis;
  44. student[j]=temp;
  45.  
  46. }
  47. }
  48.  
  49. for(int i=1;i<n+1;i++) {
  50. System.out.print(student[i]+" ");
  51. }
  52. }
  53.  
  54. }

0304地铁修建(80分)运行超时。借鉴http://blog.csdn.net/eternity666/article/details/68974954

  1. import java.util.ArrayList;
  2. import java.util.PriorityQueue;
  3. import java.util.Scanner;
  4.  
  5. public class Main_04 {
  6. static int maxN = 100001;
  7. static int maxValue = Integer.MAX_VALUE;
  8. static int costo[] = new int[maxN];
  9. static ArrayList<Edge>[] G = new ArrayList[maxN];
  10. static boolean vis[] = new boolean[maxN];
  11.  
  12. public static void shortestPath(int start, int n) {
  13. PriorityQueue<Node> pq = new PriorityQueue<Node>();
  14. for (int i = 0; i <= n; i++) {
  15. costo[i] = maxValue;
  16. vis[i] = false;
  17. }
  18. vis[0] = true;
  19. costo[start] = 0;
  20. pq.add(new Node(start, 0));
  21. Node temp;
  22. while (!pq.isEmpty()) {
  23. temp = pq.poll();
  24. int v = temp.v;
  25. if (vis[v])
  26. continue;
  27. vis[v] = true;
  28. for (int i = 0; i < G[v].size(); i++) {
  29. int target = G[v].get(i).target;
  30. int cost = G[v].get(i).cost;
  31. int maxCost = Math.max(cost, costo[v]);
  32. if (!vis[target] && costo[target] > maxCost) {
  33. costo[target] = maxCost;
  34. pq.add(new Node(target, costo[target]));
  35. }
  36. }
  37. }
  38. System.out.println(costo[n]);
  39. }
  40.  
  41. public static void main(String[] args) {
  42. // TODO Auto-generated method stub
  43.  
  44. for (int i = 0; i < maxN; i++) {
  45. G[i] = new ArrayList<Edge>();
  46. }
  47. Scanner s = new Scanner(System.in);
  48. int n = s.nextInt();
  49. int m = s.nextInt();
  50.  
  51. for (int i = 0; i < m; i++) {
  52. int a = s.nextInt();
  53. int b = s.nextInt();
  54. int c = s.nextInt();
  55. G[a].add(new Edge(b, c));
  56. G[b].add(new Edge(a, c));
  57. }
  58.  
  59. shortestPath(1, n);
  60.  
  61. }
  62.  
  63. public static class Node implements Comparable {
  64. public int v, dis;
  65.  
  66. public Node(int v, int dis) {
  67. this.v = v;
  68. this.dis = dis;
  69. }
  70.  
  71. @Override
  72. public int compareTo(Object o) {
  73. // TODO Auto-generated method stub
  74. Node n = (Node) o;
  75. if (this.dis > n.dis)
  76. return 1;
  77. else if (this.dis == n.dis)
  78. return 0;
  79. else
  80. return -1;
  81. }
  82.  
  83. }
  84.  
  85. public static class Edge {
  86. public int target, cost;
  87.  
  88. public Edge(int target, int cost) {
  89. this.target = target;
  90. this.cost = cost;
  91. }
  92.  
  93. }
  94. }

也可借鉴https://www.cnblogs.com/freinds/p/6742618.html

ccf201612

1201中间数(100分)

  1. package ccf_201612;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class Main {
  9.  
  10. public static void main(String[] args) {
  11. // TODO Auto-generated method stub
  12. Scanner s=new Scanner(System.in);
  13. int n=s.nextInt();
  14. List l=new ArrayList();
  15. for(int i=0;i<n;i++) {
  16. l.add(s.nextInt());
  17. }
  18. Collections.sort(l);
  19.  
  20. if(n==1) {
  21. System.out.println(l.get(0));
  22. return;
  23. }
  24.  
  25. if(l.size()%2==0) {
  26. int mid=(int) l.size()/2-1;
  27. int mid_num=(int) l.get(mid);
  28. int num=0;
  29. for(int j=0;j<l.size();j++) {
  30. if((int)l.get(j)==mid_num)
  31. num++;
  32. }
  33. if(num%2==0) {
  34. System.out.println(l.get(mid));
  35. }
  36. else
  37. System.out.println(-1);
  38. }
  39.  
  40. if(l.size()%2==1) {
  41. int mid=(int) (l.size()+1)/2-1;
  42. int mid_num=(int) l.get(mid);
  43. int num=0;
  44. for(int j=0;j<l.size();j++) {
  45. if((int)l.get(j)==mid_num)
  46. num++;
  47. }
  48. if(num%2==1) {
  49. System.out.println(l.get(mid));
  50. }
  51. else
  52. System.out.println(-1);
  53. }
  54.  
  55. }
  56.  
  57. }

1202工资计算(100分)

  1. package ccf_201612;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main_02 {
  6.  
  7. public static int algoth(int all) {
  8. int after=0;
  9. if(all<=3500) {
  10. after=all;
  11. return after;
  12. }
  13. all-=3500;
  14. if(all<=1500) {
  15. after= (int) (all*0.97);
  16. }
  17. if(1500<all&&all<=4500) {
  18. after=(int) (all-(all-1500)*0.1-1500*0.03);
  19. }
  20. if(4500<all&&all<=9000) {
  21. after=(int) (all-(all-4500)*0.2-3000*0.1-1500*0.03);
  22. }
  23. if(9000<all&&all<=35000) {
  24. after=(int) (all-(all-9000)*0.25-4500*0.2-3000*0.1-1500*0.03);
  25. }
  26. if(35000<all&&all<=55000) {
  27. after=(int) (all-(all-35000)*0.3-26000*0.25-4500*0.2-3000*0.1-1500*0.03);
  28. }
  29. if(55000<all&&all<=80000) {
  30. after=(int) (all-(all-55000)*0.35-20000*0.3-26000*0.25-4500*0.2-3000*0.1-1500*0.03);
  31. }
  32. if(all>80000) {
  33. 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);
  34. }
  35. return after+3500;
  36.  
  37. }
  38.  
  39. public static void main(String[] args) {
  40. // TODO Auto-generated method stub
  41. Scanner s=new Scanner(System.in);
  42. int money=s.nextInt();
  43.  
  44. int after[]=new int[100001];
  45. for(int i=0;i<100001;i++) {
  46. after[i]=0;
  47. }
  48.  
  49. for(int i=1;i<=100000;i++) {
  50.  
  51. int aft=algoth(i);
  52.  
  53. if(i%100==0) {
  54. after[aft]=i;
  55. }
  56.  
  57. }
  58.  
  59. System.out.println(after[money]);
  60.  
  61. }
  62.  
  63. }

ccf201609

0901最大波动(100分)

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9. // TODO Auto-generated method stub
  10. Scanner s=new Scanner(System.in);
  11. int n=s.nextInt();
  12.  
  13. List l=new ArrayList();
  14. int input[]=new int[n];
  15. int first=s.nextInt();
  16. for(int i=0;i<n-1;i++) {
  17. int second=s.nextInt();
  18. int abs=Math.abs(first-second);
  19. l.add(abs);
  20. first=second;
  21. }
  22. Collections.sort(l);
  23. int max=(int) l.get(l.size()-1);
  24. System.out.println(max);
  25. }
  26.  
  27. }

0902火车购票(90分)

90分

  1. import java.util.Scanner;
  2.  
  3. public class Main_02 {
  4. public static void main(String[] args) {
  5. // TODO Auto-generated method stub
  6. Scanner s = new Scanner(System.in);
  7. int n = s.nextInt();
  8.  
  9. int seat[][] = new int[20][5];
  10. int remain[] = new int[20];
  11. for (int i = 0; i < 20; i++) {
  12. seat[i][0] = i * 5 + 1;
  13. seat[i][1] = i * 5 + 2;
  14. seat[i][2] = i * 5 + 3;
  15. seat[i][3] = i * 5 + 4;
  16. seat[i][4] = i * 5 + 5;
  17. }
  18. for (int i = 0; i < 20; i++) {
  19. remain[i] = 5;
  20. }
  21.  
  22. int ticket[] = new int[n];
  23. for (int i = 0; i < n; i++) {
  24. ticket[i] = s.nextInt();
  25. }
  26.  
  27. for (int i = 0; i < n; i++) {
  28. int num = ticket[i];
  29. boolean flag = false;
  30.  
  31. for (int j = 0; j < 20; j++) {
  32. if (remain[j] >= num) {
  33. for (int k = 0; k < num; k++) {
  34. System.out.print(seat[j][5 - remain[j]] + " ");
  35. remain[j] -= 1;
  36. }
  37. System.out.println();
  38. flag = true;
  39. break;
  40. }
  41. }
  42.  
  43. if (!flag) {
  44. for (int j = 0; j < 20; j++) {
  45. for (int k = 0; k < remain[j]; k++) {
  46. System.out.print(seat[j][5 - remain[j]] + " ");
  47. remain[j] -= 1;
  48. num--;
  49. }
  50. if (num == 0) {
  51. System.out.println();
  52. break;
  53. }
  54. }
  55.  
  56. }
  57.  
  58. }
  59.  
  60. }
  61.  
  62. }

测试用例如下时出错:

  1. 输入:
  2.  
  3. 21
  4. 4 4 3 5 5
  5. 5 5 5 5 5
  6. 5 5 5 5 5
  7. 5 5 5 5 5
  8. 4
  9.  
  10. 输出:
  11.  
  12. 1 2 3 4
  13. 6 7 8 9
  14. 11 12 13
  15. 16 17 18 19 20
  16. 21 22 23 24 25
  17. 26 27 28 29 30
  18. 31 32 33 34 35
  19. 36 37 38 39 40
  20. 41 42 43 44 45
  21. 46 47 48 49 50
  22. 51 52 53 54 55
  23. 56 57 58 59 60
  24. 61 62 63 64 65
  25. 66 67 68 69 70
  26. 71 72 73 74 75
  27. 76 77 78 79 80
  28. 81 82 83 84 85
  29. 86 87 88 89 90
  30. 91 92 93 94 95
  31. 96 97 98 99 100
  32. 5 10 14
  33.  
  34. 最后一行应为5 10 14 15

出错代码:

  1. for (int k = 0; k < remain[j]; k++) {
  2. System.out.print(seat[j][5 - remain[j]] + " ");
  3. //此处改变了remain[j]
  4. remain[j] -= 1;
  5. num--;
  6. if (num == 0) {
  7. System.out.println();
  8. flag=true;
  9. break;
  10. }
  11. }

java满分代码:

  1. import java.util.Scanner;
  2.  
  3. public class Main_02 {
  4. public static void main(String[] args) {
  5. // TODO Auto-generated method stub
  6. Scanner s = new Scanner(System.in);
  7. int n = s.nextInt();
  8.  
  9. int seat[][] = new int[20][5];
  10. int remain[] = new int[20];
  11. for (int i = 0; i < 20; i++) {
  12. seat[i][0] = i * 5 + 1;
  13. seat[i][1] = i * 5 + 2;
  14. seat[i][2] = i * 5 + 3;
  15. seat[i][3] = i * 5 + 4;
  16. seat[i][4] = i * 5 + 5;
  17. }
  18. for (int i = 0; i < 20; i++) {
  19. remain[i] = 5;
  20. }
  21.  
  22. int ticket[] = new int[n];
  23. for (int i = 0; i < n; i++) {
  24. ticket[i] = s.nextInt();
  25. }
  26.  
  27. for (int i = 0; i < n; i++) {
  28. int num = ticket[i];
  29. boolean flag = false;
  30.  
  31. for (int j = 0; j < 20; j++) {
  32. if (remain[j] >= num) {
  33. for (int k = 0; k < num; k++) {
  34. System.out.print(seat[j][5 - remain[j]] + " ");
  35. remain[j] -= 1;
  36. }
  37. System.out.println();
  38. flag = true;
  39. break;
  40. }
  41. }
  42.  
  43. if (!flag) {
  44. for (int j = 0; j < 20; j++) {
  45. int r=remain[j];
  46. for (int k = 0; k < r; k++) {
  47. System.out.print(seat[j][5 - remain[j]] + " ");
  48. remain[j] -= 1;
  49. num--;
  50. if (num == 0) {
  51. System.out.println();
  52. flag=true;
  53. break;
  54. }
  55. }
  56. if(flag)
  57. break;
  58.  
  59. }
  60.  
  61. }
  62.  
  63. }
  64.  
  65. }
  66.  
  67. }

0904:交通规划 (100分)  借鉴 ccf交通规划 利用dijkstra和优先队列 c++

(借鉴  java

Dijkstra求解单源点最短路径

http://blog.csdn.net/moilk_nepho/article/details/52950546

  1. import java.util.ArrayList;
  2. import java.util.PriorityQueue;
  3. import java.util.Scanner;
  4.  
  5. public class Main_4 {
  6. static int maxN = 10001;
  7. static int maxValue = Integer.MAX_VALUE;
  8. static ArrayList<Edge> G[] = new ArrayList[maxN];
  9. static boolean marked[] = new boolean[maxN];
  10. static int disto[] = new int[maxN];
  11. static int costo[] = new int[maxN];
  12.  
  13. public static void dijkstra(int start, int n) {
  14.  
  15. for (int i = 0; i <= n; i++) {
  16. costo[i] = disto[i] = maxValue;
  17. marked[i] = false;
  18. }
  19.  
  20. disto[start] = 0;
  21. costo[start] = 0;
  22. PriorityQueue<Node> pq = new PriorityQueue<Node>();
  23. pq.add(new Node(start, 0));
  24. marked[0] = true;
  25.  
  26. Node temp;
  27. while (!pq.isEmpty()) {
  28. temp = pq.poll();
  29. int v = temp.v;
  30. if (!marked[v]) {
  31. marked[v] = true;
  32. int len = G[v].size();
  33. for (int i = 0; i < len; i++) {
  34. int target = G[v].get(i).target;
  35. if (marked[target])
  36. continue;
  37. int cost = G[v].get(i).cost;
  38. int newDist = disto[v] + cost;
  39. if (disto[target] > newDist) {
  40. disto[target] = newDist;
  41. costo[target] = cost;
  42. pq.add(new Node(target, disto[target]));
  43. }
  44. if (disto[target] == newDist) {
  45. costo[target] = min(costo[target], cost);
  46. }
  47.  
  48. }
  49. }
  50. }
  51.  
  52. int sum = 0;
  53. for (int i = 2; i <= n; i++) {
  54. sum += costo[i];
  55. }
  56. System.out.println(sum);
  57.  
  58. }
  59.  
  60. private static int min(int i, int j) {
  61. // TODO Auto-generated method stub
  62. if (i < j)
  63. return i;
  64. else
  65. return j;
  66. }
  67.  
  68. public static void main(String[] args) {
  69. // TODO Auto-generated method stub
  70. for (int i = 0; i < maxN; i++) {
  71. G[i] = new ArrayList();
  72. }
  73.  
  74. Scanner s = new Scanner(System.in);
  75. int numCity = s.nextInt();
  76. int numRoad = s.nextInt();
  77. for (int i = 0; i < numRoad; i++) {
  78. int source = s.nextInt();
  79. int target = s.nextInt();
  80. int w = s.nextInt();
  81. G[source].add(new Edge(target, w));
  82. G[target].add(new Edge(source, w));
  83. }
  84. dijkstra(1, numCity);
  85. }
  86.  
  87. public static class Node implements Comparable {
  88. public int v;
  89. int dis;
  90.  
  91. public Node(int v, int dis) {
  92. this.v = v;
  93. this.dis = dis;
  94. }
  95.  
  96. public int compareTo(Object o) {
  97. Node n = (Node) o;
  98. if (this.dis > n.dis)
  99. return 1;
  100. else if (this.dis == n.dis)
  101. return 0;
  102. else
  103. return -1;
  104. }
  105.  
  106. }
  107.  
  108. public static class Edge {
  109. public int target, cost;
  110.  
  111. public Edge(int target, int cost) {
  112. this.target = target;
  113. this.cost = cost;
  114. }
  115. }
  116. }

ccf201604

0401折点计数(100)

  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. // TODO Auto-generated method stub
  7. Scanner s=new Scanner(System.in);
  8. int n=s.nextInt();
  9. int n1=s.nextInt();
  10. //要注意边界值的处理!!!
  11. if(n==1) {
  12. System.out.println(0);
  13. return;
  14. }
  15. int n2=s.nextInt();
  16. int num=0;
  17. for(int i=0;i<n-2;i++) {
  18. int n3=s.nextInt();
  19. if((n2<n1&&n2<n3)||(n2>n1&&n2>n3)) {
  20. num++;
  21. }
  22. n1=n2;
  23. n2=n3;
  24. }
  25. System.out.println(num);
  26.  
  27. }
  28.  
  29. }

0404游戏(100分)(借鉴 http://blog.csdn.net/zjj582984208/article/details/55223889)

c++实现:

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Queue;
  6. import java.util.Scanner;
  7.  
  8. public class Main_04 {
  9.  
  10. public static class Node{
  11. int i=0,j=0,t=0;
  12. Node(int i1,int j1,int t1){
  13. i=i1;
  14. j=j1;
  15. t=t1;
  16.  
  17. }
  18. }
  19.  
  20. public static void main(String[] args) {
  21. // TODO Auto-generated method stub
  22. Scanner s = new Scanner(System.in);
  23. int n = s.nextInt();
  24. int m = s.nextInt();
  25. int t = s.nextInt();
  26.  
  27. int danger[][][]=new int[n][m][2];
  28. for(int i=0;i<t;i++) {
  29. int r=s.nextInt();
  30. int c=s.nextInt();
  31. int a=s.nextInt();
  32. int b=s.nextInt();
  33. danger[r-1][c-1][0]=a;
  34. danger[r-1][c-1][1]=b;
  35. }
  36. int dir[][]= {
  37. {1,0},
  38. {0,1},
  39. {-1,0},
  40. {0,-1}
  41. };
  42.  
  43. Queue<Node> q=new LinkedList<>();
  44. int seen[][][]=new int[n][m][301];
  45. q.add(new Node(0,0,0));
  46. while(!q.isEmpty()) {
  47. Node node=q.poll();
  48. if(node.i==n-1&&node.j==m-1) {
  49. System.out.println(node.t);
  50. break;
  51. }
  52. for(int i=0;i<4;i++) {
  53. int next_i=node.i+dir[i][0];
  54. int next_j=node.j+dir[i][1];
  55. if(next_i>=0&&next_i<n&&next_j>=0&&next_j<m&&node.t+1<300&&
  56. (node.t+1<danger[next_i][next_j][0]||node.t+1>danger[next_i][next_j][1])
  57. &&seen[next_i][next_j][node.t+1]==0) {
  58. q.add(new Node(next_i,next_j,node.t+1));
  59. seen[next_i][next_j][node.t+1]=1;
  60.  
  61. }
  62. }
  63. }
  64.  
  65. }
  66. }

陌上花开的博客ccf

冷暖知不知ccf

WitsMakeMen的专栏算法学习

ccf的更多相关文章

  1. CCF考试

    第八次CCF考试记录 代码还不知道对不对,过两天出成绩. 成绩出来了,310分. 100+100+100+10+0: 考试13:27开始,17:30结束,提交第4题后不再答题,只是检查前四题的代码 第 ...

  2. CCF关于NOIP2014复赛报名的通知

    CCF关于NOIP2014复赛报名的通知   CCF NOIP2014复赛全部实行网上注册.报名.未通过网上报名的选手将不具备参赛和申诉资格. 系统注册须知: NOIP2014复赛注册时间:2014年 ...

  3. [CCF] Z字形扫描

    CCF Z字形扫描 感觉和LeetCode中的ZigZag还是有一些不一样的. 题目描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z ...

  4. [CCF] ISBN号码检测

    CCF ISBN号码检测 题目概述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如"x-xxx-xxxxx-x",其 ...

  5. CCF NOI系列活动

    NOI-全国青少年信息学奥林匹克竞赛全国青少年信息学奥林匹克竞赛(NOI)是国内信息学领域内面向中学生的最高水平的大赛,每省派经选拔产生的选手(其中一名是女选手)参加,NOI每年在不同的省市举行. N ...

  6. CCF考前注意几点

    1.数组最好保持初始化习惯,且检查数组初始化在循环内还是循环外,若在循环内需要对数组进行处理,则数组初始化必须放在for循环内. 2.for循环保持好习惯,用括号括起来,以免粗心. 3.if条件判断要 ...

  7. CCF真题之最优灌溉

    201412-4 问题描述 雷雷承包了很多片麦田,为了灌溉这些麦田,雷雷在第一个麦田挖了一口很深的水井,所有的麦田都从这口井来引水灌溉. 为了灌溉,雷雷需要建立一些水渠,以连接水井和麦田,雷雷也可以利 ...

  8. 《计算机问题求解》总结——2014年CCF计算机课程改革导教班(2014.07.11)

    一:引言     "心想事成".这是自己获得导教班学习机会的最佳概括.2013年年末学习李晓明老师的<人群与网络>课程:随后网络认识烟台大学贺利坚老师,了解到2013年 ...

  9. 2016年CCF第七次测试 俄罗斯方块

    //2016年CCF第七次测试 俄罗斯方块 // 这道小模拟题还是不错 // 思路:处理出输入矩阵中含1格子的行数和列数 // 再判是否有一个格子碰到底部,否则整体再往下移动一步,如果有一个格子不能移 ...

  10. linux 通用时钟框架CCF

    linux CCF 时钟框架 简单介绍 这里讲的时钟是给soc各组件提供时钟的树状框架,并非内核使用的时间,和其它模块一样,clk也有框架,用以适配不同的平台.适配层之上是客户代码和接口,也就是各模块 ...

随机推荐

  1. 「Luogu P3931」SAC E#1 - 一道难题 Tree 解题报告

    圆原题面 我环顾四周,发现大佬们的写法都好高端! 比较差劲的我,只能交上一份DFS的题解 思路: DFS(当然了,其他算法也行) 要想切断叶子节点到根节点的连接 就是在叶子节点和根节点之间砍掉一条边 ...

  2. C语言之while循环

    while循环能做什么??? 先来个概念格式,while循环的一般形式为: while(表达式){ 语句块 } 意思是,先计算"表达式"的值,当值为真(非0)时, 执行" ...

  3. 12款好用的Visual Studio插件,最后一款良心推荐

    目录 01 CodeMaid 02 Markdown Editor 03 ReSharper 04 GitHub Extension for Visual Studio 05 ZenCoding 06 ...

  4. 14.python案例:爬取电影天堂中所有电视剧信息

    1.python案例:爬取电影天堂中所有电视剧信息 #!/usr/bin/env python3 # -*- coding: UTF-8 -*- '''======================== ...

  5. .Net PE

    // ConsoleApplication26.cpp: 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h& ...

  6. 基于Spring封装的Javamail实现邮件发送

    1.依赖 <dependency> <groupId>org.springframework</groupId> <artifactId>spring- ...

  7. 下载并部署 ArcGIS API for JavaScript 4.10

    学习ArcGIS API for JavaScript 4.10 的第一步就是下载并部署该文件. 有的读者由于之间没接触过,不知道怎么下载和部署文件.这些读者要求作者详细的写一篇关于下载和部署的文章( ...

  8. 序言vue.js介绍

    vue.js :渐进式JavaScript框架 vue.js 优点 1.体积小 例如:压缩后 33k; 2.更高的运行效率 基于虚拟dom,一种可以预先通过JavaScript进行各种计算,把最终的D ...

  9. 在Winform界面使用自定义用户控件及TabelPanel和StackPanel布局控件

    在很多时候,我们做一些非常规化的界面的时候,往往需要创建一些用户控件,在其中绘制好一些基础的界面块,作为后续重复使用的一个单元,用户控件同时也可以封装处理一些简单的逻辑.在开发Winform各种类型项 ...

  10. LeetCode刷题预备知识(二)

    Python四大数据结构的属性及方法 在LeetCode刷题预备知识一中我们掌握了常见的内置函数,和四大数据结构的基本概念: 但只掌握这些还远远不够,我们还需了解四大数据结构的属性及方法才能更高效快速 ...