第十三届蓝桥杯国赛JavaB组题解

news2024/10/5 17:21:25

A. 重合次数

在这里插入图片描述
思路:

枚举不同的时刻,判断哪些时刻秒针和分针表示的数字是相同的。这道题坑就坑在:xx:59:59 xx:00:00分针和时。也就是说一个小时会重叠两次。
题目要求是分钟和秒钟的重叠次数,故时钟,分钟,秒钟同时重叠的次数不算(这题还是有点咬文嚼字了,我说怎么比答案多了8次)。

代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

public class Main {
	
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
	static StreamTokenizer in = new StreamTokenizer(br);

	public static int nextInt() throws IOException {
		in.nextToken();
		return (int) in.nval;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int h = 6, m = 13, s = 22;
		int cnt = 0;
		while (h != 14 || m != 36 || s != 20) {
			if (m == s) cnt++;
			s++;
			if (s == 60) {
				m++;
				s = 0;
			}
			if (m == 60) {
				h++;
				m = 0;
			}
		}
		// 减去整点重复
		System.out.println(cnt - 8);
	}
}

B. 数数

在这里插入图片描述
思路:

这道题本来想通过搜索判断一个数是否可以用12个质因数表示,但是失败了。后来想到可以用试除法分解质因数判断一个数是否可以被分解成12个质因子,虽然最后也跑了一会儿不过作为填空题还是给过了。

代码:

public class Main {
    
    static int ans;
    
    public static boolean check(int x) {
        int cnt = 0;
        
        for (int i = 2; i <= x / i; i++) {
            if (x % i == 0) {
                while (x % i == 0) {
                    x /= i;
                    cnt++;
                }
            }
        }
        
        return cnt == 12;
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // for (int i = 2333333; i <= 23333333; i++) 
        //     if (check(i)) ans++;
        System.out.println(25606);
    }
}

C. 左移右移

在这里插入图片描述
在这里插入图片描述
思路:

初始的想法是保存每个数的位置,当某个数左移的时候,左边的每个数的位置都+1,右边的数不变;当某个数右移的时候,右边的每个数的位置都-1,左边的数不变,自然而然想到了差分,算出位置后,接着再根据位置输出即可。但是后来想想每次都要求得每个数的位置,复杂度为 O ( N ) O(N) O(N),一共有 M M M 个操作,复杂度就是 O ( N M ) O(NM) O(NM),最大程度会 4 × 1 0 10 4 × 10^{10} 4×1010 ,显然会超时,这种做法不可取。

后来想到了用双链表进行求解,每次交换位置的时候要先删除再插入,在插入的时候更新元素的位置。在 O ( N ) O(N) O(N) 的复杂度内可以得到正确答案。

代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class Main {
    
    static final int N = 400010;
    static int[] e = new int[N], l = new int[N], r = new int[N], loc = new int[N];
    static int n, m, idx;
    
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    
    public static void insert(int k, int x) {
        loc[x] = idx;
        e[idx] = x;
        l[idx] = k;
        r[idx] = r[k];
        l[r[k]] = idx;
        r[k] = idx++;
    }
    
    public static void remove(int k) {
        r[l[k]] = r[k];
        l[r[k]] = l[k];
    }

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String[] nm = br.readLine().split(" ");
        n = Integer.parseInt(nm[0]);
        m = Integer.parseInt(nm[1]);
        
        // 0为头节点,1为尾节点
        l[1] = 0;
        r[0] = 1;
        idx = 2;
        for (int i = 1; i <= n; i++) insert(l[1], i);
        while (m-- != 0) {
            String[] ops = br.readLine().split(" ");
            char op = ops[0].charAt(0);
            int x = Integer.parseInt(ops[1]);
            if (op == 'L') {
                remove(loc[x]);
                insert(0, x);
            } else {
                remove(loc[x]);
                insert(l[1], x);
            }
        }
        
        for (int i = r[0]; i != 1; i = r[i]) out.printf("%d ", e[i]);
        out.flush();
    }
}

D. 窗口

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
思路:

定义窗口类,存储相关信息:

static class Window implements Comparable<Window> {
        int pid, top, left, height, width, priority;
        
        Window(int pid, int top, int left, int height, int width, int priority) {
            this.pid = pid;
            this.top = top;
            this.left = left;
            this.height = height;
            this.width = width;
            this.priority = priority;
        }

        void move(int ver, int hor) {
            this.top += ver;
            this.left += hor;
            this.priority = ++pri;
        }
        
        void resize(int h, int w) {
            this.height = h;
            this.width = w;
            this.priority = ++pri;
        }

        @Override
        public int compareTo(Window o) {
            // TODO Auto-generated method stub
            return priority - o.priority;
        }
        
    }

