当前位置:   article > 正文

蓝桥杯考前复习一

蓝桥杯考前复习一

目录

被蓝桥杯整的焦虑死了,我要复习复习!!!

1.二维前缀和

2.BigIntenger常用方法

3.差分

4.DFS

5.01背包

6.并查集

7.BFS

 8.最短路模板

9.分解质因数

10.两个小结论:


1.二维前缀和

1.统计子矩阵 - 蓝桥云课 (lanqiao.cn)

  1. import java.util.Scanner;
  2. public class 统计子矩阵 {
  3. static int N=510;
  4. static int[][]g=new int[N][N];
  5. static int[][]s=new int[N][N];
  6. static int res=0;
  7. public static void main(String[] args) {
  8. Scanner sc = new Scanner(System.in);
  9. int n = sc.nextInt();
  10. int m = sc.nextInt();
  11. int K = sc.nextInt();
  12. for(int i=1;i<=n;i++){
  13. for(int j=1;j<=m;j++){
  14. g[i][j] = sc.nextInt();
  15. s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+g[i][j];
  16. }
  17. }
  18. //枚举子矩阵
  19. //矩阵左上角
  20. for(int i=1;i<=n;i++){
  21. for(int j=1;j<=m;j++){
  22. for(int k=i;k<=n;k++){
  23. for(int l=j;l<=m;l++){
  24. int sum=s[k][l]-s[k][j-1]-s[i-1][l]+s[i-1][j-1];
  25. if(sum<=K){
  26. res++;
  27. }
  28. }
  29. }
  30. }
  31. }
  32. System.out.println(res);
  33. }
  34. }

2.BigIntenger常用方法

快速幂取模:  

  1. BigInteger a=new BigInteger("2");
  2. BigInteger b=new BigInteger("6");
  3. BigInteger p=new BigInteger("10");
  4. BigInteger bigInteger = a.modPow( b,p);
  5. System.out.println(bigInteger);

最大公约数,最小公倍数数据较大的时候(高精度)

0最小公倍数 - 蓝桥云课 (lanqiao.cn)

  1. import java.math.BigInteger;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int n = sc.nextInt();
  7. BigInteger a=new BigInteger("1");
  8. BigInteger b=new BigInteger("2");
  9. BigInteger c=new BigInteger("1");
  10. for(int i=2;i<=n;i++) {
  11. a=lcm(a,b);
  12. b=b.add(c);
  13. }
  14. System.out.println(a);
  15. }
  16. public static BigInteger lcm(BigInteger a,BigInteger b) {
  17. return a.multiply(b).divide(a.gcd(b));
  18. }
  19. }

3.差分

4.DFS

4498. 指针 - AcWing题库

  1. import java.util.Scanner;
  2. public class Main {
  3. static int N=20;
  4. static int[]a=new int[N];
  5. static int n;
  6. static boolean flag=false;
  7. public static void main(String[] args) {
  8. Scanner sc=new Scanner(System.in);
  9. n=sc.nextInt();
  10. for(int i=1;i<=n;i++) {
  11. a[i]=sc.nextInt();
  12. }
  13. dfs(1,0);
  14. if(flag) {
  15. System.out.println("YES");
  16. }else {
  17. System.out.println("NO");
  18. }
  19. }
  20. public static void dfs(int u,int res) {
  21. if(u>n) {
  22. if(res==0||res%360==0) {
  23. flag=true;
  24. }
  25. return ;
  26. }
  27. dfs(u+1,res+a[u]);
  28. dfs(u+1,res-a[u]);
  29. }
  30. }

5.01背包

278. 数字组合 - AcWing题库

  1. import java.util.Scanner;
  2. public class Main {
  3. static int N=1000010;
  4. static int[]p=new int[N];
  5. public static void main(String[] args) {
  6. int ans=0;
  7. Scanner sc=new Scanner(System.in);
  8. int m=sc.nextInt();
  9. int n=sc.nextInt();
  10. for(int i=1;i<=m*n;i++) {
  11. p[i]=i;
  12. }
  13. int k=sc.nextInt();
  14. while(k--!=0) {
  15. int a=sc.nextInt();
  16. int b=sc.nextInt();
  17. p[find(a)]=find(b);
  18. }
  19. for(int i=1;i<=m*n;i++) {
  20. if(p[i]==i) {
  21. ans++;
  22. }
  23. }
  24. System.out.println(ans);
  25. }
  26. public static int find(int x) {
  27. if(p[x]!=x) p[x]=find(p[x]);
  28. return p[x];
  29. }
  30. }

6.并查集

1.合根植物 - 蓝桥云课 (lanqiao.cn)

  1. import java.util.Scanner;
  2. public class Main {
  3. static int N=1000010;
  4. static int[]p=new int[N];
  5. public static void main(String[] args) {
  6. int ans=0;
  7. Scanner sc=new Scanner(System.in);
  8. int m=sc.nextInt();
  9. int n=sc.nextInt();
  10. for(int i=1;i<=m*n;i++) {
  11. p[i]=i;
  12. }
  13. int k=sc.nextInt();
  14. while(k--!=0) {
  15. int a=sc.nextInt();
  16. int b=sc.nextInt();
  17. p[find(a)]=find(b);
  18. }
  19. for(int i=1;i<=m*n;i++) {
  20. if(p[i]==i) {
  21. ans++;
  22. }
  23. }
  24. System.out.println(ans);
  25. }
  26. public static int find(int x) {
  27. if(p[x]!=x) p[x]=find(p[x]);
  28. return p[x];
  29. }
  30. }

