2020年蓝桥杯Java B组第二场题目+部分个人解析

news2025/3/1 19:38:11

#A:门牌制作 624

解一:

public static void main(String[] args) {

   int count=0;

    for(int i=1;i<=2020;i++) {

    int n=i;

   while(n>0) {

     if(n%10==2) {

      count++;

    }

    n/=10;

 }

}

System.out.println(count);

}

解二: 

public static void main(String[] args) {

        int cnt=0;

        for (int i = 1; i <= 2020; i++) {

            String s=i+"";

            for (int j = 0; j < s.length(); j++) {

                if(s.charAt(j)=='2') cnt++;

            }

        }

        System.out.println(cnt);

    }

#寻找 2020  16520

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		char[][] ch = new char[300][300];
		long sum = 0 ;
		for (int i = 0; i < ch.length; i++) {
			ch[i] = scanner.nextLine().toCharArray();
		}
		for (int i = 0; i < ch.length; i++) {
			for (int j = 0; j < ch[0].length; j++) {
				if (j+3<ch[0].length) {
					if (ch[i][j]=='2' && ch[i][j+1]=='0' &&ch[i][j+2]=='2' &&ch[i][j+3]=='0' ) {
						sum++;
					}
				}
				if (i+3<ch.length) {
					if (ch[i][j]=='2' && ch[i+1][j]=='0' &&ch[i+2][j]=='2' &&ch[i+3][j]=='0') {
						sum++;
					}
				}
				if (i+3<ch.length && j+3<ch[0].length) {
					if (ch[i][j]=='2' && ch[i+1][j+1]=='0' &&ch[i+2][j+2]=='2' &&ch[i+3][j+3]=='0') {
						sum++;
					}
				}
			}
		}
		System.out.println(sum);//16520
	}


#C: 蛇形填数 761

解题思路:先手写几行就可以发现,第一行第一列的值为1,第二行第二列的值为5.第三行第三列的值为13,第四行第四列的值为25,仔细观察就可以发现第i行第i列的值为  a = a + (i*4)。

public static void main(String[] args) {

// TODO Auto-generated method stub

     int sum = 1;

     for (int i = 0; i < 20; i++) {

         sum = sum + (i * 4);

}System.out.println(sum);

}

#D:七段码dfs)  80


  解一:

static int[][] list = new int[][]{

            {0,1,0,0,0,1,0},

            {1,0,1,0,0,0,1},

            {0,1,0,1,0,0,1},

            {0,0,1,0,1,0,0},

            {0,0,0,1,0,1,1},

            {1,0,0,0,1,0,1},

            {0,1,1,0,1,1,0},

    };

    static Set<Set<Integer>> set = new HashSet<>();

    static boolean[] booleans = new boolean[7];



    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        ArrayList<Integer> arr = new ArrayList<>();

        for (int i = 0 ; i < 7 ; i++){

            booleans[i] = true;

            arr.add(i);

            set.add(new HashSet<>(arr));

            dfs(arr);

            booleans[i] = false;

            arr.remove(arr.size()-1);

        }

        System.out.println(set.size());

    }



    private static void dfs(ArrayList<Integer> arr) {

        set.add(new HashSet<>(arr));

        int last = arr.get(arr.size()-1);

        for (int i = 0 ; i < list[last].length ; i++){

            if (list[last][i] == 1 && !booleans[i]){

                booleans[i] = true;

                arr.add(i);

                dfs(arr);

                booleans[i] = false;

                arr.remove(arr.size()-1);

            }

        }

    }

  解二:

public static void main(String[] args) {
			creatGraph();
			dfs(1);
			System.out.println(count);
		}

		private static void creatGraph() {
			arr[1][2] = arr[1][6] = 1;
			arr[2][1] = arr[2][7] = arr[2][3] = 1;
			arr[3][2] = arr[3][4] = arr[3][7] = 1;
			arr[4][3] = arr[4][5] = 1;
			arr[5][4] = arr[5][6] = arr[5][7] = 1;
			arr[6][1] = arr[6][5] = arr[6][7] = 1;
		}

		private static void dfs(int k) {
			if (k > 7) {
				check();
				int q = 0;
				for (int i = 1; i <= 7; i++) {
					if (v[i] && res[i] == i) {
						q++;
					}
				}
				if (q == 1)
					count++;
				return;
			}
			v[k] = true;
			dfs(k + 1);
			v[k] = false;
			dfs(k + 1);
		}

		private static void check() {
			for (int i = 1; i <= 7; i++) {
				res[i] = i;
			}
			for (int i = 1; i <= 7; i++) {
				for (int j = 1; j <= 7; j++) {
					if (arr[i][j] != 0 && v[i] && v[j]) {
						int fx = find(i), fy = find(j);
						if (fx != fy) {
							res[fx] = fy;
						}
					}
				}
			}
		}

		private static int find(int p) {
			if (res[p] == p) {
				return p;
			} else {
				res[p] = find(res[p]);
				return res[p];
			}
		}

