# Test questions and detailed explanation of the 8th Lanqiao cup provincial competition in 2017 (Java undergraduate group A)

Posted by jrmckee on Fri, 11 Feb 2022 05:40:48 +0100

1. Results fill in the blanks (full score 5 points)
2. Results fill in the blanks (out of 11 points)
3. Results fill in the blanks (out of 13 points)
4. Results fill in the blanks (Full Score 17 points)
5. Fill in the blank with code (full score 7 points)
6. Fill in the blank with code (full score 9 points)
7. Programming (Full Score: 19 points)
8. Programming (Full Score: 21)
9. Programming (out of 23 points)
10. Program design (Full Score 25)

## Question 1: Maze

A maze amusement park on Planet X is built on a small hillside.
It is composed of 10x10 interconnected small rooms.

There is a big letter written on the floor of the room.
We assume that the player is standing facing uphill, then:
L means go to the room on the left,
R means go to the room on the right,
U means walking to the room in the uphill direction,
D is the room going downhill.

The residents of Planet X are a little lazy and don't want to think hard.
They prefer to play luck games. The same is true of this game!

At the beginning, the helicopter put 100 players into small rooms.
Players must move according to the letters on the ground.

The maze map is as follows:
------------
UDDLUULRUL
UURLLLRRRU
RRUURLDLRD
RUDDDDUUUU
URUDLLRRUU
DURLRLDLRL
ULLURLLRDU
RDLULLRDDD
UUDDUDUDLL
ULRDLUURRR
------------

Please calculate how many players will walk out of the maze in the end?
Not in circles.

Please submit this integer to indicate the number of players out of the maze. Do not fill in any redundant content.

If you don't understand the rules of the game, please refer to the illustration of a simplified 4x4 maze:
p1.png

```public class Main {

public static void main(String[] args) {
int ans=0;
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
if(ok(i,j))
ans++;

System.out.println(ans);
}

static char[][] ch = new char[][] {
"UDDLUULRUL".toCharArray(),
"UURLLLRRRU".toCharArray(),
"RRUURLDLRD".toCharArray(),
"RUDDDDUUUU".toCharArray(),
"URUDLLRRUU".toCharArray(),
"DURLRLDLRL".toCharArray(),
"ULLURLLRDU".toCharArray(),
"RDLULLRDDD".toCharArray(),
"UUDDUDUDLL".toCharArray(),
"ULRDLUURRR".toCharArray()};

static boolean ok(int x,int y) {
int t=25;
while(t-->0) {
if(x<0 || x>=10 || y<0 || y>=10)
return true;
if(ch[x][y]=='U')
x--;
else if(ch[x][y]=='D')
x++;
else if(ch[x][y]=='L')
y--;
else if(ch[x][y]=='R')
y++;
}
return false;
}

}```

## Question 2: 9 mathematical formula

Observe the following formula:

9213 x 85674 = 789314562

The multiplier and multiplicand on the left just use all numbers from 1 to 9, once each.
The product also happens to use all numbers from 1 to 9, and each one.

With the help of the powerful computing power of the computer, please find out how many 9 formulas meet the above requirements?

be careful:
1. The total number includes the example given in the title.
2. The multiplier and the multiplicand are treated as the same scheme after being exchanged

```import java.util.HashSet;

public class Main {
public static void main(String[] args) {
dfs(0);
System.out.println(ans);
}

static int[] a = new int[] {1,2,3,4,5,6,7,8,9};
static int ans=0;
static HashSet<String> set = new HashSet<>();

static void dfs(int m) {
if(m>=9) {
for(int i=1;i<8;i++) {
int x=0,y=0;
for(int j=0;j<i;j++)
x = 10*x + a[j];
for(int j=i;j<9;j++)
y = 10*y + a[j];
String s = ""+x*y;
//				System.out.println(s);
if(ok(s)) {
String v = x+"x"+y;
String u = y+"x"+x;
if(!set.contains(v)&&!set.contains(u)) {
ans++;
}
}
}

return;
}

for(int i=m;i<9;i++) {
swap(i,m);
dfs(m+1);
swap(i,m);
}

}

static void swap(int i,int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}

static boolean ok(String s) {
if(s.length()!=9 || s.indexOf("0")>=0)
return false;
int[] b = new int[10];
for(int i=0;i<s.length();i++)
if(b[s.charAt(i)-'0']==1)
return false;
else
b[s.charAt(i)-'0']=1;
return true;
}

}```