依次处理每个操作,更新窗口信息。接着将窗口按优先级从小到大排序,优先级为0的窗口不予处理。在根据排好序的窗口绘制图形,就能实现优先级较大的窗口处于顶层。

代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;

public class Main {
    
    static final int N = 300, M = 100010;
    static char[][] g = new char[N][N];
    static Window[] arr = new Window[M];
    static Window[] wins = new Window[M];
    static int n, m, k, pri = 0;
    
    static class Window implements Comparable<Window> {
        int pid, top, left, height, width, priority;
        
        Window(int pid, int top, int left, int height, int width, int priority) {
            this.pid = pid;
            this.top = top;
            this.left = left;
            this.height = height;
            this.width = width;
            this.priority = priority;
        }

        void move(int ver, int hor) {
            this.top += ver;
            this.left += hor;
            this.priority = ++pri;
        }
        
        void resize(int h, int w) {
            this.height = h;
            this.width = w;
            this.priority = ++pri;
        }

        @Override
        public int compareTo(Window o) {
            // TODO Auto-generated method stub
            return priority - o.priority;
        }
        
    }
    
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    
    public static void draw(int x1, int y1, int x2, int y2) {
        for (int i = x1; i <= x2; i++) 
            for (int j = y1; j <= y2; j++) {
                if (i < 0 || i >= n || j < 0 || j >= m) continue;
                if (i == x1 || i == x2) {
                    if (j == y1 || j == y2) g[i][j] = '+';
                    else g[i][j] = '-';
                } else if (j == y1 || j == y2) g[i][j] = '|';
                else g[i][j] = ' ';
            }
    }

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String[] nm = br.readLine().split(" ");
        n = Integer.parseInt(nm[0]);
        m = Integer.parseInt(nm[1]);
        k = Integer.parseInt(br.readLine());
        
        HashSet<Integer> pSet = new HashSet<Integer>();
        
        for (int i = 0; i < k; i++) {
            String[] op = br.readLine().split(" ");
            int id = Integer.parseInt(op[1]);
            pSet.add(id);
            if (op[0].equals("new")) {
                int t = Integer.parseInt(op[2]);
                int l = Integer.parseInt(op[3]);
                int h = Integer.parseInt(op[4]);
                int w = Integer.parseInt(op[5]);
                arr[id] = new Window(id, t, l, h, w, ++pri);
            } else if (op[0].equals("move")) {
                int ver = Integer.parseInt(op[2]);
                int hor = Integer.parseInt(op[3]);
                arr[id].move(ver, hor);
            } else if (op[0].equals("resize")) {
                int h = Integer.parseInt(op[2]);
                int w = Integer.parseInt(op[3]);
                arr[id].resize(h, w);
            } else if (op[0].equals("close")) {
                arr[id].priority = 0;
            } else {
                arr[id].priority = ++pri;
            }
        }
        
        int num = 0;
        
        for (Integer id : pSet) {
            wins[num++] = arr[id];
        }
        
        Arrays.sort(wins, 0, num);
        
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                g[i][j] = '.';
        
        for (int i = 0; i < num; i++) {
            if (wins[i].priority != 0) {
                int x1 = wins[i].top;
                int y1 = wins[i].left;
                int x2 = x1 + wins[i].height - 1;
                int y2 = y1 + wins[i].width - 1;
                draw(x1, y1, x2, y2);
            }
        }
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++)
                out.print(g[i][j]);
            out.println();
        }
        out.flush();  
    }
}

E. 迷宫

在这里插入图片描述
在这里插入图片描述
思路:
bfs求最短路,从终点一次bfs可以求得到所有点的最短距离,将所有点的最短距离相加再除以点数即可得到最短距离的数学期望。

