Java in 蓝桥杯

Java in 蓝桥杯

JDK是1.6,集成环境是Eclipse,有填空题..补充中

一码当先

先学会写基本的使用是根本!

需要在项目根目录下建个aa.txt的文本文件

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // 读取文件1:然后用while((str=br.nextLine()!=null)不断读取内容
        BufferedReader br = new BufferedReader(new FileReader("aa.txt"));

        // 读取文件2:控制台输入重定向到文件,减少复制的麻烦
        System.setIn(new FileInputStream("aa.txt"));

        // 进制转转1:将255转换成16进制,二进制,八进制同理
        System.out.println(Integer.toHexString(255));

        // 进制转转2:将FF作为16进制的数来转换成10进制表示
        System.out.println(Integer.parseInt("FF", 16));

        // 输入加快1:使用缓冲区
        Scanner in = new Scanner(new BufferedInputStream(System.in));//更快
        System.out.println(in.nextLine());
        in.close();
        System.out.println("---------------------------------");
        Integer[] arr1 = {1, 3, 5, 7, 2, -1};

        // 数据排序1:基本数据类型升序直接使用Arrays.sort();
        Arrays.sort(arr1);
        System.out.println(Arrays.toString(arr1));

        // 数据排序2:使用自定义比较器(写个类继承Comparator)以按照要求排序
        Arrays.sort(arr1, new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        System.out.println(Arrays.toString(arr1));

        // 数据排序3:自定义对象需实现Comparable接口
        Point[] points = {new Point(1, 2), new Point(2, 1), new Point(-22, 11), new Point(1, -1)};
        Arrays.sort(points);
        System.out.println(Arrays.toString(points));

        // 数据结构1:栈LIFO peek是看栈顶不弹出,pop是弹出并返回栈顶,ArrayDeque
        Stack<Integer> stack = new Stack<Integer>();
        stack.push(2);
        stack.push(1);
        stack.push(3);
        System.out.println(stack.pop());
        System.out.println(stack.peek());

        /*
          数据结构2:双向队列Deque()可以作为栈使用,性能比extends Vector的Stack()好
          使用栈时,用ArrayDeque的push和pop方法;
          使用队列时,使用ArrayDeque的add和remove方法。
         */
        ArrayDeque<Integer> stk = new ArrayDeque<Integer>();

        // 数据结构3:优先队列(小顶堆) 无论入队顺序,当前最大的元素优先出队。用于每次提取最小的值
        Queue<Point> priorityQueue = new PriorityQueue<Point>(16);
        priorityQueue.addAll(Arrays.asList(points));
        System.out.println(priorityQueue.poll());


    }
}

class Point implements Comparable<Point> {
    int x, y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    // 先比较x,再比较y,升序
    public int compareTo(Point o) {
        return (x != o.x ? x - o.x : y - o.y);
    }

    @Override
    public String toString() {
        return "Point:{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
}

基础算法

动态规划

目的:穷举求最值,优化递归树,消除重叠子问题

要点:备忘录,用空间换时间

难点:状态转移方程、重叠子问题、最优子结构、变种多

回溯算法

贪心算法

分治算法

奇技淫巧

posted @   禾几元  阅读(139)  评论(1编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示