gpt4 book ai didi

java - 如何在方法之间进行交集

转载 作者:行者123 更新时间:2023-11-30 06:05:11 25 4
gpt4 key购买 nike

我的代码的输出是:

The fibonacci list that smaller than 40 is:
0 1 1 2 3 5 8 13 21 34
The prime list that smaller than 40 is:
2 3 5 7 11 13 17 19 23 29 31 37

我想在这两个列表之间建立交集。

使其变为:(当我将变量 n=40 放入 fibo() 和 allPrime() 方法中时)

2 3 5 13 

但我不知道该怎么做。我搜索过论坛,大多数交集问题都是在两个数组列表或两个集合之间。

我想知道是否可以像这样在两个函数之间进行交集?

public class FiboAndPrime {

static boolean IsPrime(int n) {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}

// Find all the prime numbers that are less than or equal to n
static void allPrime(int n) {
for(int i=2; i<=n; i++) {
if(IsPrime(i)) System.out.print(i+ " ");
}
}

//Find the Fibonacci numbers that are less than or equal to n
static void fibo(int n) {
int fibo[] = new int[n];
fibo[0] = 0;
fibo[1] = 1;
System.out.print(fibo[0]+" "+fibo[1]+" ");
for (int i = 2; i <= n; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
if (n >= fibo[i]) {
System.out.print(fibo[i]+ " ");
} else {
break;
}
}
}

public static void main(String[] args) {
int k = 40;
System.out.println("The fibonacci list that smaller than " + k + " is:");
fibo(k);
System.out.println();
System.out.println("The prime list that smaller than " + k + " is:");
allPrime(k);
}

}

我尝试更改代码以使用 ArrayList,但我被 fibo() 方法困住了。

输出为:

The final intersection that are both fabonacci and prime is:
0 1true true true true true true true true
The prime list that smaller than 40 is:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]

为什么它在我的斐波那契列表中变成了 boolean 类型?

static void allPrime(int n) {
List<Integer> primes = new ArrayList<Integer>(n);
for(int i=2; i<=n; i++) {
if(IsPrime(i)) {
primes.add(i);
}
}
System.out.print(primes);
}

static void fibo(int n) {
List <Integer> fibos = new ArrayList<>(n);
int fibo[] = new int[n];
fibo[0] = 0;
fibo[1] = 1;
System.out.print(fibo[0]+" " + fibo[1]);
for (int i = 2; i <= n; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
if (n >= fibo[i]) {
int in =fibo[i];
System.out.print(fibos.add(in)+ " ");
} else {
break;
}
}
}

最佳答案

您将需要使用诸如HashSetArrayList之类的数据结构来执行此操作,然后找到它们之间的交集。

使用ArrayList的解决方案:

import java.util.List;
import java.util.ArrayList;

public class FiboAndPrime {


static boolean IsPrime(int n) {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
// Find all the prime numbers that are less than or equal to n
static void allPrime(int n, List<Integer> prime_set ) {
for(int i=2; i<=n; i++) {
if(IsPrime(i)) System.out.print(i+ " ");
prime_set.add(i);
}
}
//Find the Fibonacci numbers that are less than or equal to n
static void fibo(int n, List<Integer> fibo_set ) {
int fibo[] = new int[n];
fibo[0] = 0;
fibo[1] = 1;
System.out.print(fibo[0]+" "+fibo[1]+" ");
for (int i = 2; i <= n; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
if (n >= fibo[i]) {
System.out.print(fibo[i]+ " ");
fibo_set.add(fibo[i]);
} else {
break;
}
}
}


public static void main(String[] args) {
int k = 40;


System.out.println("The fibonacci list that smaller than " + k + " is:");
List<Integer> fibo_set = new ArrayList<Integer>();
fibo_set.add(0);
fibo_set.add(1);
List<Integer> prime_set = new ArrayList<Integer>();
fibo(k,fibo_set);
System.out.println();
System.out.println("The prime list that smaller than " + k + " is:");
allPrime(k,prime_set);

fibo_set.retainAll(prime_set); // fibo_set now contains only elements in both sets

System.out.println();
System.out.println("intersection between the fibo and prime set:");
for (Integer intersection : fibo_set) {
System.out.println(intersection);
}

}
}

使用HashSet的解决方案:

import java.util.Set;
import java.util.HashSet;

public class FiboAndPrime {


static boolean IsPrime(int n) {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
// Find all the prime numbers that are less than or equal to n
static void allPrime(int n, Set<Integer> prime_set ) {
for(int i=2; i<=n; i++) {
if(IsPrime(i)) System.out.print(i+ " ");
prime_set.add(i);
}
}
//Find the Fibonacci numbers that are less than or equal to n
static void fibo(int n, Set<Integer> fibo_set ) {
int fibo[] = new int[n];
fibo[0] = 0;
fibo[1] = 1;
System.out.print(fibo[0]+" "+fibo[1]+" ");
for (int i = 2; i <= n; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
if (n >= fibo[i]) {
System.out.print(fibo[i]+ " ");
fibo_set.add(fibo[i]);
} else {
break;
}
}
}


public static void main(String[] args) {
int k = 40;


System.out.println("The fibonacci list that smaller than " + k + " is:");
Set<Integer> fibo_set = new HashSet<Integer>();
fibo_set.add(0);
fibo_set.add(1);
Set<Integer> prime_set = new HashSet<Integer>();
fibo(k,fibo_set);
System.out.println();
System.out.println("The prime list that smaller than " + k + " is:");
allPrime(k,prime_set);

fibo_set.retainAll(prime_set); // fibo_set now contains only elements in both sets

System.out.println();
System.out.println("intersection between the fibo and prime set:");
for (Integer intersection : fibo_set) {
System.out.println(intersection);
}

}
}

关于java - 如何在方法之间进行交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51490075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com