解三:

 static ArrayList<Integer>[] list;
	        static HashSet<Integer> set = new HashSet<Integer>();

	        public static void main(String[] args) {
	            init();
	            for(int i=0; i<7; ++i) {
	                vis[0] = i;
	                set.add(1<<i);
	                dfs(1, 1<<i);
	            }
	            System.out.println(set.size());
	        }

	        static int[] vis = new int[7];
	        public static void dfs(int n, int v) {
	            for(int i=0; i<n; ++i) {
	                for(int t : list[vis[i]]) {
	                    int p = v|(1<<t);
	                    if(!set.contains(p)) {
	                        set.add(p);
	                        vis[n] = t;
	                        dfs(n+1, p);
	                    }
	                }
	            }
	        }

	
	        public static void init() {
	            //存储七位数的关系 
	            list = new ArrayList[7];
	            for(int i=0; i<7; ++i) {
	                list[i] = new ArrayList<Integer>();
	            }
	            list[0].add(1);
	            list[0].add(5);

	            list[1].add(0);
	            list[1].add(6);
	            list[1].add(2);

	            list[2].add(1);
	            list[2].add(3);
	            list[2].add(6);

	            list[3].add(2);
	            list[3].add(4);

	            list[4].add(3);
	            list[4].add(5);
	            list[4].add(6);

	            list[5].add(0);
	            list[5].add(4);
	            list[5].add(6);

	            list[6].add(1);
	            list[6].add(2);
	            list[6].add(4);
	            list[6].add(5);
	        }

#E:排序  jonmlkihgfedcba

--------------------------------------------------------------------

--------------------------------------------------------------------

#F:成绩分析模拟、暴力)

public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		  int n=sc.nextInt();
		  int a,max=0,min=100;
		  double sum=0;
		  for(int i=0;i<n;i++) {
			  a=sc.nextInt();
			  if(max<a) {
				  max=a;
			  }
			  if(min>a) {
				  min=a;
			  }
			  sum+=a;
		  }
		  System.out.println(max);
		  System.out.println(min);
		  System.out.printf("%.2f",sum/n);

	}


#G:单词分析字符串)

 

public static void main(String[] args) {

 

Scanner sc=new Scanner(System.in);

String str=sc.next();

int[]arr =new int[26];

int a=0;;char ch = 'a';

for(int i=0;i<str.length();i++) {

    arr[str.charAt(i)-97]++;

    if(arr[str.charAt(i)-97]>a) {

     a=arr[str.charAt(i)-97];

     ch=str.charAt(i);

    }else if(arr[str.charAt(i)-97]==a) {

     if(ch-str.charAt(i)>0) {

     a=arr[str.charAt(i)-97];

         ch=str.charAt(i);

     }

    }

}

System.out.println(ch); System.out.println(a);





}

#H: 数字三角形DP)


   

 public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n=scanner.nextInt();

        int [][]arr=new int[n+1][n+1];

        for (int i = 1; i < arr.length; i++) {

            for (int j = 1; j <= i; j++) {

                int s=scanner.nextInt();

                arr[i][j]=s+Math.max(arr[i-1][j-1],arr[i-1][j]);

            }

        }

        System.out.println(n%2==1?arr[n][n/2+1]:Math.max(arr[n][n/2],arr[n][n/2+1]));

    }

}

  解二:

  public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();

        //base case : f(1) = a1

        int[][] dp = new int[n][n+1];

        int temp = scanner.nextInt();

        dp[0][0] = temp;

        int max = Integer.MIN_VALUE;

        for (int i = 2; i <= n; i++) {

            for (int j = 0; j < i; j++) {

                temp = scanner.nextInt();

                if (j != 0 && j != (i-1)) {

                    dp[i-1][j] = Integer.max(dp[i-2][j-1],dp[i-1][j])+temp;

                }else if (j == 0) {

                    dp[i-1][j] = dp[i-2][j]+temp;

                }else {

                    dp[i-1][j] = dp[i-2][j-1]+temp;

                }

                if(dp[i-1][j] > max)

                    max = dp[i-1][j];

            }

            dp[i-1][n] = max;

            max = Integer.MIN_VALUE;

        }

        System.out.println(dp[n-1][n]);

        scanner.close();

}

# I:子串分值和规律题吧)

 

解一:   

 public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int[] arr = new int[26];

        long res = 0;

        for(int i = 0;i < 26;i++){

            arr[i] = -1;

        }

        String s = sc.nextLine();

        String[] str = s.split("");

        int willIndex = str.length;

        for(int i = 0;i < str.length;i++){

            int lastIndex = arr[str[i].charAt(0)-97];

            res += (long)(i - lastIndex)*(willIndex - i);

            arr[str[i].charAt(0)-97] = i;

        }

        System.out.println(res);

    }

  解二:

public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        String str = sc.next();

        int[] nums = new int[26];

        long res = 0;

        long n = str.length();

        str = "0"+str;

        for (int i = 1; i < str.length(); i++) {

           

            res += (i - nums[str.charAt(i) - 'a']) * (n - i+1);

         

            nums[str.charAt(i) - 'a'] = i;

        }

        System.out.println(res);

    }

#J: 装饰珠

 

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

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

相关文章

【Azure 架构师学习笔记】- Azure Databricks (13) -- 搭建Medallion Architecture part 1

本文属于【Azure 架构师学习笔记】系列。 本文属于【Azure Databricks】系列。 接上文 【Azure 架构师学习笔记】- Azure Databricks (12) – Medallion Architecture简介 前言 上文已经介绍了关于Medallion的知识&#xff0c;本文开始用ADB 来实现&#xff0c; 但是基于内容较…

2025年2月21日优雅草内测分发站全新升级-测试运营-优雅草内测分发站新用户提供免费100下载点-2月28日正式运营并且提供私有化部署版本

2025年2月21日优雅草内测分发站全新升级-测试运营-优雅草内测分发站新用户提供免费100下载点-2月28日正式运营并且提供私有化部署版本 说明 优雅草内测分发站新用户提供免费100下载点&#xff0c;优雅草分运营站和demo测试站 运营站&#xff1a;www.youyacao.cn 提供免费100…

通过 PromptTemplate 生成干净的 SQL 查询语句并执行SQL查询语句

问题描述 在使用 LangChain 和 Llama 模型生成 SQL 查询时&#xff0c;遇到了 sqlite3.OperationalError 错误。错误信息如下&#xff1a; OperationalError: (sqlite3.OperationalError) near "sql SELECT Name FROM MediaType LIMIT 5; ": syntax error [SQL: …

IP段转CIDR:原理Java实现

&#x1f9d1; 博主简介&#xff1a;CSDN博客专家&#xff0c;历代文学网&#xff08;PC端可以访问&#xff1a;https://literature.sinhy.com/#/?__c1000&#xff0c;移动端可微信小程序搜索“历代文学”&#xff09;总架构师&#xff0c;15年工作经验&#xff0c;精通Java编…

[STM32]从零开始的STM32 DEBUG问题讲解及解决办法

一、前言 最近也是重装了一次keil&#xff0c;想着也是重装了&#xff0c;也是去官网下载了一个5.41的最新版&#xff0c;在安装和配置编译器和别的版本keil都没太大的区别&#xff0c;但是在调试时&#xff0c;遇到问题了&#xff0c;在我Debug的System Viewer窗口中没有GPIO&…

MySQL当中的Lock

1. 总览锁的类型 锁的类型&#xff1a; 锁类型 符号/缩写 描述 全局锁 FTWRL 锁定整个数据库&#xff08;FLUSH TABLES WITH READ LOCK&#xff09;&#xff0c;用于全库备份。 表级锁 - 表锁 S/X LOCK TABLES ... READ&#xff08;共享锁&#xff09;或 WRITE&#…

electron-builder打包时github包下载失败【解决办法】

各位朋友们&#xff0c;在使用electron开发时&#xff0c;选择了electron-builder作为编译打包工具时&#xff0c;是否经常遇到无法从github上下载依赖包问题&#xff0c;如下报错&#xff1a; Get "https://github.com/electron/electron/releases/download/v6.1.12/ele…

【免费】YOLO[笑容]目标检测全过程(yolo环境配置+labelimg数据集标注+目标检测训练测试)

一、yolo环境配置 这篇帖子是我试过的&#xff0c;非常全&#xff0c;很详细【cudaanacondapytorchyolo(ultralytics)】 yolo环境配置 二、labelimg数据集标注 可以参考下面的帖子&#xff0c;不过可能会出现闪退的问题&#xff0c;安装我的流程来吧 2.1 labelimg安装 label…

服务器IPMI用户名、密码批量检查

背景 大规模服务器部署的时候&#xff0c;少不了较多的网管和监测平台&#xff0c;这些平台会去监控服务器的性能、硬件等指标参数&#xff0c;为了便于管理和控制&#xff0c;则需要给服务器IPMI带外管理添加较多的用户&#xff0c;这就需要对较多的服务器检查所对应的IPMI用…

