# BUPT2014 Counselling Afternoon Computer Test of Java Questions

## Topic 1

#### Title Description

Given a non-descending column of length N, find the median of the column.
Median: When the item number N of a number column is odd, the variable value at the middle of f is the median. When N is even, the median is the average of two numbers in the middle.

#### Input Format

The first row of input data is an integer T (1 < T < 100), which represents the number of groups of test data.For each of three sets of test data:
The first row is a positive integer N (1 < N < 100) representing the length of the column.
The second row has N integers, limited by spaces, and no integers exceed 105, representing this column.

#### Output Format

For each set of test data, the median of the output number column.Do not output extra 0 at the end of the decimal point.

2
4
1 1 2 2
5
1 1 2 2 3

#### sample output

1.5
2

```import java.util.Scanner;

public class c1405 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int t=0;t<T;t++) {
int n = sc.nextInt();
double sum = 0;
for(int i = 0;i<n;i++) {
int a = sc.nextInt();
if(n%2==0&&(i==n/2||i==n/2-1)) {
sum+=a;
}
if(n%2==1&&i==n/2) {
sum+=a;
}
}
if(n%2==0) {
System.out.println(sum/2);
}else {
System.out.println(String.format("%.0f", sum));
}
}
sc.close();
}

}
```

## Question 2 Memory Allocation

#### Title Description

Memory allocation is a very important task in an operating system.
Known memory space consists of N memory blocks numbered from 1 to N.
When allocating memory, the operating system will select a process with enough memory to allocate all of the requested memory.For example, when a process requests 10MB of memory, the operating system must allocate a block of memory not less than 10MB to the process.Memory blocks cannot be allocated repeatedly.

The operating system has three basic assignments:
First fit: Search from No. 1 to No. N memory blocks in turn, and then find the first block that is large enough to be unallocated for a month and assign it to the drawing process.
Best fit: Find the smallest memory block that is not currently allocated and is large enough to allocate to a process.
Worst fit: Find the smallest of the currently unallocated and sufficiently sized inner blocks to allocate to the process.

Among them, the best way to adapt is the most widely used.The operating system now handles memory purchases for M processes in turn, allocating memory in an optimal way and outputting the size of the corresponding memory block.If there is not enough memory block of size to satisfy the pre-lan requirement, output "NULL" (without quotation marks) and skip the poem

#### Input Format

The first row of input data is the number of test data sets T(T < 20). Each set of data consists of four rows:
The first behavior is an integer N(1 < N < 100); indicates that there are N memory blocks
The second row has N integers, and the ith integer represents the size of the ith block of memory
The third behavior is an integer M(1 < M < 100), which indicates that there are M requests
The fourth line has M integers representing the memory space requested by the process.

#### Output Format

Each set of data outputs a row with M numbers per row, indicating the size of the memory block allocated to the process in turn by the operating system in the best-fit manner.
If no memory blocks are available, output "NULL" (without quotation marks)
Do not output extra trailing spaces.

2
4
7 5 10 3
2
4 6
4
3 5 9 10
3
5 12 6

#### sample output

5 7
5 NULL 9

```import java.util.Scanner;

public class c1406 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int t=0;t<T;t++) {
int n = sc.nextInt();
int[] arr = new int[n+1];
for(int i = 1;i<=n;i++) {
arr[i] = sc.nextInt();
}
int m = sc.nextInt();
for(int i = 1;i<=m;i++) {
int x = sc.nextInt();
int pos = -1;
int min = Integer.MAX_VALUE;
for(int j = 1;j <= n;j++) {
if(arr[j] >= x&&arr[j]<min) {
pos = j;
min = arr[j];
}
}
if(pos == -1) {
System.out.print("NULL ");
}else {
System.out.print(arr[pos]+" ");
arr[pos] -= min;
}
}
System.out.println();
}
sc.close();
}
}
```

## Question 3

#### Title Description

In image recognition, we often need to analyze some features in a specific image, and one of the important points is to identify multiple areas of the image.In this question, we will give an image of N x M in which each 1 x 1 point represents its RGB color with a value of [0, 255].If the difference in color between two adjacent pixel points does not exceed D., we assume that the two pixel points belong to one region of the bean.For a pixel point (x,y), the following eight points (if any) are adjacent to it: (x-1,y-1),(x-1,y),(x-1,y + 1),(x,y-1),(x, y + 1),(x, y + 1),(x + 1,y - 1),(x + 1,y),(x + 1,y) (x +1,y + 1).

Your task is to write a program that distinguishes how many areas are divided in a given image.

#### Input Format

The input data contains multiple sets of test data.

The first line of input is an integer T (T < 100), which represents the number of groups of test validity.

The first row of each set of test data is three integers, N,M,D(1 < N,M < 100, 0 < D < 255), whose meanings are described above.

Next N lines, M integers per line, represent the color of each pixel of a given image.

#### Output Format