## Question 3: cube state

The second-order Rubik's cube is a Rubik's cube with only two layers, which is only composed of eight small pieces.
As shown in Figure P1 PNG.

Xiao Ming is very naughty. He only likes three colors. All the second-order Rubik's cube at home are painted again, as follows:

Front: Orange
Right: Green
Top: yellow
Left: Green
Below: Orange
Back: yellow

Please calculate how many different states there are after such a magic cube is disturbed.

If the two states have the same color on all sides after the overall rotation of the magic cube, it is considered to be the same state.

Please submit an integer representing the number of States and do not fill in any superfluous content or explanatory text

```import java.util.HashSet;
import java.util.Set;

/*
Title: Rubik's cube state
The second-order Rubik's cube is a Rubik's cube with only two layers, which is only composed of eight small pieces.
As shown in Figure P1 PNG.
Xiao Ming is very naughty. He only likes three colors. All the second-order Rubik's cube at home are painted again, as follows:
Front: Orange
Right: Green
Top: yellow
Left: Green
Below: Orange
Back: yellow
Please calculate how many different states there are after such a magic cube is disturbed.
If the two states have the same color on all sides after the overall rotation of the magic cube, it is considered to be the same state.
Please submit an integer representing the number of States and do not fill in any redundant content or explanatory text.
*/
public class Main {

static char[][] start = {"oybbgb".toCharArray(),
"oygbbb".toCharArray(),
"bygbby".toCharArray(),
"bybbgy".toCharArray(),
"obbogb".toCharArray(),
"obgobb".toCharArray(),
"bbgoby".toCharArray(),
"bbbogy".toCharArray()};
static char[][][] q = new char[2000000][8][6];
static Set<String> all_state = new HashSet<String>();
static int  front, tail;

static String to_string(char[][] a) {
String ans = "";
for (int i = 0; i < 8; ++i) {
ans += new String(a[i]);
}
return ans;
}

private static void swap(char[] a, int i, int j) {
char t = a[i];
a[i] = a[j];
a[j] = t;
}

private static void swap(char[][] a, int i, int j) {
char[] t = a[i];
a[i] = a[j];
a[j] = t;
}

//The rotation of the upper block and the relative position of the surface are exchanged
static void ucell(char[] a) {
swap(a, 0, 2);
swap(a, 2, 5);
swap(a, 5, 4);
}

//The upper layer rotates clockwise
static void u(char[][] s) {
ucell(s[0]);
ucell(s[1]);
ucell(s[2]);
ucell(s[3]);
//    Relative position exchange of blocks
swap(s, 1, 0);
swap(s, 2, 1);
swap(s, 3, 2);

}

//The right layer rotation is the change of the position of the face
static void rcell(char[] a) {
swap(a, 1, 0);
swap(a, 0, 3);
swap(a, 3, 5);
}

static void r(char[][] s)//The right layer of the cube turns clockwise
{
rcell(s[1]);
rcell(s[2]);
rcell(s[6]);
rcell(s[5]);
//    Block position change
swap(s, 2, 1);
swap(s, 5, 1);
swap(s, 6, 5);
}

static void fcell(char[] a) {
swap(a, 2, 1);
swap(a, 1, 4);
swap(a, 4, 3);
}

static void f(char[][] s)//The front floor turns clockwise
{
fcell(s[0]);
fcell(s[1]);
fcell(s[4]);
fcell(s[5]);
swap(s, 1, 5);
swap(s, 0, 1);
swap(s, 4, 0);
}

static void uwhole(char[][] s)//The whole cube turns clockwise from the top to judge the weight
{
u(s);//Upper rotation
//    Lower rotation
ucell(s[4]);
ucell(s[5]);
ucell(s[6]);
ucell(s[7]);
//    After the spin is completed, the position of the block changes
swap(s, 5, 4);
swap(s, 6, 5);
swap(s, 7, 6);
}

static void fwhole(char[][] s)//The whole cube turns clockwise from the front to judge the weight
{
f(s);
fcell(s[2]);
fcell(s[6]);
fcell(s[7]);
fcell(s[3]);
swap(s, 2, 6);
swap(s, 3, 2);
swap(s, 7, 3);
}

static void rwhole(char[][] s)//The whole cube turns clockwise from the right to judge the weight
{
r(s);
rcell(s[0]);
rcell(s[3]);
rcell(s[4]);
rcell(s[7]);
swap(s, 3, 7);
swap(s, 0, 3);
swap(s, 4, 0);
}

static boolean try_insert(char[][] s) {
char[][] k = new char[8][6];
memcpy(k, s);
for (int i = 0; i < 4; i++) {
fwhole(k);
for (int j = 0; j < 4; j++) {
uwhole(k);
for (int q = 0; q < 4; q++) {
rwhole(k);
if (all_state.contains(to_string(k))) {
return false;
}
}
}
}
return true;

}

private static void memcpy(char[][] k, char[][] s) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 6; j++) {
k[i][j] = s[i][j];
}
}
}

static void solve() {
front = 0;
tail = 1;
memcpy(q[front], start);//Filling in q[0] is equivalent to the first state entering the queue
while (front < tail) {
/*Try to add all its deformations to the set*/
memcpy(q[tail], q[front]);//Copy to tail
u(q[tail]);//The upper layer rotates clockwise
if (try_insert(q[tail])) {
tail++;//Extended queue
}
memcpy(q[tail], q[front]);//Copy to tail
r(q[tail]);//The right layer rotates clockwise
if (try_insert(q[tail])) {
tail++;//Extended queue
}
memcpy(q[tail], q[front]);//Copy to tail
f(q[tail]);//Front clockwise rotation
if (try_insert(q[tail])) {
tail++;//Extended queue
}
//        cout << front << " " << tail << endl;
}

System.out.println(front);
}

public static void main(String[] args) {
solve();
}
}```

