英文:
The numbers never occur next to each other
问题
我编写了一个程序,它读取一个整数数组和两个数字 n
和 m
。程序会检查数组中是否从未同时出现过 n
和 m
(无论顺序如何)。
import java.util.*;
class Main {
public static void main(String[] args) {
// 放置您的代码在这里
Scanner scanner = new Scanner(System.in);
int len = scanner.nextInt();
int[] array = new int[len];
boolean broken = false;
for (int i = 0; i < len; i++) {
array[i] = scanner.nextInt();
}
int n = scanner.nextInt();
int m = scanner.nextInt();
for (int j = 1; j < len; j++) {
if ((array[j] == n) && (array[j + 1] == m) || (array[j] == n) && (array[j - 1] == m) || (array[j] == m) && (array[j + 1] == n) || (array[j] == m) && (array[j - 1] == n)) {
broken = true;
break;
}
}
System.out.println(broken);
}
}
测试输入:
3
1 2 3
3 4
正确输出:true
我的输出为空白。我做错了什么?
英文:
I wrote a program that reads an array of integers and two numbers n
and m
. The program check that n
and m
never occur next to each other (in any order) in the array.
import java.util.*;
class Main {
public static void main(String[] args) {
// put your code here
Scanner scanner = new Scanner (System.in);
int len = scanner.nextInt();
int [] array = new int [len];
boolean broken = false;
for (int i = 0; i < len; i++){
array [i] = scanner.nextInt();
}
int n = scanner.nextInt();
int m = scanner.nextInt();
for (int j = 1; j < len; j++){
if((array[j]==n)&&(array[j+1]==m) || (array[j]==n)&&(array[j-1]==m) || (array[j]==m)&&(array[j+1]==n) || (array[j]==m)&&(array[j-1]==n)){
broken = true;
break;
}
}
System.out.println(broken);
}
}
Test input:
3
1 2 3
3 4
Correct output: true
My output is blank. What am I doing wrong?
答案1
得分: 0
Your code will throw ArrayIndexOutOfBoundsException
as you are using array[j+1]
whereas you have loop condition as j < len
. The condition should be j < len -1
.
The following works as expected:
for (int j = 1; j < len - 1; j++) {
if ((array[j] == n && array[j + 1] == m) || (array[j] == n && array[j - 1] == m)
|| (array[j] == m && array[j + 1] == n) || (array[j] == m && array[j - 1] == n)) {
broken = true;
break;
}
}
A sample run:
3
1 2 3
3 4
true
英文:
Your code will throw ArrayIndexOutOfBoundsException
as you are using array[j+1]
whereas you have loop condition as j < len
. The condition should be j < len -1
.
The following works as expected:
for (int j = 1; j < len - 1; j++) {
if ((array[j] == n && array[j + 1] == m) || (array[j] == n && array[j - 1] == m)
|| (array[j] == m && array[j + 1] == n) || (array[j] == m && array[j - 1] == n)) {
broken = true;
break;
}
}
A sample run:
3
1 2 3
3 4
true
答案2
得分: 0
Your code will throw ArrayIndexOutOfBoundsException
because of array[j+1]
where j can be len-1
. Actually you don't need to check both sides(previous and next element), checking combination with previous is enough since in the next iteration combination with next element will be checked.
for (int j = 1; j < len; j++){
if((array[j]==n && array[j-1]==m) || (array[j]==m && array[j-1]==n)){
broken = true;
break;
}
}
英文:
Your code will throw ArrayIndexOutOfBoundsException
because of array[j+1]
where j can be len-1
. Actually you don't need to check both sides(previous and next element), checking combination with previous is enough since in the next iteration combination with next element will be checked.
for (int j = 1; j < len; j++){
if((array[j]==n && array[j-1]==m) || (array[j]==m && array[j-1]==n)){
broken = true;
break;
}
}
答案3
得分: 0
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int len = scan.nextInt();
Map<Integer, Set<Integer>> map = new HashMap<>();
for (int i = 0, prv = 0; i < len; i++) {
int num = scan.nextInt();
if (!map.containsKey(num))
map.put(num, new HashSet<>());
if (i > 0)
map.get(prv).add(num);
prv = num;
}
int n = scan.nextInt();
int m = scan.nextInt();
boolean res = !map.containsKey(n) || !map.containsKey(m) || !map.get(n).contains(m) && !map.get(m).contains(n);
System.out.println(res);
}
英文:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int len = scan.nextInt();
Map<Integer, Set<Integer>> map = new HashMap<>();
for (int i = 0, prv = 0; i < len; i++) {
int num = scan.nextInt();
if (!map.containsKey(num))
map.put(num, new HashSet<>());
if (i > 0)
map.get(prv).add(num);
prv = num;
}
int n = scan.nextInt();
int m = scan.nextInt();
boolean res = !map.containsKey(n) || !map.containsKey(m) || !map.get(n).contains(m) && !map.get(m).contains(n);
System.out.println(res);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论