我的代码的输出是:
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
我想在这两个列表之间建立交集。
使其变为:(当我在fibo()和allPrime()方法中放入变量n = 40时)
2 3 5 13
但是我不知道该怎么做。我已经搜索了论坛,并且大多数交集问题是在两个arraylist或两个setlist之间。
我想知道是否可以像这样在两个函数之间进行交集?
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]
为什么它在我的fibo列表中变成布尔类型?
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;
}
}
}
答案 0 :(得分:1)
您将需要使用HashSet
或ArrayList
之类的数据结构来执行此操作,然后找到它们之间的交集。
使用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);
}
}
}
答案 1 :(得分:0)
绝对有可能。我不会为您编写代码,但是我可以建议您应该怎么做。您应该将它们存储在2个不同的数组中并尝试查找它们之间的共同点,而不是打印40以下的斐波那契数和素数。但是我认为您不熟悉arraylist和set,我建议您使用另一种方法。
当斐波那契数在40以下时,请检查该数是否为素数。如果是这样,则打印它,否则就不打印。
答案 2 :(得分:0)
为什么在Java中使用array
而不是list
。您可以通过retainAll
来实现,如下所示:
List<Integer> ret = new ArrayList<>(primes);
ret.retainAll(fibos);
对于本地测试:
public static void main(String... args) {
List<Integer> primes = new ArrayList<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37));
List<Integer> fibos = new ArrayList<>(Arrays.asList(0, 1, 1, 2, 3, 5, 8, 13, 21, 34));
List<Integer> ret = new ArrayList<>(primes);
ret.retainAll(fibos);
System.out.println(ret);
}
输出将是:
[2, 3, 5, 13]
根据OP的要求,我将其添加为参考:
static void fibo(int n) {
List <Integer> fibos = new ArrayList<>(n);
fibos.add(0);
fibos.add(1);
System.out.print(fibos.get(0) + "," + fibos.get(1));
for (int i = 2; i <= n; i++) {
int a = fibos.get(i-1) + fibos.get(i-2);
if (a <= n) {
fibos.add(a);
System.out.print("," + a);
} else break;
}
}