## Question 4: grid segmentation

6x6 square, cut into two parts along the edge of the grid.
The shape of these two parts is required to be exactly the same.

As shown in the figure: P1 png, p2. png, p3. PNG is a feasible segmentation method.

Trial calculation:
How many different segmentation methods are there, including these three segmentation methods.
Attention: it belongs to the same method of rotation.

Please submit this integer and do not fill in any redundant content or explanatory text.

```public class Main {

public static void main(String[] args) {
dfs(0);
System.out.println(ans);
}

static int[] a = new int[18];// {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35};
static boolean[][] vis = new boolean[6][6];
static int res=0,ans=0;
static int[] dx = new int[] {1,0,-1,0};
static int[] dy = new int[] {0,1,0,-1};

static void dfs(int m) {

for(a[0]=0;a[0]<36;a[0]++)
for(a[1]=a[0]+1;a[1]<36;a[1]++)
for(a[2]=a[1]+1;a[2]<36;a[2]++)
for(a[3]=a[2]+1;a[3]<36;a[3]++)
for(a[4]=a[3]+1;a[4]<36;a[4]++)
for(a[5]=a[4]+1;a[5]<36;a[5]++)
for(a[6]=a[5]+1;a[6]<36;a[6]++)
for(a[7]=a[6]+1;a[7]<36;a[7]++)
for(a[8]=a[7]+1;a[8]<36;a[8]++)
for(a[9]=a[8]+1;a[9]<36;a[9]++)
for(a[10]=a[9]+1;a[10]<36;a[10]++)
for(a[11]=a[10]+1;a[11]<36;a[11]++)
for(a[12]=a[11]+1;a[12]<36;a[12]++)
for(a[13]=a[12]+1;a[13]<36;a[13]++)
for(a[14]=a[13]+1;a[14]<36;a[14]++)
for(a[15]=a[14]+1;a[15]<36;a[15]++)
for(a[16]=a[15]+1;a[16]<36;a[16]++)
for(a[17]=a[16]+1;a[17]<36;a[17]++)
{
for(int i=0;i<6;i++)
for(int j=0;j<6;j++)
vis[i][j] = false;
for(int i=0;i<18;i++)
vis[a[i]/6][a[i]%6] = true;
ok();

}

}

static void ok() {
if(vis[0][0]==false)
return;

for(int i=0;i<6;i++)
for(int j=0;j<6;j++)
if(vis[i][j]==vis[5-i][5-j])
return;

res=0;
for(int i=0;i<6;i++)
for(int j=0;j<6;j++) {
if(vis[i][j]) {
dfs(i,j);
if(res==18) {
ans++;
//						System.out.println(ans);
}
return;
}
}
}

static void dfs(int x,int y) {
res++;
vis[x][y] = false;
for(int i=0;i<4;i++) {
int u = x + dx[i];
int v = y + dy[i];
if(u>=0 && u<6 && v>=0 && v<6 && vis[u][v])
dfs(u,v);
}

}

}```

## Question 5: alphabetic string

A, B and C can form many strings.
For example: "A","AB","ABC","ABA","AACBB"

Now, Xiao Ming is thinking about a question:
If the number of each letter is limited, how many strings of known length can be formed?

He asked a good friend to help and soon got the code,
The solution is super simple, but the most important part is vague.

Please carefully analyze the source code and fill in the missing content in the underlined part

```public class A
{
// How many strings with different length n can be formed by a letter A, b letter B and C letter C.
static int f(int a, int b, int c, int n)
{
if(a<0 || b<0 || c<0) return 0;
if(n==0) return 1;

return ________________________________;  //Fill in the blanks
}

public static void main(String[] args)
{
System.out.println(f(1,1,1,2));
System.out.println(f(1,2,3,3));
}
}```

For the above test data, Xiaoming's oral calculation result should be:
6
19

Note: only fill in the missing code in the underlined part, and do not submit any redundant content or explanatory text.

`answer:f(a-1,b,c,n-1)+f(a,b-1,c,n-1)+f(a,b,c-1,n-1)`

## Question 6: maximum common substring

The maximum common substring length is:
Find the maximum length that can be matched in all substrings of two strings.

For example: "abcdkkk" and "baabcddabc",
The longest common substring that can be found is "abcd", so the maximum common substring length is 4.

The following program is solved by matrix method, which is a more effective solution for the case of small string size.

Please analyze the idea of this solution and complete the missing code in the underlined part

```public class Main
{
static int f(String s1, String s2)
{
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();

int[][] a = new int[c1.length+1][c2.length+1];

int max = 0;
for(int i=1; i<a.length; i++){
for(int j=1; j<a[i].length; j++){
if(c1[i-1]==c2[j-1]) {
a[i][j] = __________________;  //Fill in the blanks
if(a[i][j] > max) max = a[i][j];
}
}
}

return max;
}

public static void main(String[] args){
System.out.println(n);
}
}```
`answer:1+a[i-1][j-1]`

## Question 7: regularity problem

Consider a simple regular expression:
A regular expression consisting only of x () |.
Xiao Ming wants to find the length of the longest string that this regular expression can accept.

For example ((xx|xxx) x| (x|xx)) the longest string that XX can accept is xxxxxx, with a length of 6.

input
----
A regular expression consisting of x() |. The input length shall not exceed 100 to ensure legality.

output
----
The length of the longest string that this regular expression can accept.

For example,
Input:
((xx|xxx)x|(x|xx))xx

The program output should:
6

Resource agreement:
Peak memory consumption (including virtual machine) < 256M
CPU consumption < 1000ms

Please output in strict accordance with the requirements, and do not print like "please input..." Superfluous content.

All code is placed in the same source file. After debugging, the copy is submitted to the source code.
Do not use package statements. Do not use jdk1 7 and above.
The name of the Main class must be: Main, otherwise it will be treated as an invalid code

