• 蓝桥杯(3.12)


    1064: 【模板】埃氏筛法

    import java.util.Scanner;
    
    public class Main{
    	static final int N = 2000010;
    	static boolean[] vis = new boolean[N];
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		vis[0] = true;
    		vis[1] = true;
    		for(int i=2;i<=n;i++) {
    			if(!vis[i]) {
    				for(int j=2*i;j<=n;j+=i)
    					vis[j] = true;
    			}
    		}
    		for(int i=2;i<=n;i++) {
    			if(!vis[i])
    				System.out.print(i+" ");
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    1051: 【模板】gcd和lcm

    import java.util.Scanner;
    
    public class Test03{
    	public static long gcd(long a,long b) {
    		return b == 0 ? a : gcd(b,a%b);
    	}
    	public static long lcm(long a,long b) {
    		return (a*b)/gcd(a,b);
    	}
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		while(n-- != 0) {
    			long a = sc.nextLong();
    			long b = sc.nextLong();
    			System.out.println(gcd(a,b)+" "+lcm(a,b));
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    1047: 【模板】快速幂

    import java.util.Scanner;
    
    public class Main {
        static long qmi(long a, long b, long c) {
            long res = 1;
            while (b != 0) {
                if ((b & 1) == 1) {
                    res = res * a % c;
                }
                a = a * a % c;
                b >>= 1;
            }
            return res;
        }
    
        static void solve() {
            Scanner scanner = new Scanner(System.in);
            int t = scanner.nextInt();
            while (t-- != 0) {
                int a = scanner.nextInt();
                int b = scanner.nextInt();
                int c = scanner.nextInt();
                System.out.println(qmi(a, b, c));
            }
        }
    
        public static void main(String[] args) {
            solve();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    a的b次幂mod c

    1055. 股票买卖 II

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

    104. 货仓选址

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int[] res = new int[n];
    		for(int i=0;i<n;i++) {
    			res[i] = sc.nextInt();
    		}
    		Arrays.sort(res);//1 2 6 9
    		int mid = res[n/2];
    		int sum = 0;
    		for(int i=0;i<n;i++) {
    			sum+=Math.abs(mid-res[i]);
    		}
    		System.out.println(sum);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    矩阵相乘

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int m = sc.nextInt();
    		int k = sc.nextInt();
    		int[][] a = new int[n][m];
    		int[][] b = new int[m][k];
    		int[][] c = new int[n][k];
    		for(int i=0;i<n;i++) {
    			for(int j=0;j<m;j++) {
    				a[i][j] = sc.nextInt();
    			}
    		}
    		for(int i=0;i<m;i++) {
    			for(int j=0;j<k;j++) {
    				b[i][j] = sc.nextInt();
    			}
    		}
    		
    		for(int i=0;i<n;i++) {
    			for(int j=0;j<k;j++) {
    				for(int z=0;z<m;z++) {
    					c[i][j] += a[i][z]*b[z][j];
    				}
    				System.out.print(c[i][j]+" ");
    			}
    			System.out.println();
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    最大公约数

    import java.util.Scanner;
    
    public class Main {
    	public static int gcd(int a,int b) {
    		return b == 0 ? a : gcd(b,a%b);
    	}
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		while(n-- != 0) {
    			int a = sc.nextInt();
    			int b = sc.nextInt();
    			System.out.println(gcd(a,b));
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    疑似素数

    import java.util.Scanner;
    
    public class Main {
    	public static boolean isTrue(int i) {
    		int num = 0;
    		int n = i;//保存i的初始值
    		while(n!=0) {
    			int t = n%10;
    			num+=t;
    			n/=10;
    		}
    		//如果每一位求和得到的数是质数就返回true
    		if(num < 2)
    			return false;
    		for(int j=2;j<=num/j;j++) {
    			if(num % j == 0)
    				return false;
    		}
    		return true;
    	}
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();//输入n
    		int sum = 0;//记录满足条件的数目
    		for(int i=2;i<=n;i++) {//2 3 5 7 
    			if(isTrue(i)) {
    				sum++;
    			}
    		}
    		System.out.println(sum);//输出结果
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    小明的素数对

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Main {
    	static final int N = 100010;
    	static boolean[] vis = new boolean[N];
    	static BufferedReader br = new  BufferedReader(new InputStreamReader(System.in));
    	static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
    	
    	public static void main(String[] args) throws IOException{
    		int n = Integer.parseInt(br.readLine());
    		vis[0] = true;
    		vis[1] = true;
    		for(int i=2;i<=n;i++) {
    			if(!vis[i]) {
    				for(int j=2*i;j<=n;j+=i) {
    					vis[j] = true;
    				}
    			}
    		}
    		
    		List<Integer> list = new ArrayList<>();
    		for(int i=2;i<=n;i++) {
    			if(!vis[i]) {
    				list.add(i);
    			}
    		}
    
    		int sum = 0;
    		for(int i=0;i<list.size();i++) {
    			for(int j=i+1;j<list.size();j++) {
    				int k = list.get(j) - list.get(i);
    				if((!vis[k])) {
    					sum++;
    				}
    			}
    		}
    		pw.println(sum);
    		pw.close();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 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
  • 相关阅读:
    天锐绿盾数据防泄密系统
    pycharm opencv无法自动补全
    nginx+redis+jvm三级缓存设计与落地实现
    AD9371 官方例程HDL详解之JESD204B TX侧时钟生成 (三)
    苹果“自揭老底”:自家应用不如第三方的好
    你知道吗?chrome自动更新到104版本,居然引起Java服务内存泄漏
    Ubuntu配置NFS服务器(Linux挂载Linux)
    数据分析常见的业务面试题
    kubectl 速查手册
    nodejs+vue+elementui前台美食网上订餐点菜系统 vscode项目
  • 原文地址:https://blog.csdn.net/qq_62552630/article/details/136641405