只打印那些是 Java 数组中另一个数字的两倍的数字

Hus*_*989 7 java arrays algorithm

我正在练习算法,假设我们有一个包含元素的数组2, 3, 9, 12, 7, 18,然后我只想打印 18 ,因为它是 9 的两倍。当我打印结果时,它总是显示更多的行,但是,打印的数字(如果有的话)很好。如何管理正确显示结果?(仅使用数组。)我知道这是一个愚蠢的问题,但我尝试了很多方法,结果更糟。

现在,它向我展示了这一点(140 是正确的,但不需要另一行):

The following number is doubled of another number from the array: 0
The following number is doubled of another number from the array: 0
The following number is doubled of another number from the array: 140
The following number is doubled of another number from the array: 0
The following number is doubled of another number from the array: 0
Run Code Online (Sandbox Code Playgroud)

我的代码:

public class DoubleNums {

    public static void main(String[] args) {

        Random random = new Random();

        int[] array = new int[50];
        int[] even;
        int[] doubleNum;
        int count = 0;
        int doubles = 0;

        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(200);
            System.out.println(array[i] + " ");
        }
        for (int j = 0; j < array.length; j++) { 
            even = new int[array.length];
            if (array[j] % 2 == 0) {
                even[count] = array[j];
                count++;
            }
            for (int k = 0; k < array.length; k++) {
                doubleNum = new int[array.length];
                if (array[j] / 2 == array[k]) {
                    even[doubles] = k;
                    doubles++;
                    System.out.println("The following number is doubled of another number from the array: " + even[doubles]);               
                }                
            }               
        }
        System.out.println("Number of even nums: " + count);
        }
    }
Run Code Online (Sandbox Code Playgroud)

And*_*ner 8

使用 aHashSet<Integer>来存储每个元素的双倍值:

Set<Integer> setOfArray = new HashSet<>();
for (int a : array) setOfArray.add(2 * a);
Run Code Online (Sandbox Code Playgroud)

然后再次循环遍历数组,寻找匹配的元素:

for (int a : array) {
  if (setOfArray.contains(a)) {
    System.out.println(a);
  }
}
Run Code Online (Sandbox Code Playgroud)

或者

IntStream.of(array).boxed().filter(setOfArray::contains)
    .forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)

  • 整数除法不会导致赔率处理不当吗?也许在条件中需要 % 2 == 0 。 (3认同)

Dan*_*iel 5

这是一个简单的任务:首先,对于每个数字n,将2n存储在一个集合中。然后,遍历数字并检查它们中的哪些在集合中。

import java.util.*;

public class DoubleNums {
    public static void main(String[] args) {
        int[] numbers = new int[]{2, 3, 9, 12, 7, 18};   //original array

        HashSet<Integer> h = new HashSet<Integer>();     //set

        for(int i = 0; i < numbers.length; i++) h.add(numbers[i] * 2);
        for(int i = 0; i < numbers.length; i++){
            if(h.contains(numbers[i])) System.out.println(numbers[i]);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

复杂度:O(n)

AHashSet<Integer>像一个简单的集合一样工作,但速度更快,因为它使用哈希表的属性来添加和查找元素。


Alb*_*ino 3

我看到您要求一个以您自己的代码开头的解决方案......

我知道它更快,但我认为我们可以使用我的代码进行一些细微的更改找到解决方案。

所以我试图理解你想要做什么,但我无法破译你的方法,所以从你的开始,我创建了一个更清晰的版本。


变化

  • 我使用 ArrayList ( list) 作为支持,而不是你的数组 ( even)。List对于您的情况来说非常方便,因为它具有该功能contains()并且其大小是可变的。
  • 我在数组中取出一个偶数,并查找是否至少有“一半”。

代码

public static void main(String[] args) {
    Random random = new Random();

    int[] array = new int[20];
    ArrayList<Integer> list = new ArrayList<>();
    int evenNumber = 0;

    for (int i = 0; i < array.length; i++) {
        array[i] = random.nextInt(100);
    }

    System.out.println(Arrays.toString(array));

    for (int i = 0; i < array.length; i++) {
        // If I found an even number and this is NOT already in the list then I'll study it
        if (array[i] % 2 == 0 && !list.contains(array[i])) {
            evenNumber = array[i];
            for (int j = 0; j < array.length; j++) {
                // I add "evenNumber" to the list only if at least one its "half" exist in the array
                if (array[j] * 2 == evenNumber) {
                    list.add(evenNumber);
                    // I dont need to search more, I already found its "half"!
                    break;
                }
            }
        }
    }

    System.out.println("Doubled nums: " + list);
}
Run Code Online (Sandbox Code Playgroud)

样本输出

[71, 88, 45, 97, 64, 31, 54, 12, 14, 86, 22, 42, 35, 44, 70, 65, 93, 85, 99, 14]
Doubled nums: [88, 44, 70]
Run Code Online (Sandbox Code Playgroud)