CSP201612-1: intermediate number, CSP201612-1:
Introduction:CSP (http://www.cspro.org/lead/application/ccf/login.jsp) is a "Computer vocational qualification certification" examination initiated by CCF, perform capability certification for professionals in computer software development, software testing, information management, and other fields. The subjects of certification are those who are engaged in or will be engaged in IT professional technical and technical management personnel, as well as review subjects for university recruitment and postgraduate students.
In an integer sequence a1, a2 ,..., An, if there is a number, the number of integers greater than it is equal to the number of integers smaller than it, it is called as the number. In a sequence, multiple subscripts may have different intermediate numbers. These intermediate numbers have the same values.
For an integer sequence, locate the value of the intermediate number of the sequence.
The first line of the input contains an integer n, indicating the number of numbers in the integer sequence.
The second row contains n positive integers, indicating a1, a2 ,..., An.
If the intermediate number of the agreed sequence exists, the value of the intermediate number is output; otherwise, output-1 indicates that there is no intermediate number.
6
2 6 5 6 3 5
5
There are two numbers smaller than 5 and two smaller than 5.
4
3 4 6 7
-1
The four numbers in the sequence do not meet the definition of the intermediate number.
5
3 4 6 6 7
-1
None of the five numbers in the sequence meet the definition of the intermediate number.
- Scale and conventions of evaluation cases
For all evaluation cases, 1≤n ≤1000,1≤ai ≤1000.
First, use the sort () function to sort the n numbers in ascending order, so that all numbers with the same value will be adjacent. Then, we can find the starting point I and ending point j of a set of repeated numbers through a loop, as long as the number before I is equal to the number after j, this indicates that the number of duplicates in this group is the intermediate number we are looking.
# Include <stdio. h> # Include <stdlib. h> # Include <memory. h> # Include <algorithm> Using namespace std; Bool compare (int a, int B) { Return a <B; } Int main (void) { Int n; // number Int sign = 1; Scanf ("% d", & n ); Int * input = (int *) malloc (sizeof (int) * n ); Memset (input, 0, sizeof (int) * n ); For (int I = 0; I <n; I ++) { Scanf ("% d", input + I ); } Sort (input, input + n, compare); // sort in ascending order Int I, j; For (I = 0; I <n ;) { For (j = I; j <n; j ++) { If (input [I]! = Input [j]) { Break; } } If (I = n-j) // number greater than input [I] = number less than input [I] { Printf ("% d \ n", input [I]); Sign = 0; } I = j; } If (sign) { Printf ("% d \ n",-1 ); } Return 0; } |