7.BFS

有一个整数 A=2021,每一次,可以将这个数加 1 、减 1 或除以 2,其中除以 2 必须在数是偶数的时候才允许。
例如,2021 经过一次操作可以变成 2020、2022。
再如,2022 经过一次操作可以变成 2021、2023 或 1011。
请问,2021 最少经过多少次操作可以变成 1。

  1. public class Main3 {
  2. public static void main(String[] args) {
  3. //数组开个10000完全够用了
  4. boolean[] visit=new boolean[10000];
  5. Queue<Integer> queue=new LinkedList<>();
  6. queue.offer(2021);
  7. visit[2021]=true;
  8. int time=0;
  9. while(!queue.isEmpty()) {
  10. int size=queue.size();
  11. while(size-->0) {
  12. int curr=queue.poll();
  13. //判断curr如果是1则直接输出time就是我们的答案
  14. if(curr==1) {
  15. System.out.println(time);//14
  16. return;
  17. }
  18. if(!visit[curr+1]) {
  19. visit[curr+1]=true;
  20. queue.offer(curr+1);
  21. }
  22. if(!visit[curr-1]) {
  23. visit[curr-1]=true;
  24. queue.offer(curr-1);
  25. }
  26. //一定要记住,只有偶数才可以选择除以2的操作
  27. if(curr%2==0&&!visit[curr/2]) {
  28. visit[curr/2]=true;
  29. queue.offer(curr/2);
  30. }
  31. }
  32. time++;
  33. }
  34. }
  35. }

 8.最短路模板

  1. import java.util.*;
  2. public class Main{
  3. static int N = 510,n,m, max = 0x3f3f3f3f;
  4. static int[][] g = new int[N][N];//存每个点之间的距离
  5. static int[] dist = new int[N];//存每个点到起点之间的距离
  6. static boolean[] st = new boolean[N];//存已经确定了最短距离的点
  7. public static int dijkstra(){
  8. Arrays.fill(dist,max);//将dist数组一开始赋值成较大的数
  9. dist[1] = 0; //首先第一个点是零
  10. //0开始,遍历n次,一次可以确定一个最小值
  11. for(int i = 0 ; i < n ; i ++ ){
  12. int t = -1; //t这个变量,准备来说就是转折用的
  13. for(int j = 1 ; j <= n ; j ++ ){
  14. /***
  15. * 因为数字是大于1的,所以从1开始遍历寻找每个数
  16. * 如果s集合中没有这个数
  17. * 并且t == -1,表示刚开始 或者 后面的数比我心找的数距离起点的距离短
  18. * 然后将j 的值赋值给 t
  19. ***/
  20. if(!st[j] && (t == -1 || dist[j] < dist[t])){
  21. t = j;
  22. }
  23. }
  24. st[t] = true;//表示这个数是已经找到了确定了最短距离的点
  25. //用已经确认的最短距离的点来更新后面的点
  26. //就是用1到t的距离加上t到j的距离来更新从1到j的长度
  27. for(int j = 1 ; j <= n ; j ++ ){
  28. //
  29. dist[j] = Math.min(dist[j],dist[t] + g[t][j]);
  30. }
  31. }
  32. //如果最后n的长度没有改变,输出-1,没有找到;否则输出最短路n
  33. if(dist[n] == max) return -1;
  34. else return dist[n];
  35. }
  36. public static void main(String[] args){
  37. Scanner scan = new Scanner(System.in);
  38. n = scan.nextInt();
  39. m = scan.nextInt();
  40. //将他们每个点一开始赋值成一个较大的值
  41. for(int i = 1 ; i <= n ; i ++ ){
  42. Arrays.fill(g[i],max);
  43. }
  44. while(m -- > 0){
  45. int a = scan.nextInt();
  46. int b = scan.nextInt();
  47. int c = scan.nextInt();
  48. g[a][b] = Math.min(g[a][b],c);//这个因为可能存在重边,所以泽出最短的
  49. }
  50. int res = dijkstra();
  51. System.out.println(res);
  52. }
  53. }

9.分解质因数

1.质因数个数 - 蓝桥云课 (lanqiao.cn)

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc=new Scanner(System.in);
  5. long n=sc.nextLong();
  6. long res=0;
  7. for(long i=2;i<=n/i;i++){
  8. if(n%i==0){
  9. while(n%i==0){
  10. n/=i;
  11. }
  12. res++;
  13. }
  14. }
  15. //39===3
  16. if(n>1) res++;
  17. System.out.println(res);
  18. }
  19. }

10.两个小结论:

    已知a,b为质数,则不能由a,b凑出来的最大的数是(a-1)(b-1)-1  ;

一段子数组在包含一段连续的自然数时,会有什么性质?设区间[ L , R ][L,R]的值域为[ m i n , m a x ] ,不难发现如果该区间是一段连续自然数时,将会满足:

max−min=j-i;

今天就复习到这里吧,回去吃我的塔斯丁去啦!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号