Question number: | 201612-1 |
Question name: | Middle number |
time limit: | 1.0s |
Memory limit: | 256.0MB |
Problem Description: | Problem Description In an integer sequence a 1, a 2, …, an , if there is a certain number, the number of integers greater than it is equal to the number of integers less than it, then it is called an intermediate number. In a sequence, there may be multiple intermediate numbers with different subscripts, and the values of these intermediate numbers are the same. Input format The first line of input contains an integer n , which represents the number of numbers in the integer sequence. Output format If the middle number of the agreed sequence exists, the value of the middle number is output, otherwise, -1 is output to indicate that there is no middle number. Sample input 6 Sample output 5 Sample description There are 2 numbers smaller than 5, and 2 numbers larger than 5. Sample input 4 Sample output -1 Sample description None of the 4 numbers in the sequence satisfy the definition of the middle number. Sample input 5 Sample output -1 Sample description None of the five numbers in the sequence satisfy the definition of the middle number. Evaluation use case scale and conventions For all evaluation use cases, 1 ≤ n ≤ 1000 and 1 ≤ ai ≤ 1000. |
import java.util.Scanner;
public class 中间数 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n+1];
for(int i=1;i<=n;i++){
a[i]=sc.nextInt();
}
int more=0;
int less=0;
for(int i=1;i<=n;i++){
more=0;
less=0;
for(int j=1;j<=n;j++){
if(a[i]>a[j]){
less++;
}else if(a[i]<a[j]){
more++;
}
}
if(more==less){
System.out.println(a[i]);
break;
}
}
if(more!=less){
System.out.println(-1);
}
}
}