蓝桥杯 java组 Day18 做题

本文探讨了编程题目,涉及数字三角形的求和、回文日期的生成以及通过规则猜解生日问题。通过示例展示了Java代码实现技巧和逻辑思考过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一题 卡片

import java.util.*;

public class Main {
	private static int[] a;
	private static int count = 0;
	
	public static void main(String[] args) {
		a = new int[10];
		for(int i = 0;i<=9;i++){
				a[i] = 2021;
	    }
		int temp = 1;
		int flag = 0;
		for(int i = 1;i <= 99999;i++) {
			int j = i;
			while(j>0) {
				if(a[j%10] == 0) {
					System.out.println(i-1);
					return ;
				}
				a[j%10]--;
				j = j/10;
			}
		}
	}
	
}

3181

第二题 单词分析

import java.util.*;

public class Main {
	private static int[] a;
	private static int count = 0;
	
	public static void main(String[] args) {
		a = new int[26];
		Scanner scanner = new Scanner(System.in);
	    char[] c = scanner.next().toCharArray();
		for(int i = 0;i<c.length;i++) {
			a[c[i]-'a']++;
		}
		int max = 0;
		int maxNum = 0;
		for(int i = 0;i<26;i++) {
			if(a[i]>maxNum) {
				maxNum = a[i];
				max = i;
			}
		}
		System.out.println((char)(max+'a'));
		System.out.println(maxNum);
	}
	
}

第三题 成绩统计

输入输出样例

示例

输入

7
80
92
56
74
88
100
0

输出

71%
43%

import java.util.*;

public class Main {
	private static int[] a;
	private static int count = 0;
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
	    int n = scanner.nextInt();
	    int k1 = 0;
	    int k2 = 0;
	    for(int i =1;i<=n;i++) {
	    	int temp = scanner.nextInt();
	    	if(temp>=60) {
	    		k1++;
	    	}
	    	if(temp>=85) {
	    		k2++;
	    	}
	    }
	    System.out.println(Math.round((double)(k1*100)/n)+"%");
	    System.out.println(Math.round((double)(k2*100)/n)+"%");
	}
	
}

第四题 数字三角形

示例

输入

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

输出

27

import java.util.*;

public class Main {
	private static int[] a;
	private static int count = 0;
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt();
		int[][] map = new int[N+1][N+1];
		int[][] dp = new int[N+1][N+1];
		for(int i = 1;i<=N;i++) {
			for(int j = 1;j<=i;j++) {
				map[i][j] = scanner.nextInt();
			}
		}
		dp[1][1] = map[1][1]; 
		for(int i = 2;i<=N;i++) {
			for(int j = 1;j<=i;j++) {
				if(j == 1) {
					dp[i][j] = dp[i-1][j] +map[i][j];
				}else if(i == j) {
					dp[i][j] = dp[i-1][j-1] + map[i][j];
				}else {
					dp[i][j] = Math.max(dp[i-1][j-1],dp[i-1][j]) +map[i][j];
				}
			}
		}
		
		if(N%2 == 1) {
			System.out.println(dp[N][N/2+1]);
		}else {
			System.out.println(Math.max(dp[N][N/2], dp[N][N/2+1]));
		}
		
	}
}

 第五题 等差素数列

import java.util.*;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.IconifyAction;

public class Main {
	private static int[] a;
	private static int count = 0;
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int[] prime = new int [10000];
		int k =0;
		for(int i = 2;i<=10000;i++) {
			if(isPrime(i)) {
				prime[k++] = i;
			}
		}
		
		for(int i =0;i<k;i++) {
			for(int d= 10;d<=500;d++) {
				int j = 0;
				for(j = 0;j<10;j++) {
					if(!isPrime(prime[i]+d*j)) {
						break;
					}
				}
				if(j==10) {
					System.out.println(d);
					return;
				}
			}
		}
	}
	
	private static boolean isPrime(int k) {
		for (int i = 2; i < Math.sqrt(k); i++) {
			if(k%i == 0) {
				return false;
			}
		}
		return true;
	}
}

 第六题 跑步锻炼


