关于蓝桥杯历届真题-简单难度题的解析-研究生组/JavaA

33 篇文章 1 订阅
29 篇文章 3 订阅

蓝桥杯解析

子串分值和-第十一届蓝桥杯

子串分值和【第十一届】【省赛】

image-20220404083831166image-20220404083842504

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int sum = 0;
		Scanner scanner = new Scanner(System.in);
		char[] str = scanner.next().toCharArray();
		int j,k=0;
		for(int i = 0; i<str.length;i++) {//寻找右边界
			for(j = i+1;j<str.length;j++) {
				if(str[i] == str[j]) break;
			}
			for(k = i-1;k>=0;k--) {//寻找左边界
				if(str[i] == str[k])break;
			}
			sum += (i-k)*(j-i);
		}
		System.out.println(sum);

	}

}

会问日期-第十一届蓝桥杯

image-20220404105654644

import java.util.Scanner;
public class Main {
	static int[] mo = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; //  定义月份,判断日期是否合法的时候会用到
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int num = scanner.nextInt();
		boolean flag = false;
		for(int i = num+1;i<=99999999;i++) {
			String data = Integer.toString(i);//转换为字符串
			int year = i/10000; //年
			int month = i%10000/100;  //  月
			int day = i%100; //日
			if(check(year, month, day) && huiwen(data)) {
				if(!flag) {
					System.out.println(data);
					flag = true;
				}
				if(ABAB(data)) {
					System.out.println(data);
					break;
				}			
			}		
		}
	}
    //判断ABAB
	public static boolean ABAB(String data) {
		char[] str = data.toCharArray();  
        return str[0] == str[2] && str[1] == str[3];
	}
    //判断回文
	public static boolean huiwen(String data) {
		StringBuffer stringBuffer = new StringBuffer(data);
		String string = stringBuffer.reverse().toString();
		return data.equals(string);
	}
    //判断日期是否正确
	public static boolean check(int year,int month,int day) {
		if (month>12||month<1)  //  若月不合法就返回false
			return false;
		if(year % 400 == 0 ||(year % 4 ==0 && year%100 !=0)) {
			mo[2] = 29;
		}else mo[2] = 28;
		return day <= mo[month] && day >= 1;	
	}
}

成绩分析-第十一届蓝桥杯

image-20220404111426848

import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int[] nums = new int[n];
		int maxNum = Integer.MIN_VALUE;
		int minNum = Integer.MAX_VALUE;
		float sum = 0;
		for(int i =0;i<n;i++) {
			nums[i] = scanner.nextInt();
		}
		for(int i =0;i<n;i++) {
			maxNum = Math.max(maxNum,nums[i]);
			minNum = Math.min(minNum,nums[i]);
			sum += nums[i];
		}
		System.out.println(maxNum);
		System.out.println(minNum);
		System.out.printf("%.2f",sum/n);
	}

}

时间显示-第十二届蓝桥杯

image-20220405084523077

import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		long n = scanner.nextLong();
		 n = n/1000;
		 n = n %(24*60*60);//剩下一天的时间
		 long h = n/3600;
		 long m = n/60%60;
		 long s = n%60;
		 System.out.printf("%02d:",h);
		 System.out.printf("%02d:",m);
		 System.out.printf("%02d",s);	
	}
}

双向排序-第十二届蓝桥杯

image-20220405094022079

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int m = scanner.nextInt();
		Integer[] num  = new Integer[n];
		for(int i = 0;i<n;i++) num[i] = i+1;
		for(int i=0;i<m;i++) {
			int control = scanner.nextInt();
			int index = scanner.nextInt();
			MyComparator cmpComparator = new MyComparator();
			if(control == 0) {
				//前面nums[i][1]降序排列
				Arrays.sort(num, 0, index,cmpComparator);
			}else {
				Arrays.sort(num,index-1,num.length);
			}
		}
		for(int a :num) {
			System.out.print(a+" ");
		}
		
	}
}
class MyComparator implements Comparator<Integer>{

	@Override
	public int compare(Integer o1, Integer o2) {
		if(o1 > o2) return -1;
		else if(o1 < o2) return 1;
		else return 0;
	}
	
}

image-20220405101324803

import java.util.ArrayList;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;


public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
        Set<Integer> set = new HashSet<>();
        int n = scan.nextInt();
        int[] nums = new int[n];
        for(int i = 0; i<n;i++) {
        	nums[i] = scan.nextInt();
        }
        set.add(0);//加入0是为了将砝码的重量加入
        for(int i = 0;i<n;i++) {
        	ArrayList<Integer> list = new ArrayList<>(set);
        	for(int k:list){
                //set是有序不可重复的,所以可以直接进行加减不需要考虑去重
        		set.add(k+nums[i]);
        		set.add(Math.abs(k-nums[i]));
        	}
        }
        set.remove(0);
        System.out.println(set.size());
		
		
	}
}

货物摆放-第十二届蓝桥杯