Output a row for each set of test data, the number of areas in the image

2
3 3 0
1 1 1
0 1 0
0 1 0
3 4 1
10 11 12 13
9 8 7 6
2 3 4 5

#### sample output

3
1

```import java.util.Scanner;

public class c1407 {

static int n;
static int m;
static int d;
static int[][] color;
static int[][] parent;
static int[][] pos = {{-1,-1},{-1,0},{-1,1},{0,-1},
{0,1},{1,-1},{1,0},{1,1}};

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int t=0;t<T;t++) {
n = sc.nextInt();
m = sc.nextInt();
d = sc.nextInt();
color = new int[n][m];
parent = new int[n][m];
for(int i = 0;i<n;i++) {
for(int j = 0;j<m;j++) {
color[i][j] = sc.nextInt();
parent[i][j] = -1;
}
}
int ans = 0;

for(int i = 0;i<n;i++) {
for(int j = 0;j<m;j++) {
if(parent[i][j]==-1) {
cover(i,j,color[i][j]);
ans++;
}
}
}

System.out.println(ans);
}
sc.close();
}

static void cover(int p,int q,int c) {
parent[p][q] = c;
for(int i = 0;i<8;i++) {
int pp = p+pos[i];
int qq = q+pos[i];
if(pp>=0&&pp<n&&qq>=0&&qq<m) {
if(parent[pp][qq]==-1&&Math.abs(color[pp][qq]-color[p][q])<=d)
cover(pp,qq,c);
}
}
return ;
}
}
```

## Question 4  Input:

2
3
MOV AX,2
MOV BX,3
5
MOV AX,2
MOV BX,030H
MOV CX,11B

```import java.util.Scanner;

public class c1408 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int t=0;t<T;t++) {
int[] x = new int;
int[] l = new int;
int[] h = new int;
int n = sc.nextInt();
for(int i = 0;i<n;i++) {
String op = sc.next();
String s = sc.next();
String[] param = s.split(",");
int flag = -1;//0 for mov and 1 for add
if(op.equals("MOV")) {
flag = 0;
}else {
flag = 1;
}
//Processing parameter 1, last place to store
int abcd1 = param.charAt(0)-'A';
char xhl1 = param.charAt(1);
//Processing parameter 2
int num = 0;
if(param.charAt(0)>='0'&&param.charAt(0)<='9') {
num = transfer(param);
}else {
int abcd2 = param.charAt(0)-'A';
char xhl2 = param.charAt(1);
if(xhl2 == 'X') {
num = x[abcd2];
}else if(xhl2 == 'H') {
num = h[abcd2];
}else {
num = l[abcd2];
}
}
//Start computing
if(xhl1 == 'X') {
if(flag == 0) {
x[abcd1] = num;
}else {
x[abcd1] += num;
}
//Related registers will also change
String x2 = String.format("%08d", Integer.parseInt(Integer.toBinaryString(num)));
String h2 = x2.substring(0,4);
String l2 = x2.substring(4);
h[abcd1] = Integer.parseInt(h2,2);
l[abcd1] = Integer.parseInt(l2,2);
}else if(xhl1 == 'H') {
if(flag == 0) {
h[abcd1] = num;
}else {
h[abcd1] += num;
}
//Related registers will also change
String h2 = String.format("%04d", Integer.parseInt(Integer.toBinaryString(num)));
String l2 = String.format("%04d", Integer.parseInt(Integer.toBinaryString(l[abcd1])));
String x2 = h2+l2;
x[abcd1] = Integer.parseInt(x2,2);
}else {
if(flag == 0) {
l[abcd1] = num;
}else {
l[abcd1] += num;
}
//Related registers will also change
String h2 = String.format("%04d", Integer.parseInt(Integer.toBinaryString(h[abcd1])));
String l2 = String.format("%04d", Integer.parseInt(Integer.toBinaryString(num)));
String x2 = h2+l2;
x[abcd1] = Integer.parseInt(x2,2);
}
}
for(int j = 0;j<4;j++) {
System.out.print(x[j]+" ");
}
System.out.println();
}
sc.close();
}

static int transfer(String s) {
if(s.charAt(s.length()-1)=='B') {
String temp = s.substring(0,s.length()-1);
int ans = 0;
int k = 1;
for(int i = temp.length()-1;i>=0;i--) {
ans+=(s.charAt(i)-'0')*k;
k*=2;
}
return ans;

}else if(s.charAt(s.length()-1)=='H') {
String temp = s.substring(0,s.length()-1);
int ans = 0;
int k = 1;
for(int i = temp.length()-1;i>=0;i--) {
ans+=(s.charAt(i)-'0')*k;
k*=16;
}
return ans;
}else {
return Integer.parseInt(s);
}
}
}
```  14 original articles published. 0% praised. 92% visited

Tags: Java less

Posted on Fri, 13 Mar 2020 18:17:00 -0700 by rkeppert