import java.util.Calendar;

public class Main {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2000, 0,1);
        int year = 0;
        int month = 0;
        int week = 0;
        int day = 0;
        int sum = 0;
        while(true) {
        	if(year == 2020&&month == 10&&day ==1) {
        		break;
        	}
        	year = calendar.get(Calendar.YEAR);
        	month = calendar.get(Calendar.MONTH) + 1;
        	week = calendar.get(Calendar.DAY_OF_WEEK);
        	day = calendar.get(Calendar.DAY_OF_MONTH);
        	if(week == 2||day == 1) {
        		sum = sum+2;
        	}else {
        		sum = sum+1;
        	}
        	calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        System.out.println(sum);
    }
}

 再也不用担心和日期相关的问题了

第七题 回文日期

输入输出样例

示例

输入

20200202

输出

20211202
21211212


import java.util.Calendar;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int year = N/10000;
        int month = (N-10000*year)/100;
        int day = N-10000*year-100*month;
        Calendar calendar = Calendar.getInstance();
        calendar.set(year,month-1,day+1);
        int flag1 = 0;
        int flag2 = 0;
        while(true) {
        	if(flag1==1&&flag2==1) {
        		break;
        	}
        	year = calendar.get(Calendar.YEAR);
        	month = calendar.get(Calendar.MONTH) + 1;
        	day = calendar.get(Calendar.DAY_OF_MONTH);
        	int Y1 = year/1000;
        	int Y2 = (year-1000*Y1)/100;
        	int Y3 = (year-1000*Y1-100*Y2)/10;
        	int Y4 = year - 1000*Y1-100*Y2-10*Y3;
        	int M1 = month/10;
        	int M2 = month-10*M1;
        	int D1 = day/10;
        	int D2 = day-D1*10;
        	
        	if((Y1==D2)&&(Y2==D1)&&(Y3==M2)&&(Y4==M1)&&flag1==0) {
        		System.out.print(year);
        		if(month<10) {
        			System.out.print("0"+month);
        		}else {
					System.out.print(month);
				}
        		if(day<10) {
        			System.out.print("0"+ day);
        		}else {
					System.out.print(day);
				}
        		System.out.println();
        		flag1 =1;
        	}
        	if(flag2==0&&Y1==Y3&&Y1==M2&&Y1==D2&&Y3==M2&&Y3==D2&&M2==D2&&Y2==Y4&&Y2==M1&&Y2==D1&&Y4==M1&&Y4==D1&&M1==D1) {
        		System.out.print(year);
        		if(month<10) {
        			System.out.print("0"+month);
        		}else {
					System.out.print(month);
				}
        		if(day<10) {
        			System.out.print("0"+ day);
        		}else {
					System.out.print(day);
				}
        		System.out.println();
        		flag2 =1;
        	}
        	calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
    }
}

像个小学生写的,太菜了。

第八题 猜生日

import java.math.BigInteger;
import java.util.Calendar;

public class Main {
    public static void main(String[] args) {
    	Calendar calendar = Calendar.getInstance();
    	calendar.set(1900, 0,1);
    	int year = 0;
    	int month = 0;
    	int day = 0;
    	while(true) {
    		year = calendar.get(Calendar.YEAR);
    		month = calendar.get(Calendar.MONTH)+1;
    		day = calendar.get(Calendar.DAY_OF_MONTH);
    		int  k = 10000*year + 100*month + day;
    		if(k%2012==0&&k%3==0&&k%12==0 && month == 6) {
    			System.out.println(year +" " + month + " " +day);
    			
    		}
    		calendar.add(Calendar.DAY_OF_MONTH, 1);
    	}
    	
    }
    
}

第九题 明码

这段信息是(一共 1010 个汉字):

