## 计蒜客2019蓝桥杯省赛B组模拟赛（一）题目及解析

### A. 结果填空：钟表 题库链接

class Main {
public static void main(String[] args) {
System.out.println("07:55:41");
}
}

### B. 结果填空：青蛙爬井 题库链接

import java.util.Scanner;

public class 青蛙爬井 {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int up = in.nextInt();
int down = in.nextInt();
int s=0;
int ans = 0;
while(true) {
ans++;
s+=up;
System.out.println("h:"+s);
if(s>=n)
break;
s-=down;
}
System.out.println(ans);
}

}

class Main {
public static void main(String[] args) {
System.out.println("863");
}
}

### C. 结果填空：倍数 题库链接

class Main {
public static void main(String[] args) {
System.out.println("52573230519");
}
}
import java.math.BigInteger;
import java.util.Scanner;

public class 倍数 {
//1032 12302135942453 234
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigInteger n = BigInteger.valueOf(1032);
BigInteger d = BigInteger.valueOf(234);
BigInteger t = new BigInteger("12302135942453");
BigInteger ans = BigInteger.ZERO;
//		while(n.compareTo(t)<=0) {
//			if(n.mod(d).equals(BigInteger.ZERO)) {
////				System.out.println(n+" "+d+" "+ans+"  "+n.mod(d));
//			}
//		}
System.out.println(t.divide(d));
System.out.println(n.divide(d));
}

}

### D. 结果填空：马的管辖 题库链接

#include <cstdio>
#include <cstring>
using namespace std;

int px[4] = {0, 0, 1, -1};
int py[4] = {1, -1, 0, 0};
int dx[4][2] = {-1, 1, -1, 1, 2, 2, -2, -2};
int dy[4][2] = {2, 2, -2, -2, -1, 1, -1, 1};
int vis[5][5], g[5][5];
int n = 5, m = 5;
bool in(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
int main() {
int r = n * m;
int minx = r + r, ans = 0;
for (int s = 0; s < 1 << r; s++) {
memset(vis, 0, sizeof vis);
int cnt = 0;
for (int i = 0; i < r; i++) {
if (s >> i & 1) {
g[i / m][i % m] = 1;
cnt++;
} else {
g[i / m][i % m] = 0;
}
}
if (cnt > minx) continue;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (g[i][j] == 0) continue;
vis[i][j] = 1;
for (int k = 0; k < 4; k++) {
int x = i + px[k], y = j + py[k];
if (in(x, y) && g[x][y] == 0) {
for (int u = 0; u < 2; u++) {
int tx = i + dx[k][u];
int ty = j + dy[k][u];
if (in(tx, ty)) {
vis[tx][ty] = 1;
}
}
}
}
}
}
int ok = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (vis[i][j] == 0) {
ok = 0;
break;
}
}
}
if (ok) {
if (cnt < minx) {
minx = cnt;
ans = 1;
} else if (cnt == minx) {
ans++;
}
}
}
printf("%d %d\n", minx, ans);
return 0;
}

### E. 代码填空：LIS 题库链接

import java.util.*;
import java.math.*;

public class Main {
public static final int N = (int)1e5 + 9;
public static int n = 0;
public static int[] f = new int[N];
public static int[] a = new int[N];

public static int find(int l, int r, int x) {
while (l < r) {
int mid = (l + r) / 2;
if (f[mid] < x) {
l = mid + 1;
} else {
r = mid;
}
}
return l;
}

public static int lis() {
int len = 0;
for (int i = 0; i < n; i++) {
/*在这里填写必要的代码*/
int k = find(0, len, a[i]);
f[k] = a[i];
if (k == len) {
len++;
}
}
return len;
}

public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
n = cin.nextInt();
for (int i = 0; i < n; i++) {
a[i] = cin.nextInt();
}
System.out.println(lis());
}
}

### 数据范围

3
4
8
20

2 2
3 5
3 17

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
boolean[] is = new boolean[1000000+5];
is[1] = true;
for(int i=2;i<=1000000;i++)
if(!is[i])
for(int j=2*i;j<=1000000;j+=i)
is[j] = true;
ArrayList<Integer> list = new ArrayList<>();
for(int i=2;i<=1000000;i++)
if(!is[i])

int n = list.size();
//		System.out.println(n);
while(t-->0) {
int x = in.nextInt();
for(int i=0;i<n;i++) {
boolean flag = false;
for(int j=i;j<n;j++) {
if(list.get(i)+list.get(j)==x) {
flag = true;
System.out.println(list.get(i)+" "+list.get(j));
break;
}
if(list.get(i)+list.get(j)>x)
break;
}
if(flag)
break;
}
}

}

}
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 100005;
bool p[1000005];
int main() {
int _;
scanf("%d", &_);
for (int i = 2; i <= 1000000; i++) {
p[i] = true;
}
for (int i = 1; i * i <= 1000000; i++) {
if (p[i]) {
for (int j = i * i; j <= 1000000; j += i) {
p[j] = false;
}
}
}
while (_--) {
int n;
scanf("%d", &n);
for (int i = 2;; i++) {
if (p[i] && p[n - i]) {
printf("%d %d\n", i, n - i);
break;
}
}
}
return 0;
}

### 数据范围

3
ba
a
aba