image-20220405102046858

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class Main {
	public static void main(String[] args) {
		long n = 2021041820210418l;
		Set<Long> set = new HashSet<>();
		for(long i =1;i<=Math.sqrt(n);i++) {
			if(n%i==0) {
				set.add(i);
				if(n/i!=i) set.add(n/i);
			}		
		}
		int counrt = 0;
		ArrayList<Long> list = new ArrayList<Long>(set);
		for(long i = 0;i<set.size();i++) {
			for(long j = 0;j<set.size();j++) {
				for(long k = 0;k<set.size();k++) {
					if(list.get((int) i)*list.get((int) j)*list.get((int) k) == n) counrt ++;
				}
			}
		}
		System.out.println(counrt);
	}
}

直线-第十二届蓝桥杯

image-20220406090042576

import java.util.*;


public class Main {
	public static void main(String[] args) {
		HashSet<Integer> set = new HashSet<>();
		HashSet<String> hashSet = new HashSet<>();
		int x = 0;//横坐标
		int y = 0;//纵坐标
		for(x=0;x<20;x++) {
			for(y=0;y<21;y++) {
				set.add(x*100+y);
			}
		}
		List<Integer> list = new ArrayList<>(set);
		for(int i=0;i<set.size();i++) {
            //第一个点
			int x1 = list.get(i)/100;
			int y1 = list.get(i)%100;
			for(int j = i+1;j<set.size();j++) {
                //第二个点
				int x2 = list.get(j)/100;
				int y2 = list.get(j)%100;
				int kup = y1-y2;
				int kdown = x1-x2;
				if(kdown == 0) {
					hashSet.add("x=" + x1);
					continue;
				}
				int kgcd = gcd(kup, kdown);
				kup = kup/kgcd;
				kdown = kdown/kgcd;
				int bup = kdown*y1-kup*x1;
				int bdown = kdown;
				int bgcd = gcd(bup, bdown);
				bup = bup/bgcd;
				bdown = bdown/bgcd;
				hashSet.add(kup+"/"+kdown+"x"+"+"+bup+"/"+bdown);
			}
		}
		System.out.println(hashSet.size());
		
	}
	public static int gcd(int a,int b) {
		if(b == 0) return a;
		return gcd(b, a%b);
	}
}

Fibonacci 数列与黄金分割-第十届

image-20220406092921174

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		double n = sc.nextInt();
		if (n <= 21) {
			double x = f(n);
			double y = f(n+1);
			double ratio =(double)(x/y);
			System.out.println(String.format("%.8f", ratio));
		}else {
			System.out.println(0.61803399);
		}
	}
	
	public static double f(double n) {
		if (n == 1 || n == 2) {
			return 1;
		}
		return f(n-1)+f(n-2);
	}
}

平方和-第十届

image-20220406100050651

import java.util.*;
public class Main {
	public static void main(String[] args) {
		HashSet<Character> set = new HashSet<>();
		HashSet<Integer> set2 = new HashSet<>();
		set.add('0');
		set.add('1');
		set.add('2');
		set.add('9');
		for(int i = 1; i<=2019;i++) {
			String s = Integer.toString(i);
			//System.out.println(s);
			for(int j = 0; j<s.length();j++) {				
				if (set.contains(s.charAt(j))) {
					count++;
					set2.add(i);
					break;
				}
			}
		}
		long sumh = 0l;
		List<Integer> list = new ArrayList<>(set2);
		for(int i = 0; i<list.size();i++) {
			int temp = list.get(i);		
			sumh += temp*temp;
		}
		System.out.println(sumh);
	}	
}

数列求值-第十届

image-20220406103419408

import java.util.*;
public class Main {
	public static void main(String[] args) {
		long[] dp = new long[20190324];
		dp[0] = 1;
		dp[1] = 1;
		dp[2] = 1;
		for(int i = 3;i<20190324;i++) {
			dp[i] = dp[i-3]+dp[i-2]+dp[i-1];
			dp[i] = dp[i] % 10000;
		}
		System.out.println(dp[20190323]);		
	}	
}

完全二叉树的权值

package hello;

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int[] nums = new int[n];
		for(int i = 0;i<n;i++) {
			nums[i] = scanner.nextInt();
		}
		
		int res = Integer.MIN_VALUE;//每一层的和
		int depth = 0;//深度
		int count = 0;//最后的深度
		int index = 0;//索引
		boolean flag = true;
		while(flag) {
			depth ++;
			int sum = 0;//每一次的和0
			for(int j = 0; j<Math.pow(2,depth-1);j++) {
				if(index >= n) {
					flag = false;
					break;
				}
				sum += nums[index++];
			}
			if(res < sum) {
				res = sum;
				count = depth;
			}
		}			
		System.out.println(count);
	}
}

外卖优先级-第十届

image-20220407100123186

image-20220407100207562

package hello;

import java.util.*;
public class Main {
	static Scanner in = new Scanner(System.in);
	static int n, m, t;
	static Map<Integer, ArrayList<Integer>> map = new TreeMap<Integer, ArrayList<Integer>>();
	static int result;

