蓝桥杯13届JAVA A组 国赛

​​​​​​​

java 复制代码
package 蓝桥杯国赛;
// 贪心选个数最少的进行摆
// 2:1 ,3:1, 4:1,5 : 3,6:3,7:1
// 选 1,7,4,2,3,5,9
// 然后都选满10个
public class 火彩棒数字 {
	public static void main(String[] args)
	{
		System.out.print("9999999999777777777755555555554444444444333333333322222222221111111111");
	}
}
java 复制代码
package 蓝桥杯国赛;
import java.util.Scanner;
//1:无需package
//2: 类名必须Main, 不可修改
//C28 * f[i] 错排公式

//f[i] = (i - 1) (f[i - 1] + f[i - 2])

public class 小蓝与钥匙 {
	static int N = 100;
	static long[] f = new long[N];
	public static void main(String[] args) {
	     long ans = 1;
	     f[2] = 1; 
	     for(int i = 3;i < N;i++)
	       f[i] = (i - 1) * (f[i - 1] + f[i - 2]);
	     for(long i = 0;i < 14;i++)
	       ans = ans * (28 - i) / (i + 1);
	     System.out.println(ans * f[14]);
	 }
}
java 复制代码
package 蓝桥杯国赛;
import java.util.Scanner;

public class 内存空间 {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int t=in.nextInt();
        long sum=0;
        in.nextLine();
        while(t-->0){
            String []s=in.nextLine().split(" ");
            if(s[0].equals("int")){
                String[]s1=s[1].split(",");
                sum+=4*s1.length;
            }else if(s[0].equals("long")){
                String[]s1=s[1].split(",");
                sum+=8*s1.length;
            }else if(s[0].equals("String")){
                String[]s1=s[1].split(",");
                for(String ss:s1){
                    int index=ss.indexOf("\"");
                    int tmp=ss.indexOf("\"",index+1)-index-1;
                    sum+=tmp;
                }
            }else if(s[0].equals("int[]")) {
                for(int i=2;i<s.length;i++){
                    int l=s[i].indexOf("[");
                    int r=s[i].indexOf("]");
                    int tmp=Integer.parseInt(s[i].substring(l+1,r));
                    sum+=4*tmp;
                }
            }else if(s[0].equals("long[]")) {
                for(int i=2;i<s.length;i++){
                    int l=s[i].indexOf("[");
                    int r=s[i].indexOf("]");
                    int tmp=Integer.parseInt(s[i].substring(l+1,r));
                    sum+=8*tmp;
                }
            }
        }
        
        long gb,mb,kb,k;
        gb=sum/1024/1024/1024;
        mb=sum%(1024*1024*1024)/1024/1024;
        kb=sum%(1024*1024)/1024;
        k=sum%1024;
        String res="";
        if (gb != 0)
            res += gb + "GB";
        if (mb != 0)
            res += mb + "MB";
        if (kb != 0)
            res += kb + "KB";
        if (k != 0)
            res += k + "B";
        System.out.println(res);

    }

}
java 复制代码
package 蓝桥杯国赛;

import java.util.Scanner;
// 由之前算出当n > 30 f[] > 1e6

public class 斐波那契数组 {
	static int N = 100100;
	static int[] f = new int[N],a = new int[N];
	public static void main(String[] args)
	{
		f[1] = 1;f[2] = 1;
		for(int i = 3;i <= 30;i++)
			f[i] = f[i - 1] + f[i - 2];
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		for(int i = 1;i <= n;i++)
			a[i] = scanner.nextInt();
		int ans = 0,m = n;
		if(n > 30) n = 30;
		for(int i = 1;i <= n;i++) {
			int cnt = 0;
			if(a[i] % f[i] == 0) {
				int t = a[i] / f[i];
				for(int j = 1;j <= n;j++)
					if(f[j] * t == a[j]) cnt++;
				ans = Math.max(ans,cnt);
			}
			
		}
		System.out.print(m - ans);
	}
}
java 复制代码
package 蓝桥杯国赛;
import java.util.*;
// 特殊的dijkstra
// dijkstra是每个选取最小的,如果弹出的节点已径扩展过了,就continue
// 然后扩展每个点计算经过该点的最短路径,如果小于就原最短路径,则入堆
// 这里特殊的边权值是动态变化的
// tt < g 则当前路口为绿灯
// g < tt < g + d 则当前路口为黄灯且下一个灯为红灯
// g + d < tt < g + d + r 则当前路口为红灯
// g + d + r < tt < g + 2d + r 则当前路口为黄灯且下一个灯为绿灯 
public class 交通信号 {
	static final int N = 1000010, M = 4 * N;
    static int[] h = new int[N], e = new int[M], ne = new int[M], g = new int[M], r = new int[M], d = new int[M], f = new int[M];
    static long[] times = new long[N];

