蓝桥杯练习题(二)

📑前言

本文主要是【算法】------蓝桥杯练习题(二)的文章,如果有什么需要改进的地方还请大佬指出⛺️

🎬作者简介:大家好,我是听风与他🥇

☁️博客首页:CSDN主页听风与他

🌄每日一句:狠狠沉淀,顶峰相见

目录

1038.含2天数

java 复制代码
package 蓝桥杯第二次;

public class 含2天数1 {
	
	static int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		long res=0;
		for(int year=1900;year<=9999;year++) {
			if(leapyear(year)) days[2]=29;
			else days[2]=28;
			for(int month=1;month<=12;month++) {
				for(int day=1;day<=days[month];day++) {
					if(contain_2(year)||contain_2(month)||contain_2(day)) {
						res++;
					}
				}
			}
		}
		System.out.println(res);
	}
	
	public static boolean leapyear(int n) {
		return (n%4==0&&n%100!=0)||n%400==0;
	}

	public static boolean contain_2(int n) {
		while(n>0) {
			if(n%10==2) {
				return true;
			}
			n/=10;
		}
		return false;
	}
}

498.回文日期

java 复制代码
package 蓝桥杯第二次;

import java.util.Scanner;

public class 回文日期 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int flag1=0;
		int flag2=0;
		while(true) {
			n++;
			if(check_day(n)) {
				if (flag1==0 && check_huiwen(n)) {
					System.out.println(n);
					flag1=1;
				}
				if(flag2==0 && check_ab(n)) {
					System.out.println(n);
					flag2=1;
				}
			}
			if(flag1==1&&flag2==1) {
				return;
			}
		}
	}
	
	static int days[]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
	
	public static boolean check_day(int num) {
		int year = num/10000;
		int month = num%10000/100;
		int day = num%100;
		if(month>12||month<1) {
			return false;
		}
		if((year%4==0&&year%100!=0)||year%400==0) {
			days[2]=29;
		}else {
			days[2]=28;
		}
		if(day>days[month]||day<1) {
			return false;
		}
		return true;
	}
	public static boolean check_huiwen(int num) {
		int a[] = new int[8];
		int k=7;
		while(num>0) {
			a[k--]=num%10;
			num/=10;
		}
		if(a[0]==a[7]&&a[1]==a[6]&&a[2]==a[5]&&a[3]==a[4]) {
			return true;
		}
		return false;
	}
	
	public static boolean check_ab(int num) {
		int a[] = new int[8];
		int k=7;
		while(num>0) {
			a[k--]=num%10;
			num/=10;
		}
		if(a[0]==a[2]&&a[0]==a[5]&&a[0]==a[7]&&a[1]==a[3]&&a[1]==a[4]&&a[1]==a[6]) {
			return true;
		}
		return false;
	}

}

1624.小蓝吃糖果

java 复制代码
package 蓝桥杯第二次;

import java.util.Scanner;

public class 小蓝吃糖果1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		long sum=0;
		int max=0;
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int a[] = new int[n];
		for(int i=0;i<n;i++) {
			a[i] = sc.nextInt();
			if(a[i]>max) {
				max = a[i];
			}
			sum+=a[i];
		}
		if (max <= (sum+1)/2) {
			System.out.println("Yes");
		}else {
			System.out.println("No");
		}
	}

}

2411.星期几

java 复制代码
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		int n = sc.nextInt();
		int ans = (m+n)%7==0?7:(m+n)%7;
		System.out.println(ans);
    }
}

323.用杂志拼接信件

java 复制代码
package 蓝桥杯第二次;

import java.util.Scanner;

public class 用杂志拼接信件 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String a = sc.next();
		String b = sc.next();
		int num1[] = new int[26];
		int num2[] = new int[26];
		boolean ans=true;
		for(int i=0;i<a.length();i++) {
			num1[a.charAt(i)-97]++;
		}
		for(int i=0;i<b.length();i++) {
			num2[b.charAt(i)-97]++;
		}
		for(int i=0;i<26;i++) {
			if(num1[i]<num2[i]) {
				ans = false;
				break;
			}
		}
		if (ans) {
			System.out.println("YES");
		}else {
			System.out.println("NO");
		}
	}

}

📑文章末尾

相关推荐
majingming1232 小时前
FUNCTION
java·前端·javascript
zopple2 小时前
常见的 Spring 项目目录结构
java·后端·spring
Fly Wine3 小时前
Leetcode之有效字母异位词
算法·leetcode·职场和发展
xuxie994 小时前
N11 ARM-irq
java·开发语言
cjy0001114 小时前
springboot的 nacos 配置获取不到导致启动失败及日志不输出问题
java·spring boot·后端
程序员夏末4 小时前
【LeetCode | 第七篇】算法笔记
笔记·算法·leetcode
wefly20174 小时前
从使用到原理,深度解析m3u8live.cn—— 基于 HLS.js 的 M3U8 在线播放器实现
java·开发语言·前端·javascript·ecmascript·php·m3u8
zhenxin01224 小时前
Spring Boot实现定时任务
java
小江的记录本5 小时前
【事务】Spring Framework核心——事务管理:ACID特性、隔离级别、传播行为、@Transactional底层原理、失效场景
java·数据库·分布式·后端·sql·spring·面试
sheji34165 小时前
【开题答辩全过程】以 基于springboot的校园失物招领系统为例,包含答辩的问题和答案
java·spring boot·后端