2
3
1

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String[] s = new String[n];
for(int i=0;i<n;i++)
s[i] = reverse(in.next());
for(int i=0;i<n;i++) {
int ans = 0;
for(int j=0;j<n;j++)
if(s[j].indexOf(s[i])==0)
ans++;
System.out.println(ans);
}
}

static String reverse(String x) {
char[] ch = new char[x.length()];
for(int i=0;i<x.length();i++)
ch[i] = x.charAt(x.length()-1-i);
return new String(ch);
}

}

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 100005;
string a[N];
int main() {
map<string, int> mp;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
for (int j = 0; j < a[i].size(); j++) {
mp[a[i].substr(j)]++;
}
}
for (int i = 0; i < n; i++) {
cout << mp[a[i]] << endl;
}
return 0;
}

### H. 程序设计：轻重搭配 题库链接

hhha，来个反面教材，应该从n/2开始匹配，因为他们最多可以匹配n/2对，所以不要从n/2之前就配对了，这样浪费了，也不要最轻和最重匹配，这样也会造成浪费

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

public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++)
a[i] = in.nextInt();
Arrays.sort(a);
int l=0,r=n-1;
int ans = 0;
while(l<=r) {
if(a[r]>=2*a[l]) {
l++;
r--;
ans++;
}
else {
l++;
ans++;
}
}
System.out.println(ans);
}

}

c++

#include <algorithm>
#include <cstdio>
using namespace std;
int a[500005];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a, a + n);
int ans = n, pos = n / 2;
for (int i = 0; i < n / 2; i++) {
while (pos < n && a[pos] < a[i] * 2) pos++;
if (pos == n) break;
ans--;
pos++;
}
printf("%d\n", ans);
return 0;
}

java

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

public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++)
a[i] = in.nextInt();
Arrays.sort(a);
int l=0,r=n/2,ans=0;
while(l<n/2 && r<n) {
if(a[r]>=2*a[l]) {
l++;
r++;
ans++;
} else
r++;
}
System.out.println(n-ans);
}

}

### 样例解释

3
4 5
1	0	0	0	1
1	0	1	0	1
1	0	1	0	1
1	0	0	0	1
5 6
1	1	1	1	1	1
1	0	1	0	1	1
1	0	1	0	1	1
1	0	0	0	1	1
1	1	1	1	1	1
10 10
1	1	1	1	1	1	1	1	1	1
1	0	0	0	0	0	1	0	1	0
1	0	0	0	0	0	1	0	1	0
1	0	0	1	0	0	1	0	0	0
1	0	0	0	0	0	1	1	1	1
1	0	0	0	0	0	1	1	1	1
1	1	1	1	1	1	1	1	1	1
1	1	1	0	0	0	1	0	0	0
1	1	1	0	1	0	1	0	1	0
1	1	1	0	0	0	1	0	0	0

0 0 0 0 0
0 0 1 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0

一模一样的java代码会超时呵呵，还有就是末尾空格会爆0

#include<iostream>
#include<cstdio>
using namespace std;

int a[505][505];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int n,m,t;

void dfs(int x,int y) {
a[x][y] = 0;
for(int i=0;i<4;i++)
if(x+dx[i]>=0 && x+dx[i]<n && y+dy[i]>=0 && y+dy[i]<m && a[x+dx[i]][y+dy[i]]!=0)
dfs(x+dx[i],y+dy[i]);
}

int main() {
cin>>t;
while(t-->0) {
cin>>n>>m;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
scanf("%d",&a[i][j]);
for(int i=0;i<n;i++) {
if(a[i][0]!=0)
dfs(i,0);
if(a[i][m-1]!=0)
dfs(i,m-1);
}
for(int i=0;i<m;i++) {
if(a[0][i]!=0)
dfs(0,i);
if(a[n-1][i]!=0)
dfs(n-1,i);
}
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++)
printf("%d%c", a[i][j], " \n"[j == m]);

//				cout<<a[i][j]<<" ";
//			cout<<endl;
}
}
return 0;
}

### 输出格式

10个样例过8个样例的代码，把它看成最大字段和，然后稍微优化一下，这里出现了环，我们通常的做法是，构造2*n的数组把环拆了

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[2*n+5];
for(int i=0;i<n;i++) {
a[i] = in.nextInt();
a[n+i] = a[i];
}
long len =0,s=0,max=0;
for(int i=0;i<2*n;i++) {
s+=a[i];
len++;
if(s<0) {
s=0;
len=0;
}
if(len>n) {
s-=a[i-n];
len--;
}
max = Math.max(max,s);
}
System.out.println(max);

}

}

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 200005;
int a[N];
LL pre[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
a[n + i] = a[i];
}
for (int i = 1; i <= 2 * n; i++) {
pre[i] = pre[i - 1] + a[i];
}
deque<int> q;
q.push_back(0);
LL ans = a[1];
for (int i = 1; i <= 2 * n; i++) {
if (!q.empty() && q.front() < i - n) {
q.pop_back();
}
ans = max(ans, pre[i] - pre[q.front()]);
while (!q.empty() && pre[q.back()] >= pre[i]) {
q.pop_back();
}
q.push_back(i);
}
printf("%lld\n", ans);
return 0;
}

0条评论