CSP201612-1: intermediate number, CSP201612-1:

Source: Internet
Author: User

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.

 

  • Problem description

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.

  • Input Format

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.

  • Output Format

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.

  • Sample Input

6

2 6 5 6 3 5

  • Sample output

5

  • Example

There are two numbers smaller than 5 and two smaller than 5.

  • Sample Input

4

3 4 6 7

  • Sample output

-1

  • Example

The four numbers in the sequence do not meet the definition of the intermediate number.

  • Sample Input

5

3 4 6 6 7

  • Sample output

-1

  • Example

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.

 

  • Ideas

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.

  • Source code

# 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;

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.