我目前正在做家庭作业,目标是只递归(没有循环)。我很确定我可以重载并添加辅助方法,这是我看到自己完成此任务的唯一方法。
所以问题是,我有一个int数组A = {1,2,3,4}(或类似的东西),我需要用{10,9,7,4}生成一个返回的数组
10 = 1 + 2 + 3 + 4
9 = 2 + 3 + 4
7 = 3 + 4
4 = 4
我想过使用这样的东西(不是工作代码)
int counter = 0;
public int[] r(int[] numbers){
return r(number, counter);
}
public int[] r(int[] numbers, int index){
int sum = 0;
// base case to check if next value exists otherwise end it
// this would be a helper method instead of a for loop
for(int x=index; x<numbers.length; x++){
sum += numbers[x];
}
numbers[index] = sum;
index++;
return r(numbers, index);
}
然而,我不知道如何去做。这也是我递归的第一周,所以这对我来说有点混乱。我得到了正确的数组,但我对数字[index] = sum有一个ArrayIndexOutOfBoundsException,在我的return语句上返回r(数字,索引),我不知道如何修复它。有什么想法吗?
答案 0 :(得分:1)
您需要一个停止条件,以及之后检索数组的方法
public int sum(int old, int[] numbers, int index) {
if (index == numbers.length) return old;
return sum(numbers[index] + old, numbers, index + 1);
}
public int[] r(int[] numbers, int[] output, int count) {
if (count == numbers.length) {
return output;
} else {
output[count] = sum(0, numbers, 0);
return r(numbers, output, count + 1);
}
}
public int[] r(int[] numbers) {
return r(numbers, new int[numbers.length], 0);
}
编辑:更改了代码,无需for
循环
答案 1 :(得分:1)
我认为这会给你正确答案:
public static int[] slideAndSumArrayElements(int[] array, int[] result, int index) {
// base case - stop when the index is same as the array.length
if (index == array.length) {
return result;
}
else {
// Add all elements of the array starting from the index position till length of the array and store the result in result[index]
result[index] = addArrayElementsRecursively(array, index);
// slide the main array by incrementing the index
return slideAndSumArrayElements(array, result, index + 1);
}
}
public static int addArrayElementsRecursively(int[] arr, int index){
// base case - when the index is same as the original array len stop
if (arr.length == index){
return 0;
}
// add progressively each element of the given array
return arr[index] + addArrayElementsRecursively(arr, index + 1);
}
public static void arraySum(){
int[] array = {1, 2, 3, 4};
int[] result = new int[array.length];
result = slideAndSumArrayElements(array, result, 0);
}
输出数组或结果将是: [10,9,7,4]
答案 2 :(得分:0)
你的功能是incorect
public int[] r(int[] numbers, int index){
//--> needs a stop condition e.g. index==numbers.length, index==0
int sum = 0;
// base case to check if next value exists otherwise end it
// this would be a helper method instead of a for loop
for(int x=index; x<numbers.length; x++){
//--> numbers has been overwritten by the sum
// you should just do numbers[index] = numbers[index-1]+numbers[index]
// this is opposite to the summation you wish for which starts adding
// from the last position
sum += numbers[x];
}
numbers[index] = sum;
index++;
return r(numbers, index);
}
你的功能的正确解决方案是
public int[] r(int[] numbers, int index){
if (index == 0) {
return numbers;
}else{
numbers[index] = numbers[index+1] + numbers[index]
return r(numbers,index-1);
}
public int[] r(int[] numbers){
return r(numbers, numbers.length);
}
但是你因为覆盖它而丢失了初始数组
OR
你可以建立一个这样的功能
public int sumRecursive(int[] input, int[] output,int pos){
if (pos==0){
output[pos] = input[pos]
return input[pos]
}else{
output[pos] = input[pos] + sumRecursive(input,output,pos-1);
return output[pos];
}
}
你需要像这样称呼它
int[] output = new int[input.length];
sumRecursive(input,output,input.length);