如何从偶数 {2,4,6,8,10,14} 中找到缺失的数字

我想从偶数中找到缺失的数字


例如:{2,4,6,8,10,14}; //输出应该是12


我试过:


public class MissingNumber {

    public static void main(String[] args) {


    int a[] = {2,4,6,8,10,14};

    int sum = 0;

    for (int i = 0; i<a.length; i++) {

        sum = sum + a[i];

    } 



    int sum1 = 0;

    for(int j=1; j<=7; j++) {

        sum1 = sum1 + j;

    } 

    System.out.println("missing number is:"+(sum1-sum));

    }

}


猛跑小猪
浏览 164回答 6
6回答

素胚勾勒不出你

我想从偶数中找到缺失的数字例如:{2,4,6,8,10,14}; //输出应该是12我试过:public class MissingNumber {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; int a[] = {2,4,6,8,10,14};&nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; for (int i = 0; i<a.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; sum = sum + a[i];&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; int sum1 = 0;&nbsp; &nbsp; for(int j=1; j<=7; j++) {&nbsp; &nbsp; &nbsp; &nbsp; sum1 = sum1 + j;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; System.out.println("missing number is:"+(sum1-sum));&nbsp; &nbsp; }}

慕慕森

不确定你为什么要查看总和,除非你确定你的一系列数字总是只缺少一个数字。否则,这样的事情怎么样:&nbsp; &nbsp; int a[] = {2,4,6,8,10,14};&nbsp; &nbsp; int expected = 2;&nbsp; &nbsp; for (int val : a) {&nbsp; &nbsp; &nbsp; &nbsp; if (expected != val) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Missing number is " + expected);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; expected = expected +2;&nbsp; &nbsp; }

慕田峪7331174

我对您的代码进行了一些更改。通过这些改变,你会得到你的答案。public class MissingNumber {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int a[] = {2,4,6,8,10,14};&nbsp; &nbsp; &nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i<a.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = sum + a[i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; int sum1 = 0;&nbsp; &nbsp; &nbsp; &nbsp; int even = 2;&nbsp; &nbsp; &nbsp; &nbsp; for (int j=0; j<=a.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum1 = sum1 + even;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; even = even + 2;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("missing number is:"+(sum1-sum));&nbsp; &nbsp; }}

MYYA

上面有很多解决方案,但它们的复杂度都是 O(N)。我相信您可以使用https://en.wikipedia.org/wiki/Binary_search_algorithm用 O(log N) 解决这个问题。原始代码:public class MissingNumber {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int a[] = {2,4,8,10, 12, 14};&nbsp; &nbsp; &nbsp; &nbsp; int start = 0;&nbsp; &nbsp; &nbsp; &nbsp; int end = a.length;&nbsp; &nbsp; &nbsp; &nbsp; int pointer = 0;&nbsp; &nbsp; &nbsp; &nbsp; while (end - start > 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (a[pointer] == (pointer + 1) * 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start = pointer;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end = pointer;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pointer = (start + end) / 2;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Missing element: " + (pointer + 2) * 2);&nbsp; &nbsp; }}您可以添加更多条件。

海绵宝宝撒

您可以尝试对数组元素进行偶数和检查。public static void main(String[] args) {&nbsp; &nbsp; int a[] = {2, 4, 6, 8, 10, 14};&nbsp; &nbsp; int sum = 2;&nbsp; &nbsp; for (int i = 0; i < a.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (a[i] != sum) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("missing number is: " + sum);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sum = sum + 2;&nbsp; &nbsp; }}

莫回无

您可以执行以下操作:// create the set of initial values to use them in filter step.Set<Integer> givenValues = Arrays.stream(a).boxed().collect(Collectors.toSet());OptionalInt first =&nbsp; &nbsp; // generate range by 2 and limit it to size of input array&nbsp; &nbsp; IntStream.iterate(2, i -> i + 2).limit(a.length)&nbsp; &nbsp; // filter out only this value that are not in givenValues&nbsp; &nbsp; .filter(i -> !givenValues.contains(i))&nbsp; &nbsp; // get first value&nbsp; &nbsp; &nbsp; &nbsp; .findFirst();// finally it have to be check if found any value. If not then return default value.System.out.println("missing number is: "+first.orElse(-1));它可能过于工程化。但它不依赖于给定输入的顺序。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java