    static int idx = 0;

    static void add(int a, int b, int green, int red, int dist, int flag) {
        e[idx] = b; g[idx] = green; r[idx] = red;
        d[idx] = dist; ne[idx] = h[a]; f[idx] = flag; h[a] = idx++;
    }

    static long dijkstra(int n, int m, int s, int t) {
        Arrays.fill(times, Long.MAX_VALUE);
        times[s] = 0;

        PriorityQueue<Pair> pq = new PriorityQueue<>((a,b) -> Long.compare(a.dist,b.dist));
        pq.offer(new Pair(s, 0));

        while (!pq.isEmpty()) {
            Pair pair = pq.poll();
            int u = pair.node;
            long dist = pair.dist;

            if (dist > times[u]) continue;

            for (int i = h[u]; i != -1; i = ne[i]) {
                int j = e[i];
                long gr = g[i], re = r[i], di = d[i], fl = f[i];
                long tot = gr + re + 2 * di;
                long curt = dist % tot;

                if (fl == 1) curt = curt >= gr ? tot - curt : 0;
                else curt = curt >= gr + di && curt < gr + di + re ? 0 : curt < gr + di ? gr + di - curt : tot - curt + gr + di;

                long newDist = dist + curt + di;
                if (newDist < times[j]) {
                    times[j] = newDist;
                    pq.offer(new Pair(j, newDist));
                }
            }
        }

        return times[t] > Long.MAX_VALUE / 2 ? -1 : times[t];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt(),m = scanner.nextInt();
        int s = scanner.nextInt(),t = scanner.nextInt();

        Arrays.fill(h, -1);
        for (int i = 0; i < m; i++) {
            int a = scanner.nextInt(), b = scanner.nextInt();
            int green = scanner.nextInt(),red = scanner.nextInt();
            int dist = scanner.nextInt();
            add(a, b, green, red, dist, 1); add(b, a, green, red, dist, 0);
        }

        System.out.println(dijkstra(n, m, s, t));
    }

    static class Pair {
        private final int node;
        private final long dist;
        public Pair(int node, long dist) {
            this.node = node;
            this.dist = dist;
        }
    }    
}
java 复制代码
import java.util.*;

public class Main {
    static final int MAXV = 10;
    static final int mod = 1000000007;

