gpt4 book ai didi

java - 我向 Java 数组添加项目是否错误?

转载 作者:行者123 更新时间:2023-12-02 08:25:13 24 4
gpt4 key购买 nike

public class ProjectEulerProb2 {

int firstInt = 1, secondInt = 2, thirdInt = 0, answer = 0;
int[] array = new int[4000000];
int[] evenArray = new int[90];

public static void main(String args[]) {
ProjectEulerProb2 prob = new ProjectEulerProb2();
prob.doIt();
prob = null;

}

public void doIt() {

for (int i = 0; i <= 4000000; i++) {

if (i == 0) {
thirdInt = firstInt + secondInt;
}
else {
firstInt = secondInt;
secondInt = thirdInt;
thirdInt = firstInt + secondInt;
}

array[i] = firstInt;
array[i + 1] = secondInt;
array[i + 2] = thirdInt;


if (thirdInt >= 4000000) {
break;
}
}


for (int j = 0; j <= 90; j = j + 3) {
if (j == 0) {
if (array[j + 1] % 2 == 0) {
System.out.println(" " + array[j + 1] % 2 + " " + array[j + 1]);
evenArray[j / 3] = array[j + 1];
}
if (array[j + 2] % 2 == 0) {
System.out.println(" " + array[j + 2] % 2 + " " + array[j + 2]);
evenArray[j / 3] = array[j + 2];
}
}

if (array[j] % 2 == 0) {
System.out.println(" " + array[j] % 2 + " " + array[j]);
evenArray[j / 3] = array[j];
}

}


for (int u = 0; u < evenArray.length; u++) {
if (u == 0) {
answer = evenArray[u];
}
else {
answer = answer + evenArray[u];
}
}
System.out.println(answer);
}
}

有人可以帮我解决这个问题吗?每次我打印数组的值时,它都会显示为 0 而不是分配的值。

编辑:好吧,我拿走了所有的“System.out.println” 发现我不需要。

编辑2:好吧,我重写了代码以不再使用数组。不过,我仍然有兴趣找出上一个版本哪里出了问题。



<p>public class ProjectEulerProb2Other {
static int firstInt=1, secondInt=2, thirdInt=0, answer=0;</p>

<p>public static void main(String[] args){</p>

<pre><code>for(int i = 0; i<=4000000;i++){
if(i==0){
if(firstInt%2==0){

answer = answer+firstInt;

}
if(secondInt%2==0){

answer = answer+secondInt;

}
thirdInt = firstInt+secondInt;
}else{
firstInt = secondInt;
secondInt = thirdInt;
thirdInt = firstInt+secondInt;
if(thirdInt%2==0){

answer = answer+thirdInt;

}

}



if(thirdInt>=4000000){
System.out.println(answer);

break;

}
}

}

}
</code></pre>

最佳答案

向数组添加元素的方式没有问题。

array[i] = firstInt;

正确。不过,你的逻辑有问题。因为这可能是一项作业,所以我会让你找到它们:)

编辑

好的,问题出在第一个版本的循环中:

for (int j = 0; j <= 90; j = j + 3) {
if (j == 0) { //Bad idea!!
if (array[j + 1] % 2 == 0) { //Always false. array[0 + 1] == 1
System.out.println(" " + array[j + 1] % 2 + " " + array[j + 1]);
evenArray[j / 3] = array[j + 1];
}
if (array[j + 2] % 2 == 0) { //Always true. array[0 + 2] == 2
System.out.println(" " + array[j + 2] % 2 + " " + array[j + 2]);
evenArray[j / 3] = array[j + 2];
}
}

if (array[j] % 2 == 0) {
System.out.println(" " + array[j] % 2 + " " + array[j]);
evenArray[j / 3] = array[j];
}

}

我的修复:

int jCpt = 0; //To add in evenArray in an orderly manner
for (int j = 0; jCpt < 90 && j < 4000000; ++j) { //Changed this
if (array[j] % 2 == 0) {
System.out.println(" " + array[j] % 2 + " " + array[j]);

evenArray[jCpt++] = array[j]; //We add alement #j from array to evenArray
/* Equivalent of
* evenArray[jCpt] = array[j];
* jCpt = jCpt + 1;
*/
}
}

但那个版本可能更好:

int evenCpt = 0; //To insert the even numbers one after the other
int fibonacciCpt = 0; //To iterate through the fibonacci numbers in array

for (; evenCpt < 90 && fibonacciCpt < 4000000; ++fibonacciCpt) {
if (array[fibonacciCpt] % 2 == 0)
evenArray[evenCpt++] = array[fibonacciCpt];
}

恭喜,您解决了问题 #2 :)

关于java - 我向 Java 数组添加项目是否错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4665026/

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