代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
	
	static final int N = 2010, M = N * N, inf = 0x3f3f3f3f;
	static int[] h = new int[M], e = new int[N * 2], ne = new int[N * 2];
	static int[][] d = new int[N][N];
	static int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
	static int n, m, idx;
	
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
	static StreamTokenizer in = new StreamTokenizer(br);

	public static int nextInt() throws IOException {
		in.nextToken();
		return (int) in.nval;
	}
	
	public static void add(int a, int b) {
		e[idx] = b;
		ne[idx] = h[a];
		h[a] = idx++;
	}
	
	public static int get(int x, int y) {
		return x * N + y;
	}
	
	public static void bfs() {
		for (int i = 0; i <= n; i++)
			for (int j = 0; j <= n; j++)
				d[i][j] = inf;
		Queue<Integer> q = new LinkedList<Integer>();
		q.offer(n * N + n);
		d[n][n] = 0;
		
		while (!q.isEmpty()) {
			int v = q.poll();
			int x = v / N, y = v % N;
			for (int i = 0; i < 4; i++) {
				int tx = x + dx[i], ty = y + dy[i];
				if (tx <= 0 || tx > n || ty <= 0 || ty > n) continue;
				if (d[tx][ty] > d[x][y] + 1) {
					d[tx][ty] = d[x][y] + 1;
					q.offer(tx * N + ty);
				}
			}
			for (int i = h[v]; i != -1; i = ne[i]) {
				int j = e[i];
				int tx = j / N, ty = j % N;
				if (d[tx][ty] > d[x][y] + 1) {
					d[tx][ty] = d[x][y] + 1;
					q.offer(tx * N + ty);
				}
			}
		}
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		n = nextInt();
		m = nextInt();
		
		Arrays.fill(h, -1);
		while (m-- != 0) {
			int x1 = nextInt();
			int y1 = nextInt();
			int x2 = nextInt();
			int y2 = nextInt();
			int a = get(x1, y1);
			int b = get(x2, y2);
			add(a, b);
			add(b, a);
		}
		
		bfs();
		
		long ans = 0;
		for (int i = 1; i <= n; i++) 
			for (int j = 1; j <= n; j++) 
				ans += d[i][j];
		
		out.printf("%.2f", ans * 1.0 / (n * n));
		out.flush();
	}

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/554926.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

MySQL---事务

1. 事务操作 开启事务&#xff1a;Start Transaction 任何一条DML语句(insert、update、delete)执行&#xff0c; 标志事务的开启命令&#xff1a;BEGIN 或 START TRANSACTION 提交事务&#xff1a;Commit Transaction 成功的结束&#xff0c;将所有的DML语句操作历史记录…

G2O学习使用

g2o全称是General Graph Optimization&#xff0c;也就是图优化&#xff0c;我们在做SLAM后端或者更加常见的任何优化问题&#xff08;曲线拟合&#xff09;都可以使用G2O进行处理。 就经验而言&#xff0c;solvers给人的感觉是大同小异&#xff0c;而 types 的选取&#xff0…

C语言小游戏——扫雷

前言 结合前边我们所学的C语言知识&#xff0c;本期我们将使用C语言实现一个简单的小游戏——扫雷 目录 前言 总体框架设计 多文件分装程序 各功能模块化实现 初始化棋盘 棋盘打印 埋雷 判赢与排雷 游戏逻辑安排 总结 总体框架设计 和三子棋相同&#xff0c;游戏开始时…

32岁测试工程师,陷入中年危机,最终我裸辞了....

前言 今年32岁&#xff0c;我从公司离职了&#xff0c;是裸辞。 前段时间&#xff0c;我有一件事情一直憋在心里很难受&#xff0c;想了很久也没找到合适的人倾诉&#xff0c;就借着今天写出来。 我一个十几年IT经验&#xff0c;七年测试经验的职场老人&#xff0c;我慢慢涨…

02 Android开机启动之BootLoader及kernel的启动

Android开机启动之BootLoader及kernel的启动 1、booloader的启动流程 第一阶段:硬件初始化,SVC模式,关闭中断,关闭看门狗,初始化栈,进入C代码 第二阶段:cpu/board/中断初始化;初始化内存以及flash,将kernel从flash中拷贝到内存中,执行bootm,启动内核 2、kernel的启…

学习如何将Jenkins与UI测试报告完美整合,事半功倍,轻松获取高薪职位!

目录 引言 &#xff08;一&#xff09;在本地整合出报告 1.在cmd分别安装pytest和allure-pytest 2.进入需要执行的代码所在的路径 3.运行测试报告&#xff0c;代码如下 4.解析此json文件&#xff0c;代码如下&#xff08;新打开cmd进入路径&#xff09; 5.打开此HTML文件…

包管理工具

包 package&#xff0c;代表了一组特定功能的源码集合。 包管理工具 管理包的应用软件&#xff0c;可以对包进行下载安装、更新、删除、上传等操作。 借助包管理工具&#xff0c;可以快速开发项目&#xff0c;提升开发效率。 常用包管理工具 npm&#xff08;nodejs官方内…

百度API实现自动写诗

作者介绍 张琪&#xff0c;男&#xff0c;西安工程大学电子信息学院&#xff0c;2022级研究生 研究方向&#xff1a;机器视觉与人工智能 电子邮件&#xff1a;3126743452qq.com 王泽宇&#xff0c;男&#xff0c;西安工程大学电子信息学院&#xff0c;2022级研究生&#xff0…

Spring——Spring_IOC

1.Spring_IOC概念引入 控制反转 2.Spring_IOC代码测试 IOC代码演示 控制反转&#xff1a;就是创建对象的权力交给了容器 1.创建一个接口&#xff0c;定义一个抽象方法 package org.example;public interface Empdao {int addemp(); } 2.创建一个实现类&#xff0c;实现这…

