2018年第九届蓝桥杯省赛 Java A 组真题
发布时间
阅读量:
阅读量
2018年第九届蓝桥杯省赛 Java A 组真题
第一题

import java.util.Calendar;
public class Q1 {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2000);
// 月份从 0 开始
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
int cnt = 1;
while (cal.get(Calendar.MONTH) != 4 || cal.get(Calendar.DAY_OF_MONTH) != 4) {
cal.add(Calendar.DAY_OF_MONTH, 1);
cnt++;
}
System.out.println(cnt);
}
}
答案:125
第二题


public class Q2 {
static int r = 1000;
public static void main(String[] args) {
int ans = 0;
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= r; j++) {
if (i * i + j * j <= r * r)
ans++;
}
}
System.out.println(ans * 4);
}
}
答案:3137548
第三题

import java.math.BigInteger;
public class Q3 {
public static void main(String[] args) {
BigInteger[] ans = pow(new BigInteger("2"), new BigInteger("3"), 123456);
System.out.println(ans[0] + " " + ans[1] + "i");
}
static BigInteger[] pow(BigInteger x, BigInteger y, int n) {
if (n == 0) return new BigInteger[] {new BigInteger("1"), new BigInteger("0")};
if (n == 1) return new BigInteger[] {x, y};
if (n % 2 != 0) {
BigInteger[] t = pow(x, y, n - 1);
return new BigInteger[] {x.multiply(t[0]).subtract(y.multiply(t[1])), x.multiply(t[1]).add(y.multiply(t[0]))};
} else {
BigInteger[] t = pow(x, y, n / 2);
return new BigInteger[] {t[0].multiply(t[0]).subtract(t[1].multiply(t[1])), t[0].multiply(t[1]).multiply(new BigInteger("2"))};
}
}
}
答案:太长了
第四题

import java.util.Arrays;
public class Q4 {
static int n = 1000;
static int k = 3;
// dp[i][j] : i 部手机,j 层楼 最坏情况需要试几次
static int[][] dp = new int[k + 1][n + 1];
public static void main(String[] args) {
for (int[] ints : dp)
Arrays.fill(ints, n + 1);
// base case :
for (int i = 1; i <= n; i++) {
dp[1][i] = i; // 只有一部手机,有几层就得试几层
}
for (int i = 0; i <= k; i++) {
dp[i][0] = 0; //没有楼层肯定是第0层
}
for (int i = 1; i <= k; i++) { // i 部手机
for (int j = 1; j <= n; j++) { // j 层楼
for (int l = 1; l <= j; l++) { // 在 l 层仍
dp[i][j] = Math.min(dp[i][j], Math.max(dp[i - 1][l - 1], dp[i][j - l]) + 1);
}
}
}
System.out.println(dp[3][1000]);
}
}
答案:19
第五题

import java.util.Random;
public class Q5{
public static int quickSelect(int a[], int l, int r, int k) {
Random rand = new Random();
int p = rand.nextInt(r - l + 1) + l;
int x = a[p];
int tmp = a[p]; a[p] = a[r]; a[r] = tmp;
int i = l, j = r;
while(i < j) {
while(i < j && a[i] < x) i++;
if(i < j) {
a[j] = a[i];
j--;
}
while(i < j && a[j] > x) j--;
if(i < j) {
a[i] = a[j];
i++;
}
}
a[i] = x;
p = i;
if(i - l + 1 == k) return a[i];
if(i - l + 1 < k) return quickSelect(a, i + 1, r, k - i + l - 1); //填空
else return quickSelect(a, l, i - 1, k);
}
public static void main(String args[]) {
int [] a = {1, 4, 2, 8, 5, 7};
System.out.println(quickSelect(a, 0, 5, 4));
}
}
第六题

import java.util.Arrays;
import java.util.Scanner;
public class Q6 {
static int MAX_N = 100005;
static int[] a = new int[MAX_N];
static int[] b = new int[MAX_N];
static int[] c = new int[MAX_N];
static int n;
static long ans = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int[][] p = {a, b, c};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < n; j++) {
p[i][j] = sc.nextInt();
}
Arrays.sort(p[i], 0, n);
}
for (int i = 0; i < n; i++) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = (left + right) >>> 1;
if (a[mid] >= b[i])
right = mid - 1;
else
left = mid + 1;
}
int x = right + 1;
left = 0;
right = n - 1;
while (left <= right) {
int mid = (left + right) >>> 1;
if (c[mid] <= b[i])
left = mid + 1;
else
right = mid - 1;
}
int y = n - left;
ans += x * y;
}
System.out.println(ans);
}
}
第七题

import java.util.Scanner;
public class Q7 {
static int x, y, xx, yy;
static long n, d;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
y = sc.nextInt();
n = Math.max(Math.abs(x), Math.abs(y));
// 走到 (-n, -n) 需要的步数 (1 + 2 + .. + n) * 2
d = 4 * n * (n + 1);
if (y == -n) {
d -= x + n;
} else if (x == n) {
d -= (n + n) + (y + n);
} else if (y == n) {
d -= (n + n) + (n + n) + (n - x);
} else {
d -= (n + n) + (n + n) + (n + n) + (n - y);
}
System.out.println(d);
}
}
第八题

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Q8 {
static int MAX_N = 100005;
static List<Integer>[] a = new ArrayList[MAX_N];
static int n, d, k, ts, id;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
d = sc.nextInt();
k = sc.nextInt();
for (int i = 0; i < n; i++) {
ts = sc.nextInt();
id = sc.nextInt();
if (a[id] == null)
a[id] = new ArrayList<>();
a[id].add(ts);
}
for (int i = 0; i <= 100000; i++) {
if (a[i] == null) continue;
Collections.sort(a[i]);
int left = 0, right = 0;
while (right < a[i].size()) {
while (left <= right && a[i].get(right) - a[i].get(left) >= d) {
left++;
}
if (right - left + 1 >= k) {
System.out.println(i);
break;
}
right++;
}
}
}
}
第九题

此处贴上 c++ AC 代码,不知道什么原因 Java 代码运行错误。>_<
#include <iostream>
using namespace std;
const int MAX_N = 1005;
char mp[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];
int delta[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int n, cnt1 = 0, cnt2 = 0;
bool dfs(int x, int y) {
vis[x][y] = true;
bool res = false;
if (mp[x + 1][y] == '#' && mp[x - 1][y] == '#' && mp[x][y + 1] == '#' && mp[x][y - 1] == '#')
res = true;
for (int i = 0; i < 4; i++) {
int xx = x + delta[i][0];
int yy = y + delta[i][1];
if (xx >= 1 && x < n - 1 && yy >= 1 && yy < n - 1 && mp[xx][yy] == '#' && !vis[xx][yy]) {
res |= dfs(xx, yy);
}
}
return res;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> mp[i][j];
}
}
for (int i = 1; i < n - 1; i++) {
for (int j = 1; j < n - 1; j++) {
if (mp[i][j] == '#' && !vis[i][j]) {
if (dfs(i, j))
cnt2++; // 能够存活的岛屿数量
cnt1++; // 岛屿数量
}
}
}
cout << cnt1 - cnt2 << endl;
return 0;
}
全部评论 (0)
还没有任何评论哟~