import java.math.BigInteger;
import java.util.Calendar;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
    	Scanner scanner  = new Scanner(System.in);
    	int[] a = new int [33];
    	for (int i = 1; i <= 32; i++) {
			a[i] = scanner.nextInt();
		}
    	
    	for(int i =1;i<=32;i++) {
    		int temp = a[i];
    		int[] k =new int[8];
    		for(int j = 0;j<8;j++) {
    			k[j] = temp%2;
    			temp = temp/2;
    		}
    		if(a[i]<0) {
    			for(int j = 0;j<8;j++) {
    				if(k[j] == 1) {
    					k[j] = 0;
    					continue;
    				}
    				if(k[j] == 0){
    					k[j] = 1;
    				}
    			}
    			int jinwei = 0;
    			k[0] = k[0] +1; 
    			if(k[0]== 2) {
    				k[0] = 0;
    				jinwei  =1;
    			}else{
    				k[0] = 1;
    				jinwei = 0;
    			}
    			for(int j = 1;j<8;j++) {
    				k[j] = k[j] + jinwei;
    				if(k[j] == 2) {
    					k[j] = 0;
    					jinwei  = 1;
    				}else if(k[j] == 1){
    					k[j] = 1;
    					jinwei  = 0;
    				}else {
    					k[j] = 0;
    					jinwei = 0;
    				}
    			}
    		}
    		
    		for(int j = 7;j>=0;j--) {
    			if(k[j]==1) {
    				System.out.print(".");
    			}else {
    				System.out.print(" ");
    			}
    		}
    		if(i%2==0) {
    			System.out.println();
    		}
    	}
    }
    
}
4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0 
16 64 16 64 34 68 127 126 66 -124 67 4 66 4 66 -124 126 100 66 36 66 4 66 4 66 4 126 4 66 40 0 16 
4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0 
0 -128 64 -128 48 -128 17 8 1 -4 2 8 8 80 16 64 32 64 -32 64 32 -96 32 -96 33 16 34 8 36 14 40 4 
4 0 3 0 1 0 0 4 -1 -2 4 0 4 16 7 -8 4 16 4 16 4 16 8 16 8 16 16 16 32 -96 64 64 
16 64 20 72 62 -4 73 32 5 16 1 0 63 -8 1 0 -1 -2 0 64 0 80 63 -8 8 64 4 64 1 64 0 -128 
0 16 63 -8 1 0 1 0 1 0 1 4 -1 -2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 5 0 2 0 
2 0 2 0 7 -16 8 32 24 64 37 -128 2 -128 12 -128 113 -4 2 8 12 16 18 32 33 -64 1 0 14 0 112 0 
1 0 1 0 1 0 9 32 9 16 17 12 17 4 33 16 65 16 1 32 1 64 0 -128 1 0 2 0 12 0 112 0 
0 0 0 0 7 -16 24 24 48 12 56 12 0 56 0 -32 0 -64 0 -128 0 0 0 0 1 -128 3 -64 1 -128 0 0 

 第十题 数的分解

import java.math.BigInteger;
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Scanner;

public class Main {
	private static int[] a;
	private static int count = 0;
    public static void main(String[] args) {
    	a = new int[2020];
    	for (int i = 1; i <=2019; i++) {
			a[i] = i;
		}
    	dfs(1,2019);
    	System.out.println(count/6);
    	
    }
    
    private static void dfs(int x,int y) {
		if(x == 4) {
			fun();
			return;
		}
		for(int i = x;i<= y;i++) {
			swap(x,i);
			dfs(x+1, y);
			swap(x, i);
		}
	}
    
    private static void fun() {
		if(a[1]+a[2]+a[3] == 2019 && fun2(a[1]) &&fun2(a[2])&&fun2(a[3])) {
			count++;
		}
	}
    
    private static boolean fun2(int k) {
		while(k>0) {
			if(k%10==2||k%10==4) {
				return false;
			}
			k = k/10;
		}
		return true;
	}
    
    private static void swap(int i,int j) {
		int temp = 0;
		temp = a[i];
		a[i] = a[j];
		a[j] = temp;
	}
}

 全排列跑的很慢 写完才发现不用这么麻烦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值