```import java.util.Scanner;

public class Main {
//((xx|xxx)x|(x|xx))xx 6
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
s = in.next();
n = s.length();
System.out.println(f());
}

static String s;
static int p=0;//Record pointer
static int n=0;//Record length

static int f() {
int ans=0,temp=0;
while(p<n) {
if(s.charAt(p)=='(') {
p++;
temp+=f();
}
else if(s.charAt(p)=='x') {
p++;
temp++;
}
else if(s.charAt(p)=='|') {
p++;
ans = Math.max(ans, temp);
temp=0;//After clearing, the above ans has saved the value of temp. If there is 3|4|5, it is equivalent to that 3|4 is calculated in ANS
}
else if(s.charAt(p)==')') {
p++;
return Math.max(ans, temp);
}
}
return Math.max(ans, temp);
}

}```

## Question 8: how many steamed stuffed buns

Xiao Ming eats breakfast at a steamed stuffed bun shop almost every morning. He found that this steamed stuffed bun shop has N kinds of steamers, of which the i steamer can just put Ai steamed stuffed buns. Each kind of steamer has many steamers, which can be regarded as infinite steamers.

Whenever a customer wants to buy X steamed stuffed buns, the uncle selling steamed stuffed buns will quickly select several cages of steamed stuffed buns, so that there are exactly X steamed stuffed buns in these cages. For example, there are three kinds of steamers, which can put 3, 4 and 5 steamed buns respectively. When customers want to buy 11 steamed stuffed buns, uncle will choose 2 cages of 3 and 1 cage of 5 (or 1 cage of 3 and 2 cages of 4).

Of course, sometimes uncle baozi can't figure out the quantity customers want to buy anyway. For example, there are three kinds of steamers, which can put 4, 5 and 6 steamed buns respectively. When customers want to buy seven steamed stuffed buns, uncle can't come up with it.

Xiao Ming wants to know how many kinds of numbers are there that uncle baozi can't figure out.

input
----
The first line contains an integer n. (1 <= N <= 100)
Each of the following N lines contains an integer AI. (1 <= Ai <= 100)

output
----
An integer represents the answer. If there are infinite numbers that cannot be summed up, output INF.

For example,
Input:
2
4
5

The program output should:
6

Another example is,
Input:
2
4
6

The program output should:
INF

Example explanation:
For example 1, the numbers that cannot be rounded up include: 1, 2, 3, 6, 7 and 11.
For example 2, none of the odd numbers can be summed up, so there are infinite numbers.

Resource agreement:
Peak memory consumption (including virtual machine) < 256M
CPU consumption < 1000ms

Please output in strict accordance with the requirements, and do not print like "please input..." Superfluous content.

All code is placed in the same source file. After debugging, the copy is submitted to the source code.
Do not use package statements. Do not use jdk1 7 and above.
The name of the Main class must be: Main, otherwise it will be treated as an invalid code.
When submitting programs, pay attention to selecting the desired language type and compiler type

```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+5];
int[] dp = new int[10005];//When there are only two numbers, the maximum number that cannot be combined x*y-x-y has this formula
int ans=0;
dp[0]=1;
for(int i=1;i<=n;i++)
a[i] = in.nextInt();

int g = a[1];
for(int i=2;i<=n;i++)
g = gcd(g,a[i]);

if(g!=1) {
System.out.println("INF");
return;
}

for(int i=1;i<=n;i++)
for(int j=a[i];j<=10000;j++)
dp[j] = Math.max(dp[j], dp[j-a[i]]);

for(int j=1;j<=10000;j++)
if(dp[j]==0)
ans++;

System.out.println(ans);
}

static int gcd(int a,int b) {
return b==0?a:gcd(b,a%b);
}

}```

## Question 9: Chocolate

On children's day, K children visited Xiao Ming's house. Xiao Ming took out his precious chocolate to entertain the children.
Xiaoming has a total of N chocolates, of which the i is a rectangle composed of Hi x Wi squares.

To be fair, Xiao Ming needs to cut K chocolates out of the N chocolates and give them to the children. The cut chocolate needs to meet:

1. The shape is a square and the side length is an integer
2. Same size

For example, a piece of 6x5 chocolate can cut 6 pieces of 2x2 chocolate or 2 pieces of 3x3 chocolate.

Of course, children all want to get as much chocolate as possible. Can you help little Hi calculate the maximum side length?

