备战蓝桥杯(java)(日益更新)

备战蓝桥杯(java)(日益更新)



前言:

将 c++ 代码 转换为 java 代码


提示:以下是本篇文章正文内容:

一、c++ 到 java 须要注意的地方:

  1. java中,如何实现像c++那样,typedef long long LL;
  2. java中,如何实现像c++那样,const int N = 1e5 + 10;
  3. java中,如何实现像c++那样,int n, m; int h[N], w[N];
  4. java中,如何实现像c++那样,scanf(“%d%d”, &n, &m);
  5. java中,如何实现像c++那样,#define x first #define y second typedef pair<int, int> PII;
  6. java中,如何实现像c++那样int l = max(1, L - t), r = min((LL)m, (LL)L + t);
  7. java中,如何实现像c++那样,sort(q, q+cnt)
  8. java中,如何实现像c++那样, bool check(int mid)
  9. java中,如何实现像c++那样,for(int i = 0; i < n; i ++) scanf(“%d%d”, &h[i], &w[i]);
  10. java中,如何实现像c++那样,for(int i = 0; i < n; i ++) scanf(“%d%d”, &w[i].x, &w[i].y);

二、多练java代码:(用java代码写算法模版)


1. acwing1277 分巧克力:

import java.util.Scanner;

public class acwing1227分巧克力 {

    static final int N = 100010;// 全局常量

    static int n, m;// 全局变量
    static int h[] = new int[N];
    static int w[] = new int[N];

    // 二分“二段性”判断
    static boolean check(int mid)
    {
        long res = 0;
        for(int i = 0; i < n; i ++)
        {
            res += h[i] / mid * (w[i] / mid);
            if(res >= m) return true;
        }
        return false;
    }


    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int n = scan.nextInt();
        int m = scan.nextInt();

		// 输入方式!!!
        for (int i = 0; i < n; i++)
        {
            h[i] = scan.nextInt();
            w[i] = scan.nextInt();
        }

        int l = 1, r = 100000;
        while(l < r)
        {
            // int mid = (l + r + 1) / 2;
            int mid = l + (r - l + 1) / 2;
            if(check(mid)) l = mid;
            else r = mid - 1;
        }

        System.out.println(r);
        scan.close();
    }

}

// 思考:为什么答案不对???


2. acwing5407 管道:

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

public class acwing5407管道 {
    static class Pair {
        public final Integer first;
        public final Integer second;

        public Pair(Integer first, Integer second) {
            this.first = first;
            this.second = second;
        }
    }

    static int N = 100010;
    static int n, m;
    static Pair[] w = new Pair[N];// 位于Li阀门会在Si时刻打开
    static Pair[] q = new Pair[N];// 二分后,区间的左右端点

    static boolean check(int mid)
    {
        int cnt = 0;
        for(int i = 0; i < n; i ++)
        {
           int L = w[i].first, S = w[i].second;// 位置,打开时刻
            if(S <= mid)
            {
                int t = mid - S;
                int l = (int) Math.max(1, L - t);
                int r = (int)Math.min((long)m, (long)L + t);
                q[cnt ++] = new Pair(l, r);// 注意
            }
        }
            // sort(q, q + cnt);
            // Java中没有直接对数组部分排序的方法,需要先将这部分元素提取出来
            Pair[] toSort = Arrays.copyOfRange(q, 0, cnt); // 提取前cnt个元素
            // 对提取出来的数组进行排序
            Arrays.sort(toSort, Comparator.comparingInt(i -> i.first));
            // 将排序后的元素放回原数组
            System.arraycopy(toSort, 0, q, 0, cnt);

            int st = -1, ed = -1;
            for(int i = 0; i < cnt; i ++)
            {
                if(q[i].first <= ed + 1) ed = Math.max(ed, q[i].second);
                else
                {
                    st = q[i].first;
                    ed = q[i].second;
                }
            }
        return st == 1 && ed == m;
    }


    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++)
        {
            int first = scan.nextInt();
            int second = scan.nextInt();
            w[i] = new Pair(first, second);
        }

        int l = 0, r = 2000000000;// 2e9
        while(l < r)
        {
            // int mid = (long)l + r >> 1;
            int mid = (int)((long)l + r >> 1);
            if(check(mid)) r = mid;
            else l = mid + 1;
        }

        System.out.println(r);
        scan.close();
    }

}



3. acwing562 壁画

import java.util.Scanner;

public class acwing562壁画 {

    static final int N = 100010;

    static int n;
    static int[] s = new int[N];
    static String str;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int T = scan.nextInt();

        for(int cases = 1; cases <= T; cases ++)
        {
            n = scan.nextInt();
            str = " " + scan.next();
            for(int i = 1; i <= n; i ++)
                    s[i] = s[i - 1] + str.charAt(i) - '0';

            int res = 0;
            int m = (n + 1) / 2;
            for(int i = m; i <= n; i ++)
                res = Math.max(res, s[i] - s[i - m]);

            System.out.println("Case #" + cases + ":" + res);
        }

        scan.close();
    }
}
/*
4
4
1332
Case #1:6
4
9583
Case #2:14
3
616
Case #3:7
10
1029384756
Case #4:31
 */

4. acwing1230 K倍区间

import java.util.Scanner;

public class acwing1230K倍区间 {

    //定义全局变量:
    static final int N = 100010;
    static int n, k;
    static long[] s = new long[N];
    static int[] cnt = new int[N];


    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        n = scan.nextInt();
        k = scan.nextInt();
        for(int i = 1; i <= n; i ++)
        {
            s[i] = scan.nextInt();
            s[i] += s[i - 1];// 前缀和
        }

        long res = 0;
        cnt[0] ++;
        for(int i = 1; i <= n; i ++)
        {
            res += cnt[(int) (s[i] % k)];
            cnt[(int)s[i] % k] ++;
        }

        System.out.println(res);
        scan.close();
    }
}
/*
5 2
1
2
3
4
5
6
 */

5. acwing4262 空调

import java.util.Scanner;

public class acwing4262空调 {
    // 全局变量:
    static final int N = 100010;
    static int n;
    static  int[] a = new int[N];


    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        n = scan.nextInt();
        for(int i = 1; i <= n; i ++) a[i] = scan.nextInt();
        for(int i = 1; i <= n; i ++)
        {
            int b = scan.nextInt();
            a[i] -= b;
        }

        for(int i = n; i >= 1; i --) a[i] -= a[i - 1];

        int pos = 0, neg = 0;
        for(int i = 1; i <= n; i ++)
        {
            if(a[i] >0) pos += a[i];
            else neg -= a[i];
        }

        System.out.println(Math.max(pos, neg));
        scan.close();
    }
}
/*
5
1 5 3 3 4
1 2 2 2 1
5
 */

6. acwing5396 棋盘

import java.util.Scanner;

public class acwing5396棋盘 {

    static final int N = 2010;
    static int n, m;
    static int[][] b = new int[N][N];

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        n = scan.nextInt();
        m = scan.nextInt();

        while (m-- > 0) {
            int x1 = scan.nextInt();
            int y1 = scan.nextInt();
            int x2 = scan.nextInt();
            int y2 = scan.nextInt();

            b[x1][y1] ++;
            b[x1][y2 + 1]--;
            b[x2 + 1][y1]--;
            b[x2 + 1][y2 + 1]++;
        }

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
                System.out.print(b[i][j] & 1);
            }
            System.out.println();
        }

        scan.close();
    }
}

总结

提示:这里对文章进行总结:

💕💕💕

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lennard-lhz

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

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

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

打赏作者

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

抵扣说明:

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

余额充值