备战蓝桥杯(java)(日益更新)
文章目录
- 备战蓝桥杯(java)(日益更新)
- 前言:
- [一、c++ 到 java 须要注意的地方:](#一、c++ 到 java 须要注意的地方:)
- 二、多练java代码:(用java代码写算法模版)
-
- [1. acwing1277 分巧克力:](#1. acwing1277 分巧克力:)
- [2. acwing5407 管道:](#2. acwing5407 管道:)
- [3. acwing562 壁画](#3. acwing562 壁画)
- [4. acwing1230 K倍区间](#4. acwing1230 K倍区间)
- [5. acwing4262 空调](#5. acwing4262 空调)
- [6. acwing5396 棋盘](#6. acwing5396 棋盘)
- 总结
前言:
将 c++ 代码 转换为 java 代码
提示:以下是本篇文章正文内容:
一、c++ 到 java 须要注意的地方:
- java中,如何实现像c++那样,typedef long long LL;
- java中,如何实现像c++那样,const int N = 1e5 + 10;
- java中,如何实现像c++那样,int n, m; int h[N], w[N];
- java中,如何实现像c++那样,scanf("%d%d", &n, &m);
- java中,如何实现像c++那样,#define x first #define y second typedef pair<int, int> PII;
- java中,如何实现像c++那样int l = max(1, L - t), r = min((LL)m, (LL)L + t);
- java中,如何实现像c++那样,sort(q, q+cnt)
- java中,如何实现像c++那样, bool check(int mid)
- java中,如何实现像c++那样,for(int i = 0; i < n; i ++) scanf("%d%d", &h[i], &w[i]);
- java中,如何实现像c++那样,for(int i = 0; i < n; i ++) scanf("%d%d", &w[i].x, &w[i].y);
二、多练java代码:(用java代码写算法模版)
1. acwing1277 分巧克力:
java
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 管道:
java
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 壁画
java
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倍区间
java
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 空调
java
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 棋盘
java
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();
}
}
总结
提示:这里对文章进行总结:
💕💕💕