	public static void main(String[] args) {
		n = in.nextInt();
		m = in.nextInt();
		t = in.nextInt();
		for (int i = 1; i <= m; ++i) {
			int time = in.nextInt();
			int id = in.nextInt();
			if (map.containsKey(id)) {
				map.get(id).add(time);
			} else {
				ArrayList<Integer> temp = new ArrayList<Integer>();
				temp.add(time);
				map.put(id, temp);
			}
		}

		ArrayList<Map.Entry<Integer, ArrayList<Integer>>> list = new ArrayList<Map.Entry<Integer, ArrayList<Integer>>>(
				map.entrySet());
		for (int i = 0; i < list.size(); ++i) {//商户
			Map.Entry<Integer, ArrayList<Integer>> entry = list.get(i);//商户对应的订单时间
			ArrayList<Integer> list2 = entry.getValue();//取出value 订单时间
			int level = 0;//优先级
			int[] count = new int[t + 2];
			boolean flag = false;
			for (int j = 0; j < list2.size(); ++j)
				count[list2.get(j)]++;//这个时刻的订单数

			for (int j = 1; j <= t; ++j) {
				if (count[j] == 0) {
					if (level > 0)
						level--;
					if (level <= 3)
						flag = false;
				} else {
					level += count[j] * 2;
					if (level > 5)
						flag = true;
				}
			}
			if (flag)
				result++;
		}
		System.out.println(result);
	}
}

修改数组-第十届

image-20220408091038170

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int[] nums = new int[n];
		for(int i = 0; i<n;i++) {
			nums[i] = scanner.nextInt();
		}
		HashSet<Integer> set = new HashSet<>();
		set.add(nums[0]);
		for(int i = 1; i<n;i++) {
			while(set.contains(nums[i])) {
				nums[i] ++;
			}
			set.add(nums[i]);
		}
		for(int num:nums) {
			System.out.println(num);
		}
	}
}
import java.util.Scanner;

public class Main {

	static int[] a = new int[100010];
	static int[] f = new int[100010];
	static int n;
	static Scanner cin = new Scanner(System.in);

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		// 初始化f数组为它本身
		for (int i = 1; i <= 100009; i++) {
			f[i] = i;
		}
		n = cin.nextInt();
		for (int i = 1; i <= n; i++) {
			a[i] = cin.nextInt();
			a[i] = find(a[i]);
			f[a[i]] = find(a[i] + 1);
		}

		for (int i = 1; i <= n; i++) {
			System.out.print(a[i] + " ");
		}
	}

	static int find(int x) {
		if (f[x] == x) {
			return x;
		} else {
			f[x] = find(f[x]);
			return f[x];
		}
	}
}

分数-第九届蓝桥杯

image-20220408092223111

public class Main {
	public static void main(String[] args) {
		System.out.println("1048575/524288");
	}
}

星期一-第九届蓝桥杯

image-20220408094609015

public class Main {
	//1901年1月1日至2000年12月31日之间
	//一年365,闰年有366
	//一周7天
	//算闰年
	public static void main(String[] args) {
		int count = 0;//闰年个数
		for(int i = 1901;i<2001;i++) {
			if(i % 400 == 0 || (i % 4 == 0 && i %100 != 0)) {
				count ++;
			}
		}
		//计算一百年一共多少天
		int sum = (100-count)*365 + count*366;
		int total = sum/7;
		//System.out.println(count);
		System.out.println(total);
	}
	
}
3
17:48:19 21:57:24
11:05:18 15:14:23
17:21:07 00:31:46
23:02:41 16:13:20
10:19:19 20:41:24
22:19:04 16:41:09

航班时间-第九届蓝桥杯

image-20220408105823261

image-20220408105835720

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) throws ParseException {
		int n = sc.nextInt();
		sc.nextLine();
		for(int i=0;i<n;i++) {
			long t1 = getTime();//第一次a->b 的时间差
			long t2 = getTime();//往返时b->a 的时间差
			long t = (t1+t2)/2; //两次的时间差相加 / 2
			System.out.printf("%02d:%02d:%02d\n",t/3600,t/60%60,t%60);//输出时分秒。%2d表共两位,不足就补0
		}
	}
	private static long getTime() throws ParseException {
		String line = sc.nextLine();//输入起飞时间 到达时间
		String[] split = line.split(" "); //获取起飞时间、到达时间、间隔天数的数组
		SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss");//日期格式化显示(10:02:03)
		Date t1 = sd.parse(split[0]); //将一个字符串类型的日期 转化为上述格式的日期
		Date t2 = sd.parse(split[1]);
		
		int day=0;//记录跨越的天数
		if(split.length==3) {//存在天数的跨度
			day = Integer.parseInt(split[2].substring(2, 3));
		}
		return day*24*60*60 + t2.getTime()/1000 - t1.getTime()/1000;
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小七rrrrr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值