从键盘输入N个整数,并输出指定的某个整数在这N个整数中的按照由小到大的顺序排列的位次(最小的位次是1,最大的位次是N,指定的整数如果不在这N个数中,则其位次是-1)
输入格式:
整数个数,指定的整数值
输出格式:
指定的整数的位次
输入样例:
在这里给出一组输入。例如:
3
12 4 7
4
输出样例:
在这里给出相应的输出。例如:
1
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
ArrayList<Integer> list = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
list.add(scan.nextInt());
}
list.sort((x, y) -> x - y);
int result = list.indexOf(scan.nextInt()) + 1;
System.out.println(result == 0 ? -1 : result);
}
}

300

被折叠的 条评论
为什么被折叠?