两台电脑之间怎么互相传文件?

​随着技术的发展&#xff0c;我们似乎可以从家中或工作电脑远程访问另一台电脑。同时&#xff0c;一些用户也在想&#xff0c;“我能不能把文件从一台电脑远程传输到另一台电脑&#xff0c;这样我就可以在本地电脑上随心所欲地查看和编辑文件了”。 这个问题的答案是…

Android自定义一个省份简称键盘

hello啊各位老铁&#xff0c;这篇文章我们重新回到Android当中的自定义View&#xff0c;其实最近一直在搞Flutter&#xff0c;初步想法是&#xff0c;把Flutter当中的基础组件先封装一遍&#xff0c;然后接着各个工具类&#xff0c;列表&#xff0c;网络&#xff0c;统统由浅入…

PostgreSQL实战之物理复制和逻辑复制(八)

目录 PostgreSQL实战之物理复制和逻辑复制&#xff08;八&#xff09; 8 级联复制 8.1 级联复制物理架构 8.2 级联复制部署 PostgreSQL实战之物理复制和逻辑复制&#xff08;八&#xff09; 8 级联复制 实际上PostgreSQL支持备库既可接收主库发送的将WAL&#xff0c;也支持…

Java泛型基本知识附面试题

一次平平无奇的面试 为什么要写这篇文档&#xff0c;主要就是在字节二面的时候&#xff0c;面试官提了这么一个问题 面试官&#xff1a;Java中的List<Integer>里有可能存String类型元素吗&#xff1f; 当时的我&#xff1a;应该…不可以吧&#xff0c;好像编译器会报错…

2023口腔护理行业分析:市场需求多元化,细分市场持续多变

随着人们生活水平的提高以及口腔护理意识的提升&#xff0c;消费者对于口腔护理的诉求愈发多样化&#xff0c;对于与此相关的产品&#xff0c;包括牙膏、牙刷、牙齿美白产品、漱口水、牙线等产品的需求也日益提高&#xff0c;在这种情况下&#xff0c;口腔护理相关细分产品的销…

软件研发管理高效的关键:11项自动化功能

1、自动锁定需求缺陷 为了提高用户需求分析质量&#xff0c;尽早发现需求缺陷&#xff0c;CoCode开发云特开发了需求分析工具&#xff0c;使用AI&#xff0c;通过需求测试和一致性检测&#xff0c;能够在几分钟内快速分析用户需求缺陷&#xff0c;如歧义、重复、遗漏、不一致和…

深度学习神经网络学习笔记-多模态方向-09-VQA: Visual Question Answering

摘要 -我们提出了自由形式和开放式视觉问答(VQA)的任务。给定一张图像和一个关于图像的自然语言问题&#xff0c;任务是提供一个准确的自然语言答案。镜像现实场景&#xff0c;比如帮助视障人士&#xff0c;问题和答案都是开放式的。视觉问题有选择地针对图像的不同区域&#…

nodejs+vue飞机机票在线预订票网站

本机票预订系统以vue作为框架&#xff0c;b/s模式以及MySql作为后台运行的数据库&#xff0c;同时使用Tomcat用为系统的服务器。本系统主要包括首页&#xff0c;个人中心&#xff0c;用户管理&#xff0c;机票类型管理&#xff0c;机票信息管理&#xff0c;订票信息管理&#x…

STM32F4_DMA直接存储器详解

目录 1. 什么是DMA 2. DMA的主要特性 3. DMA功能 3.1 DMA功能框图 3.2 DMA事务 3.3 通道选择 3.4 仲裁器 3.5 DMA数据流 3.6 源、目标和传输模式 3.6.1 外设到存储器模式 3.6.2 存储器到外设模式 3.6.3 存储器到存储器模式 3.7 指针递增 3.8 DMA内存占用 3.9 存…

<SQL>《SQL命令(含例句)精心整理版(1)》

《SQL命令精心整理》 1 SQL基础2 关键字 select & distinct3 排序检索 - order by & desc & asc4 where 语句5 操作符 -and & or & not & in6 通配符6.1 LIKE % 谓词 下划线 方括号 7 计算、拼接、别名 1 SQL基础 名词概念数据库&#xff08;database…

Mit6.006-problemSet03

3-1 哈希练习&#xff08;Hash Practice&#xff09; (a) 按顺序插入整数keys A[47, 61, 36, 52, 56, 33, 92]到尺寸为7的哈希表中&#xff0c;使用哈希函数 h ( k ) ( 10 k 4 ) m o d 7 h(k)(10k4)mod7 h(k)(10k4)mod7。哈希表的每个插槽&#xff0c;存储一个key&#xff…