小红书湖仓架构的跃迁之路

作者&#xff1a;李鹏霖(丁典)&#xff0c;小红书-研发工程师&#xff0c;StarRocks Contributor & Apache Impala Committer 本文整理自小红书工程师在 StarRocks 年度峰会上的分享&#xff0c;介绍了小红书自助分析平台中&#xff0c;StarRocks 与 Iceberg 结合后&#x…

C++-第十七章:包装器

目录 第一节&#xff1a;std::function 第二节&#xff1a;std::bind 2-1.基本介绍 2-2.调整顺序(不常用) 2-3.调整个数 2-4.std::bind与std::function 下期预告&#xff1a; C中有3种可调用对象&#xff1a;函数指针、仿函数对象、lambda函数&#xff0c;经过包装器包装后屏…

TCP的三次握手与四次挥手:建立与终止连接的关键步骤

引言 ‌TCP&#xff08;传输控制协议&#xff09;工作在OSI模型的传输层‌。OSI模型将计算机网络功能划分为七个层级&#xff0c;从底层到顶层依次是&#xff1a;物理层、数据链路层、网络层、传输层、会话层、表示层和应用层。传输层负责在网络节点之间提供可靠的端到端通信&a…

2025计算机考研复试资料(附:网课+历年复试真题+140所高校真题+机试)

目录 2025 计算机考研复试经验全攻略&#xff0c;附超全资源&#x1f381; &#xff08;一&#xff09;网课资源 &#xff08;二&#xff09;历年复试真题 &#xff08;三&#xff09;140 所高校真题 二、专业知识复习篇 &#xff08;一&#xff09;复试专业课程 二&…

Milvus高性能向量数据库与大模型结合

Milvus | 高性能向量数据库&#xff0c;为规模而构建Milvus 是一个为 GenAI 应用构建的开源向量数据库。使用 pip 安装&#xff0c;执行高速搜索&#xff0c;并扩展到数十亿个向量。https://milvus.io/zh Milvus 是什么&#xff1f; Milvus 是一种高性能、高扩展性的向量数据…

腾讯游戏完成架构调整 IEG新设五大产品事业部

易采游戏网2月28日独家消息&#xff1a;继1月份腾讯天美工作室群完成内部组织架构调整后&#xff0c;腾讯旗下互动娱乐事业群&#xff08;IEG&#xff09;再次宣布对组织架构进行优化调整。此次调整的核心在于新设立了五大产品事业部&#xff0c;包括体育产品部、音舞产品部、V…

达梦数据库系列之安装及Mysql数据迁移

达梦数据库系列之安装及Mysql数据迁移 1. 达梦数据库1.1 简介1.2 Docker安装达梦1.2.1 默认密码查询1.2.2 docker启动指定密码 1.3 达梦数据库连接工具1.3.1 快捷键 2 Mysql数据库迁移至达梦2.1 使用SQLark进行数据迁移 1. 达梦数据库 1.1 简介 DM8是达梦公司在总结DM系列产品…

java jar包内的jar包如何打补丁

问题描述&#xff1a; 主包&#xff1a;hisca.jar&#xff0c;解压后 BOOT-INFO/lib下有其他jar包 因为一个小bug&#xff0c;需要修改这个hisca包下BOOT-INF/lib下的子jar包service-hisca-impl-1.0.0.jar中的一个service类及xml文件 操作步骤&#xff1a; 1、主包jar -xvf …

【企业场景】上线的项目如何进行限流

一、常见的四种速率限流算法 对于限流&#xff0c;最为直接的就是速率限流了 固定窗口算法 比如 10r/s 就是把时间线分为 1s 一段&#xff0c;也就是周期为 1s&#xff0c;对一个时间段的请求进行计数&#xff0c;超过 10 则舍弃&#xff0c;未超过则直接处理经过 1s 后&…

git - study

文章目录 git - study概述可以用 git gui工具来添加快捷命令工具如果要在提交日志中搜索&#xff0c;可以用gitk的view编辑功能实验环境直接用git自带环境进行git操作的好处查看git所有配置配置全局数据配置项目专用的数据查询配置数据的原始值配置git使用的文本编辑器获取某个…

FPGA之硬件设计笔记-持续更新中

目录 1、说在前面2、FPGA硬件设计总计说明3、 原理图详解 - ARITX - 7 系列3.1 顶层框图介绍3.2 FPGA 电源sheet介绍&#xff1a;3.2.1 bank 14 和 bank 15的供电3.2.2 bank 0的供电3.2.3 Bank34 35 的供电 3.3 核电压和RAM电压以及辅助电压 4 原理图详解-- Ultrascale ARTIX4.…