数字永远不会相邻出现。

huangapple go评论83阅读模式
英文:

The numbers never occur next to each other

问题

我编写了一个程序,它读取一个整数数组和两个数字 nm。程序会检查数组中是否从未同时出现过 nm(无论顺序如何)。

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 &lt; len; i++){
            array [i] = scanner.nextInt();
        }

        int n = scanner.nextInt();
        int m = scanner.nextInt();

        for (int j = 1; j &lt; len; j++){
           if((array[j]==n)&amp;&amp;(array[j+1]==m) || (array[j]==n)&amp;&amp;(array[j-1]==m) || (array[j]==m)&amp;&amp;(array[j+1]==n) || (array[j]==m)&amp;&amp;(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 &lt; len. The condition should be j &lt; len -1.

The following works as expected:

for (int j = 1; j &lt; len - 1; j++) {
	if ((array[j] == n &amp;&amp; array[j + 1] == m) || (array[j] == n &amp;&amp; array[j - 1] == m)
			|| (array[j] == m &amp;&amp; array[j + 1] == n) || (array[j] == m &amp;&amp; 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 &lt; len; j++){
   if((array[j]==n &amp;&amp; array[j-1]==m) || (array[j]==m &amp;&amp; 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&lt;Integer, Set&lt;Integer&gt;&gt; map = new HashMap&lt;&gt;();

    for (int i = 0, prv = 0; i &lt; len; i++) {
        int num = scan.nextInt();

        if (!map.containsKey(num))
            map.put(num, new HashSet&lt;&gt;());

        if (i &gt; 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) &amp;&amp; !map.get(m).contains(n);
    System.out.println(res);
}

huangapple
  • 本文由 发表于 2020年10月25日 18:59:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64522930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定