【蓝桥杯】做些题儿

DP

数字三角形

刚开始用DFS

import java.util.Scanner;

public class 数字三角形 {
    static int[][] a = new int[1005][1005];
    static boolean[][] vis = new boolean[1005][1005];
    static int n;
    static int ans = Integer.MIN_VALUE;

    static int[][] dir = {
            {1, 0},
            {1, 1},
    };

    public static void dfs(int x, int y, int sum) {
        if (x == n) {
            ans = Math.max(ans, sum);
            return;
        }
        for (int i = 0; i < dir.length; i++) {
            int dx = x + dir[i][0];
            int dy = y + dir[i][1];
            if (dx >= 0 && dx <= n && dy >= 0 && dy <= n && !vis[dx][dy]) {
                vis[dx][dy] = true;
                dfs(dx, dy, sum + a[x][y]);
                vis[dx][dy] = false;
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            for (int j = i; j >= 0; j--) {
                a[i][j] = sc.nextInt();
            }
        }
        dfs(0, 0, 0);
        System.out.println(ans);
    }
}

发现只能过一个用例,其他都是超时

所以用DP

import java.util.Scanner;

public class 数字三角形 {
    static int[][] a = new int[1005][1005];
    static int[][] dp = new int[1005][1005];
    static int n;
    static int ans = Integer.MIN_VALUE;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = i; j > 0; j--) {
                a[i][j] = sc.nextInt();
            }
        }
        dp[1][1] = a[1][1];
        for (int i = 1; i <= n; i++) {
            for (int j = i; j > 0; j--) {
                dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j-1]) + a[i][j];

                if (i == n) {
                    ans = Math.max(ans, dp[i][j]);
                }
            }
        }
        System.out.println(ans);
    }
}

DFS

买瓜

第一次提交代码(可行性剪枝的DFS)

【非常非常要注意】这个状态树,有三种状态:切、不切和不买

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

public class BuyWaterMelon {
    static int n;
    static int m;
    static double[] a = new double[10];
    static int ans = -1;

    public static void DFS(double w, int cnt, int cut) {
        if (w > m) {
            return;
        }
        // cnt 是第几个瓜
        if (w == m) {
            ans = cut;
            return;
        }
        if (cnt == n) {
            return;
        }

        // 不买
        DFS(w, cnt + 1, cut);
        // 切瓜

        DFS(w + (a[cnt] / 2.0), cnt + 1, cut + 1);

        // 不切

        DFS(w + a[cnt], cnt + 1, cut);


    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        n = scan.nextInt();
        m = scan.nextInt();

        for (int i = 0; i < n; i++) {
            a[i] = scan.nextDouble();
        }
        DFS(0, 0, 0);
        System.out.println(ans);
        scan.close();
    }
}

通过70%用例,剩下超时,这还是剪了一部分

最大联通

import java.util.Scanner;

public class 最大连通 {

    static String[] map = new String[35];
    static boolean[][] vis = new boolean[30][60];
    static int res = 0;
    static int max = 1;
    static int[][] dir = {
            {1, 0},
            {0, 1},
            {-1, 0},
            {0, -1}
    };

    public static void DFS(int x, int y) {
        vis[x][y] = true;
//      标记一下初始的点已访问
        for (int i = 0; i < dir.length; i++) {
            int dx = x + dir[i][0];
            int dy = y + dir[i][1];

            if (dx >= 0 && dx < 30 && dy >= 0 && dy < 60 && !vis[dx][dy] && map[dx].charAt(dy) == '1') {
                vis[dx][dy] = true;
                DFS(dx, dy);
                max++;
                //没有回溯,因为不需要回溯
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 30; i++) {
            map[i] = sc.nextLine();
        }

        for (int i = 0; i < 30; i++) {
            for (int j = 0; j < 60; j++) {
                if (map[i].charAt(j) == '1' && !vis[i][j]) {
                    max = 1;
                    DFS(i, j);
                    res = Math.max(res, max);
                }
            }
        }
        System.out.println(res);
    }
}

飞机降落

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

public class 飞机降落 {
    static int[][] a = new int[10][3];
    static boolean[] vis = new boolean[10];
    static int n;
    static boolean judge = false;

    static void dfs(int cnt, int time) {
        // 成功降落
        if (cnt == n) {
            judge = true;
            return;
        }
        for (int i = 0; i < n; i++) {
            if (!vis[i] && time <= a[i][0] + a[i][1]) {

                int t = time; // 注意,如果当前时间不足,要等到规定时间才能起飞
                if (t < a[i][0]) {
                    t = a[i][0];
                }
                // 降落
                vis[i] = true;
                dfs(cnt + 1, t + a[i][2]);
                vis[i] = false;
            }
        }
    }

    public static void initialisation() {
        Arrays.fill(vis, false);
        judge = false;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int k = 0; k < t; k++) {
            initialisation();
            n = sc.nextInt();
            for (int i = 0; i < n; i++) {
                a[i][0] = sc.nextInt();
                a[i][1] = sc.nextInt();
                a[i][2] = sc.nextInt();
            }
            dfs(0, 0);
            if (judge) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
    }
}

日期

赛博朋克2077

import java.time.*;

public class Main {
    public static void main(String[] args) {
        int count = 0;
        LocalDate ld = LocalDate.of(2024, 12, 31);
        while (!ld.equals(LocalDate.of(2276, 12, 31))) {
            if (ld.getDayOfMonth() == 13 && ld.getDayOfWeek().toString().compareTo("SUNDAY") == 0) {
                count++;
            }
            ld = ld.plusDays(1);
        }
        System.out.println(count);
    }
}

回文日期

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Scanner;
import java.util.Stack;

public class 回文日期 {

    public static long getBetweenDays(Date start, Date end) {
        long s = start.getTime();
        long e = end.getTime();
        long l = e - s;
        return l / 24 / 60 / 60 / 1000;
    }

    public static boolean isPalindrome(LocalDate ld) {
        // s 2001-10-02
        String s = ld.toString();

        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < 4; i++) {
            stack.push(s.charAt(i));
        }
        for (int i = 5; i < 10; i++) {
            if (s.charAt(i) == '-') {
                continue;
            }
            if (!stack.pop().equals(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        LocalDate l = LocalDate.parse("20011002", DateTimeFormatter.ofPattern("yyyyMMdd"));
        Scanner sc = new Scanner(System.in);
        String date = sc.nextLine();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate startld = LocalDate.parse(date, dateTimeFormatter);
        LocalDate ld = LocalDate.parse(date, dateTimeFormatter);
        while (!isPalindrome(ld)) {
            ld = ld.plusDays(1);
        }
        System.out.println(ChronoUnit.DAYS.between(startld, ld));
    }
}

字符串

import java.util.HashMap;
import java.util.Scanner;

public class 次数差 {
    public static void main(String[] args) {
        HashMap<Character, Integer> hp = new HashMap<>();
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int min = 1000;
        int max = -1;
        boolean judge = true; // 判断是一串重复字符
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (hp.containsKey(c)) {
                int n = hp.get(c) + 1;
                if (n > max) {
                    max = n;
                }
                if (n < min) {
                    min = n;
                }
                hp.replace(c, n);
            } else {

                if (judge) {
                    judge = false;
                }
                hp.put(c, 1);
                if (min == 1000) {
                    min = 1;
                }
                if (max == -1) {
                    max = 1;
                }
            }
        }
        if (hp.size() == 1) {
            System.out.println(0);
            return;
        }
        System.out.println(max - min);
    }
}

一定要仔细看看有没有特殊情况,第一次只75%

原因是有一串重复字符

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值