    static int solve(int[] B, int N) {
        int[][][] f = new int[2][MAXV][MAXV];
        int cur = 0, ans = 0;
        for (int i0 = 0; i0 < MAXV; ++i0) {
            for (int i1 = 0; i1 < MAXV; ++i1) {
                cur = 0;
                Arrays.stream(f[cur]).forEach(arr -> Arrays.fill(arr, 0));
                f[cur][i0][i1] = 1;
                for (int i = 2; i < N; i++) {
                    cur ^= 1;
                    Arrays.stream(f[cur]).forEach(arr -> Arrays.fill(arr, 0));
                    for (int A0 = 0, prev = cur ^ 1; A0 < MAXV; ++A0) {
                        for (int A1 = 0; A1 < MAXV; ++A1) {
                            if (f[prev][A0][A1] == 0) continue;
                            for (int A2 = 0; A2 < MAXV; ++A2) {
                                if (Math.max(Math.max(A0, A1), A2) != B[i - 1]) continue;
                                if (i == N - 1) {
                                    if (Math.max(Math.max(A1, A2), i0) != B[i] || Math.max(Math.max(i0, i1), A2) != B[0]) continue;
                                }
                                f[cur][A1][A2] += f[cur ^ 1][A0][A1];
                                f[cur][A1][A2] %= mod;
                            }
                        }
                    }
                }
                for (int A0 = 0; A0 < MAXV; ++A0) {
                    for (int A1 = 0; A1 < MAXV; ++A1) {
                        ans += f[cur][A0][A1];
                        ans %= mod;
                    }
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int[] B = new int[N];
        for (int i = 0; i < N; ++i) {
            B[i] = scanner.nextInt();
        }
        System.out.println(solve(B, N));
    }
}
java 复制代码
package 蓝桥杯国赛;
import java.util.*;
//本题使用bigInteger会超时
//可以利用位运算找规律
//1
//121
//12321
//1234321
//123454321
//12345654321

//一共有2n-1位,
// 当i <= n 时
// 从右往左第i行,这一行有n - i + 1个 i,上面是i - 2,i - 4,.... 2(或1)
// 这一位是[(n - i + 1) * i + k] k = i/2*(i - 2)/2 或 k = (i-1) / 2*(i - 1)/2
// 当 i > n 时
// 末尾为奇数时对1到末尾相加,为偶数时0 到 n 相加

public class 六六大顺 {
	public static void main(String[] args)
	{
		Scanner scanner = new Scanner(System.in);
		long n = scanner.nextLong();
		StringBuilder x = new StringBuilder();
        long carry = 0, t;
        for (long i = 1; i <= n; i++) {
            t = (n - i + 1) * i;
            if (i % 2 == 0) t += i * (i - 2) / 4;
            else t += (i - 1) * (i - 1) / 4;
            t = t * 36 + carry;
            x.append(t % 10);
            carry = t / 10;
        }
        for (long i = n + 1; i < 2 * n; i++) {
            long last_value = 2 * n - i;
            long cnt = (last_value + 1) / 2;
            if (i % 2 == 0) t = (last_value + 2) * cnt / 2;
            else t = (last_value + 1) * cnt / 2;
            t = t * 36 + carry;
            x.append(t % 10);
            carry = t / 10;
        }
        x.append(carry);
        System.out.println(x.reverse().toString());
    }
}
java 复制代码
package 蓝桥杯国赛;

import java.util.Scanner;

//x的上一层的取值范围y 为 12 ~ 22,且为合数
//当y 为 12 :9 ~ 12
//当y 为 14 :14 - 7 + 1 = 8
//当y 为 15 :15 - 5 + 1 = 9
// ····

// 第一步求x 最大素数 z(如果是质数,就-1)
// y的取之范围为 (x - z + 1, x ),y为合数
// 然后在求区间中➖最大素数 + 1 最大的数 
public class 选素数 {
	static boolean is_prime(int n)
	{
		if(n <= 1) return false;
		for(int i = 2;i <= n / i;i++)
			if(n % i == 0) return false;
		return true;
	}
	
	static int lpf(int n)
	{
		int ans = 2;
		for(int i = 2; i <= n/i;i++)
		{
			while(n % i == 0)
			{
				ans = i;
				n /= i;
			}
		}
		return ans;
	}
	public static void main(String[] args)
	{
		Scanner scanner = new Scanner(System.in);
		int x = scanner.nextInt();
		if(is_prime(x)) {
			System.out.println(-1);
		}else {
			int z = lpf(x);
			int l = x - z + 1;
			
			int ans = (int)2e6;
			for(int i = l;i <= x;i++) {
				if(is_prime(i)) continue;
				ans = Math.min(ans,i - lpf(i) + 1);
			}
			System.out.println(ans);
		}
	}
}

国赛ac难度是大于省赛的,最后俩题放弃了

相关推荐
Amarantine、沐风倩✨28 分钟前
设计一个监控摄像头物联网IOT(webRTC、音视频、文件存储)
java·物联网·音视频·webrtc·html5·视频编解码·七牛云存储
古木201929 分钟前
前端面试宝典
前端·面试·职场和发展
路在脚下@1 小时前
spring boot的配置文件属性注入到类的静态属性
java·spring boot·sql
森屿Serien2 小时前
Spring Boot常用注解
java·spring boot·后端
苹果醋33 小时前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
Hello.Reader3 小时前
深入解析 Apache APISIX
java·apache
菠萝蚊鸭3 小时前
Dhatim FastExcel 读写 Excel 文件
java·excel·fastexcel
旭东怪4 小时前
EasyPoi 使用$fe:模板语法生成Word动态行
java·前端·word
007php0074 小时前
Go语言zero项目部署后启动失败问题分析与解决
java·服务器·网络·python·golang·php·ai编程
∝请叫*我简单先生4 小时前
java如何使用poi-tl在word模板里渲染多张图片
java·后端·poi-tl