📑前言
本文主要是【算法】——蓝桥杯练习题(二)的文章,如果有什么需要改进的地方还请大佬指出⛺️
🎬作者简介:大家好,我是听风与他🥇
☁️博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见
目录
- 📑前言
- 1038.含2天数
- 498.回文日期
- 1624.小蓝吃糖果
- 2411.星期几
- 323.用杂志拼接信件
- 📑文章末尾
1038.含2天数
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.回文日期
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.小蓝吃糖果
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.星期几
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.用杂志拼接信件
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");
}
}
}