简体   繁体   English

java棘手问题完整数组

[英]java tricky questions complete array

An array is defined to be complete if all its elements are greater than 0 and all even numbers that are less than the maximum even number are in the array.如果数组的所有元素都大于 0,并且小于最大偶数的所有偶数都在数组中,则数组被定义为完整数组。 For example {2, 3, 2, 4, 11, 6, 10, 9, 8} is complete because例如 {2, 3, 2, 4, 11, 6, 10, 9, 8} 是完整的,因为

a.一种。 all its elements are greater than 0它的所有元素都大于0

b.the maximum even integer is 10 c.最大偶数是 10 c。 all even numbers that are less than 10 (2, 4, 6, 8) are in the array.所有小于 10 (2, 4, 6, 8) 的偶数都在数组中。

But {2, 3, 3, 6} is not complete because the even number 4 is missing.但是 {2, 3, 3, 6} 不完整,因为缺少偶数 4。 {2, 3, 4, 3, 6} is not complete because it contains a negative number. {2, 3, 4, 3, 6} 不完整,因为它包含一个负数。 Write a function named isComplete that returns 1 if its array argument is a complete array.编写一个名为 isComplete 的函数,如果其数组参数是完整数组,则返回 1。 Otherwise it returns 0否则返回 0

This is a question i have to solve but i am not able to find good logic for this questions .这是我必须解决的问题,但我无法为这些问题找到好的逻辑。 Here I have find maximum even numbers from the array in first loop and in second loop, i have to check all the even numbers less than the maximum even numbers and i am stuck on this.在这里,我在第一个循环和第二个循环中从数组中找到了最大偶数,我必须检查所有小于最大偶数的偶数,我被困在这个问题上。 please help me ,,,,,,,,,,my code is below请帮帮我,,,,,,,,,,我的代码如下

public class Complete {
  public static void main(String[] args) {
    System.out.println(isComplete(new int[]{2,3,2,4,11,6,10,9,8}));
    System.out.println(isComplete(new int[]{2,3,3,6}));
    System.out.println(isComplete(new int[]{2,-3,4,3,6}));
  }

  private static int isComplete(int[] i) {
    int set = 1;
    int maxeven = 0;  
    int count = 0;
    for(int a = 0; a < i.length; a++) {
      if(i[a] < 0) {
        set = 0;
          break;
        }
        if(i[a]%2 == 0 && i[a] > maxeven) {
          maxeven = i[a];
        }
     }

     for (int c = 2; c <= maxeven; c=c+2) {
       for( int b = 0; b<i.length; b++) {
         if (c == i[b]) {
           count++;
         }
      }
      if (count > 0) {
        set = 1;
      } else {
        set = 0;
        break;
      }         
    }
    return set;
  }

}

I have find maximum even numbers from the array in first loop and in second loop, i have to check all the even numbers less than the maximum even numbers我在第一个循环和第二个循环中从数组中找到了最大偶数,我必须检查所有小于最大偶数的偶数

I have followed this logic and wrote this.我遵循了这个逻辑并写了这个。 I am checking the validity of condtion 1 (All elements >0) and in the same loop finding the largest even number.我正在检查条件 1(所有元素 > 0)的有效性,并在同一个循环中找到最大的偶数。 In the second loop, I am checking whether all the even numbers lesser than the largest even nuber is present.在第二个循环中,我检查是否存在小于最大偶数的所有偶数。

public class Complete {
    public static void main(String[] args) {
        System.out.println(isComplete(new int[] { 2, 3, 2, 4, 11, 6, 10, 9, 8 }));
        System.out.println(isComplete(new int[] { 2, 3, 3, 6 }));
        System.out.println(isComplete(new int[] { 2, -3, 4, 3, 6 }));
    }

    private static int isComplete(int[] i) {
        int maxEven = 0;

        for (int element : i) {
            if (element <= 0) {
                return 0;
            }
            if (element % 2 == 0) {
                if (element > maxEven) {
                    maxEven = element;
                }
            }
        }

        for (int a = 2; a < maxEven; a = a + 2) {
            if (!hasElement(i, a)) {
                return 0;
            }
        }
        return 1;
    }

    private static boolean hasElement(int[] i, int a) {
        for (int element : i) {
            if (element == a) {
                return true;
            }
        }
        return false;
    }
}

Set count = 0 in the second loop, like this:在第二个循环中设置count = 0 ,如下所示:

for(int c = 2; c<=maxeven;c=c+2){
    count = 0;
    for(int b = 0;b<i.length;b++){

That should solve your problem.那应该可以解决您的问题。 Without count = 0 , count will be >0 after you've looked for "2", so count>0 will be true when you look for "4" even if there are not "4"s.如果没有count = 0 ,则在您查找“2”后,count 将 >0,因此,即使没有“4”,当您查找“4”时,count>0 也​​会为真。

int isComplete(int[] a) { int max = 0; int isComplete(int[] a) { int max = 0;

for (int el: a) {
    if (el <= 0) {
        return 0;
    }
    if (el % 2 == 0) {
        if (el > max) {
            max = el;
        }
    }
}
int req = (max / 2) - 1;
int p = 0;
for (int c = 2; c < max; c = c + 2) {
    for (int j = 0; j < a.length; j++) {
        if (a[j] == c) {
            p++;
        }
    }
}
if (req == p) {
    return 1;
} else
    return 0;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM