NetEase 2018 Autumn Recruitment Written Test

Question one

Problem Description 
Xiaoyi has a sequence of integers of length n, a_1,...,a_n. Then consider doing n times the following operations on an empty sequence b: 
1. Put a_i at the end 
of the b sequence 2. Reverse the b sequence 
Xiaoyi requires you to calculate the b sequence after n times of output operations.

enter description

The input consists of two lines, the first line consists of an integer n (2 ≤ n ≤ 2*10^5), the length of the sequence. 
The second line includes n integers a_i (1 ≤ a_i ≤ 10^9), that is, each integer in the sequence a, separated by spaces.

output description 

Output the b sequence after n operations in one line, separated by spaces, with no spaces at the end of the line.

analyze:

input length n output sequence b
1 a0
2 a1 a0
3 a2 a0 a1
4 a3 a1 a0 a2
5 a4 a2 a0 a1 a3
6 a5 a3 a1 a0 a2 a4

It is easy to draw conclusions from the table above :

When n is odd, firstly output all even subscript items of a in reverse order, and then output all odd subscript items in positive order, when n is even, it is the opposite. Paste the java code below:

import java.util. *;
public class Overturn {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			int n = sc.nextInt();
			int[] array = new int[n];
			for(int i=0;i<n;i++)
				array[i]=sc.nextInt();
			if(n%2==0){
				for(int i = n-1;i>0;i-=2)
					System.out.print(array[i] + " ");
				for(int i = 0;i<n-2;i+=2)
					System.out.print(array[i] + " ");
				System.out.print(array[n-2]);
			}
			else{
				for(int i = n-1;i>=0;i-=2)
					System.out.print(array[i] + " ");
				for(int i = 1;i<n-2;i+=2)
					System.out.print(array[i] + " ");
				System.out.print(array[n-2]);
			}
		}
	}
}

Question two

Problem Description 

Xiaoyi now has f fruits and d yuan in his hands. He needs to consume x yuan in rent and 1 fruit a day. The store sells one fruit for p yuan. How many days can Xiaoyi last?

import java.util.Scanner;

public class Living {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			int f = sc.nextInt();
			int d = sc.nextInt();
			int x = sc.nextInt();
			int p = sc.nextInt();
			if(d/x<=f)
				System.out.println(d/x);
			else
				System.out.println((d-f*x)/(x+p)+f);
		}
	}
}

Question three

The problem describes 
the crazy queue. Given an array, the order can be arranged arbitrarily, and they are required to obtain the maximum difference between each pair. Find this crazy value, that is, the difference.

Input description 
The input consists of two lines. The first line contains an integer n (1 ≤ n ≤ 50), which represents the number of students. The 
second line contains n integers h[i] (1 ≤ h[i] ≤ 1000), which represents each student. height

The output description is 
as shown in the example: 5 10 25 40 25 
When the queue order is: 25-10-40-5-25, the sum of the absolute value of the height difference is 15+30+35+20=100. 
This is the biggest crazy value

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

public class CrazyValue {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			int n =sc.nextInt();
			int[] arr = new int[n];
			for(int i =0;i<n;i++)
				arr[i] = sc.nextInt();
			Arrays.sort(arr);
			int res = 0,res1=0;
			int middle = n/2;
			if(n%2==0){
				for(int i=0;i<middle;i++)
					res += 2*(arr[i+middle]-arr[i]) ;
				res += arr[middle-1] - arr[middle];
				System.out.println(res);
			}
			else{
				for(int i=0;i<middle;i++){
					res += 2*(arr[i+middle+1]-arr[i]);
					res1 += 2*(arr[i+middle+1]-arr[i]);
				}					
				res += arr[middle-1] -arr[middle];
				res1 += arr[middle] - arr[middle+1];//The last number may be wrong
				System.out.println(Math.max(res,res1));
			}
		}
	}

}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325520163&siteId=291194637