java小美拿到了一个排列。她想知道在这个排列中t和y是否是相邻的。你能帮帮她吗排列是指一个长度为n的数组其中1到n每个元素恰好出现-次。输入描述第一行输入一个正整数n代表排列的长度第二行输入n个正整数a代表排列的元素第三行输入两个正整数2和Y用空格隔开1 n 200000I aixy n保证x y输出描述如果x和y在排列中相邻则输出Yes。否则输出No
import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = scanner.nextInt(); } int x = scanner.nextInt(); int y = scanner.nextInt();
boolean adjacent = false;
for (int i = 0; i < n-1; i++) {
if ((arr[i] == x && arr[i+1] == y) || (arr[i] == y && arr[i+1] == x)) {
adjacent = true;
break;
}
}
if (adjacent) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
原文地址: https://www.cveoy.top/t/topic/iBnl 著作权归作者所有。请勿转载和采集!