2020蓝桥杯Java复习

    科技2022-07-16  207

    文章目录

    Java排序内部类排序(动态数组,排序自定义,foreach输出) 欧拉筛法优先队列日期问题 SImple.... 日期格式化 - parse二分 寻找第一个大于目标数的数计算日期 具体到星期几

    Java排序

    交叉排序

    import java.math.BigInteger; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); Integer a[]; Integer n = cin.nextInt(); a = new Integer[n + 5]; int l1 = cin.nextInt(); int r1 = cin.nextInt(); int l2 = cin.nextInt(); int r2 = cin.nextInt(); for(int i = 1;i <= n;i++) a[i] = cin.nextInt(); Arrays.sort(a, l1, r1 + 1); Arrays.sort(a, l2, r2 + 1, new Comparator<Integer>(){ @Override public int compare(Integer o1, Integer o2) { // TODO Auto-generated method return o2 - o1; } }); for(int i = 1;i <= n;i++) if(i == 1) System.out.print(a[i]); else System.out.print(" " + a[i]); System.out.println(); } }

    内部类排序(动态数组,排序自定义,foreach输出)

    成绩排序

    import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class Main { static class node{ int c; int m; int e; int s; int sum; String name; node(String name,int c, int m, int e, int s){ this.name = name; this.c = c; this.m = m; this.e = e; this.s = s; this.sum = c + m + e + s; } } public static void main(String[] args) { Scanner cin = new Scanner(System.in); ArrayList<node>ar = new ArrayList<node>(); int n = cin.nextInt(); for(int i = 1;i <= n;i++) { String name = cin.next(); int c = cin.nextInt(); int m = cin.nextInt(); int e = cin.nextInt(); int s = cin.nextInt(); node nd = new node(name, c, m, e, s); ar.add(nd); } ar.sort(new Comparator<node>() { @Override public int compare(node o1, node o2) { // TODO Auto-generated method stub if(o1.c == o2.c)return o1.name.compareTo(o2.name); else return o2.c - o1.c; } }); int cnt = 0; for(node T:ar) { cnt++; if(cnt == 1) System.out.print(T.name); else System.out.print(" " + T.name); if(cnt == 4)break; } System.out.println(); ar.sort(new Comparator<node>() { @Override public int compare(node o1, node o2) { // TODO Auto-generated method stub if(o1.m == o2.m)return o1.name.compareTo(o2.name); else return o2.m - o1.m; } }); cnt = 0; for(node T:ar) { cnt++; if(cnt == 1) System.out.print(T.name); else System.out.print(" " + T.name); if(cnt == 4)break; } System.out.println(); ar.sort(new Comparator<node>() { @Override public int compare(node o1, node o2) { // TODO Auto-generated method stub if(o1.e == o2.e)return o1.name.compareTo(o2.name); else return o2.e - o1.e; } }); cnt = 0; for(node T:ar) { cnt++; if(cnt == 1) System.out.print(T.name); else System.out.print(" " + T.name); if(cnt == 4)break; } System.out.println(); ar.sort(new Comparator<node>() { @Override public int compare(node o1, node o2) { // TODO Auto-generated method stub if(o1.s == o2.s)return o1.name.compareTo(o2.name); else return o2.s - o1.s; } }); cnt = 0; for(node T:ar) { cnt++; if(cnt == 1) System.out.print(T.name); else System.out.print(" " + T.name); if(cnt == 4)break; } System.out.println(); ar.sort(new Comparator<node>() { @Override public int compare(node o1, node o2) { // TODO Auto-generated method stub if(o1.sum == o2.sum)return o1.name.compareTo(o2.name); else return o2.sum - o1.sum; } }); cnt = 0; for(node T:ar) { cnt++; if(cnt == 1) System.out.print(T.name); else System.out.print(" " + T.name); if(cnt == 4)break; } System.out.println(); } }

    欧拉筛法

    链接 长度为11的等差素数列,其公差最小值是多少?

    package ch01; public class isPrime { static int []prime = new int[100005]; static boolean []vis = new boolean[100005]; static int cnt = 0, count = 0; static void sieve(int n) { cnt = 0; for(int i = 2; i <= n; i++) { if(!vis[i]) //不是目前找到的素数的倍数 是素数 prime[cnt++] = i; //找到素数 for(int j = 0; j < cnt && i * prime[j] <= n; j++) { vis[i * prime[j]] = true; //找到的素数的倍数不访问 if(i % prime[j] == 0) break; //关键!!!! } } } public static void main(String[] args) { sieve(100000); for(int i = 1;i <= 1000;i++) { for(int j = 0;j < cnt;j++) { int sum = prime[j];int k = 1; while(true) { count++; if(sum > 100000 || vis[sum] || k == 11) {//不是素数break break; } sum += i; k++; } if(k == 11) { System.out.println(i); System.out.println(count); System.exit(0); } } } } }

    优先队列

    标题:第几个幸运数 到x星球旅行的游客都被发给一个整数,作为游客编号。 x星的国王有个怪癖,他只喜欢数字3,5和7。 国王规定,游客的编号如果只含有因子:3,5,7,就可以获得一份奖品。 我们来看前10个幸运数字是: 3 5 7 9 15 21 25 27 35 45 因而第11个幸运数字是:49 小明领到了一个幸运数字 59084709587505,他去领奖的时候,人家要求他准确地说出这是第几个幸运数字,否则领不到奖品。 请你帮小明计算一下,59084709587505是第几个幸运数字。 需要提交的是一个整数,请不要填写任何多余内容。 import java.math.BigInteger; import java.util.HashMap; import java.util.LinkedList; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.TreeSet; public class Test2 { public static void main(String[] args) { PriorityQueue<Long> q = new PriorityQueue<Long>(); TreeSet<Long> st = new TreeSet<Long>(); q.add((long) 1); int cnt = 0; while (!q.isEmpty()) { long x = q.poll(); System.out.println(x); if(("" + x).equals("59084709587505"))break; if (!st.contains(x * 3)) { q.add(x * 3); st.add(x * 3); cnt++; } if (!st.contains(x * 5)) { q.add(x * 5); st.add(x * 5); cnt++; } if (!st.contains(x * 7)) { q.add(x * 7); st.add(x * 7); cnt++; } } cnt = 0; for(Long T:st) { cnt++; if((""+T).equals("59084709587505")) { System.out.println(cnt); break; } } } }

    日期问题 SImple… 日期格式化 - parse

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class Main { public static void main(String[] args) {// 年/月/日的,有采用月/日/年的,还有采用日/月/年的000 Scanner cin = new Scanner(System.in); String a[] = cin.next().split("/"); String t[] = new String[3]; t[0] = ((Integer.valueOf(a[0]) >= 60) ? "19" + a[0] : "20" + a[0]) + "-" + a[1] + "-" + a[2]; t[1] = ((Integer.valueOf(a[2]) >= 60) ? "19" + a[2] : "20" + a[2]) + "-" + a[0] + "-" + a[1]; t[2] = ((Integer.valueOf(a[2]) >= 60) ? "19" + a[2] : "20" + a[2]) + "-" + a[1] + "-" + a[0]; SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); sf.setLenient(false); Set<String> s = new TreeSet<String>(); for (String T : t) { try { sf.parse(T); } catch (ParseException e) { continue; } s.add(T); } for(String T:s) System.out.println(T); } }

    二分 寻找第一个大于目标数的数

    package ch01; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class Main { public static void main(String[] args) {// 年/月/日的,有采用月/日/年的,还有采用日/月/年的000 Scanner cin = new Scanner(System.in); int n = cin.nextInt(); long vis[] = new long[100005]; int a[][] = new int[3][n + 5]; for (int i = 0; i < n; i++) { a[0][i] = cin.nextInt(); } for (int i = 0; i < n; i++) { a[1][i] = cin.nextInt(); } for (int i = 0; i < n; i++) { a[2][i] = cin.nextInt(); } Arrays.sort(a[0], 0, n); Arrays.sort(a[1], 0, n); Arrays.sort(a[2], 0, n); long res = 0; int ff = 0; long max = 0; for (int i = 0; i < n; i++) { int l = 0, r = n; while (l <= r) { int mid = (l + r) >>> 1; if (a[0][i] < a[1][mid]) { r = mid - 1; } else l = mid + 1; } int pos = a[1][l]; // System.out.println(pos); if (l >= n) break; if (l != 0) { if (vis[a[1][l]] != 0) res += max - vis[a[1][l - 1]]; } else res += max; // System.out.println(res + " " + a[1][l] + " " + vis[a[1][l - 1]]); // System.out.println(pos); // System.out.println(vis[a[1][l - 1]]); int flag = l; if (ff == 0) { while (true) { l = 0; r = n; ff = 1; while (l <= r) { int mid = (l + r) >>> 1; if (a[1][flag] < a[2][mid]) { r = mid - 1; } else l = mid + 1; } // System.out.println(a[2][l] + " sd"); if (l >= n) break; res += (long) (n - l); if (flag == 0) vis[a[1][flag]] = n - l; else vis[a[1][flag]] = res; flag++; max = res; if (flag == n) break; } } } System.out.println(res); } }

    计算日期 具体到星期几

    package ch01; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class Main { public static void main(String[] args) {// 年/月/日的,有采用月/日/年的,还有采用日/月/年的000 Scanner cin = new Scanner(System.in); Calendar c = Calendar.getInstance(); c.set(2020, 10,9);//月份从0开始 System.out.println(c.get(Calendar.DAY_OF_WEEK) - 1); } }
    Processed: 0.011, SQL: 8