input
The first line contains two integers n and K. (1 <= N, K <= 100000)
Each of the following N lines contains two integers hi and wi. (1 <= Hi, Wi <= 100000)
Input to ensure that each child can get at least one 1x1 chocolate.

output
Output the maximum possible side length of the cut square chocolate.

Sample input:
2 10
6 5
5 6

Sample output:
2

Resource agreement:
Peak memory consumption (including virtual machine) < 256M
CPU consumption < 1000ms

Please output in strict accordance with the requirements, and do not print like "please input..." Superfluous content.

All code is placed in the same source file. After debugging, the copy is submitted to the source code.
Do not use package statements. Do not use jdk1 7 and above.
The name of the Main class must be: Main, otherwise it will be treated as an invalid code

```import java.util.Scanner;

public class Main {
public static void main(String[] args) {
int n, k;
int[] h = new int[100000];
int[] w = new int[100000];
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
k = sc.nextInt();
for (int i = 0; i < n; ++i) {
h[i] = sc.nextInt();
w[i] = sc.nextInt();
}

int r = 100001;
int l = 1;
int ans = 0;
while (l <= r) {
int mid = (l + r) / 2;

int cnt = 0;
//Each chocolate block is cut according to len
for (int i = 0; i < n; ++i) {
cnt += (h[i] / mid) * (w[i] / mid);
}

if (cnt >= k) {
l = mid + 1;
ans = mid;
} else {
r = mid - 1;
}
}
System.out.println(ans);
}
}```

## Question 10: paint area

A group of archaeological robots on Planet X are archaeological on a piece of ruins.
The ground in this area is as hard as stone and as flat as a mirror.
For the convenience of managers, a standard rectangular coordinate system has been established.

Each robot has its own specialty and unique skills. They are also interested in different contents.
After various measurements, each robot will report one or more rectangular areas as priority archaeological areas.

The representation format of the rectangle is (x1,y1,x2,y2), which represents the coordinates of the two diagonal points of the rectangle.

In order to be eye-catching, the headquarters requires that the rectangular areas selected by all robots be painted with yellow paint.
Xiao Ming doesn't need to be a painter, but he needs to calculate how much paint it will cost.

In fact, it's not difficult. Just calculate the total area covered by all rectangles.
Note that the rectangles may overlap.

The input of this question is several rectangles. It is required to output the total area covered by them.

Input format:
The first line, an integer n, indicates how many rectangles there are (1 < = n < 10000)
In the next n lines, each line has four integers x1 y1 x2 y2, separated by spaces, representing the coordinates of the two diagonal vertices of the rectangle.
(0<= x1,y1,x2,y2 <=10000)

Output format:
An integer representing the total area of a rectangle.

For example,
Input:
3
1 5 10 10
3 1 20 20
2 7 15 17

The program output should:
340

Another example is,
Input:
3
5 2 10 6
2 7 12 10
8 1 15 15

The program output should:
128

Resource agreement:
Peak memory consumption (including virtual machine) < 256M
CPU consumption < 2000ms

Please output in strict accordance with the requirements, and do not print like "please input..." Superfluous content.

All code is placed in the same source file. After debugging, the copy is submitted to the source code.
Do not use package statements. Do not use jdk1 7 and above.
The name of the Main class must be: Main, otherwise it will be treated as an invalid code.

```import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
vis = new boolean[10005][10005];
//		for(int i=0;i<10005;i++)
//			for(int j=0;j<10005;j++)
//				vis[i][j] = false;
while(n-->0) {
int x1 = in.nextInt();
int y1 = in.nextInt();
int x2 = in.nextInt();
int y2 = in.nextInt();
//			if(x1>x2 && y1>y2)
//				paint(x2,y2,x1,y1);
//			else
paint(x1,y1,x2,y2);
}
int ans=0;
for(int i=0;i<10005;i++)
for(int j=0;j<10005;j++)
if(vis[i][j])
ans++;
System.out.println(ans);
}

static boolean[][] vis;

static void paint(int x1,int y1,int x2,int y2) {
for(int i=x1;i<x2;i++)
for(int j=y1;j<y2;j++)
vis[i][j] = true;
}

}```

Topics: Java