If I have array a[]={2,3,5} resultant array should be {(2*3)6,(2*5)10,(3*5)15}. You can not use loop. This can be solves using recursion,but I am not able to solve it.
如果我有数组a[]={2,3,5}合成的数组应该是{(2*3)6,(2*5)10,(3*5)15}。你不能使用循环。这可以用递归来解决,但我不能解决它。
2 个解决方案
#1
0
int multiply1(int n, int left, int right)
{
if(left==(n-1))
return 1;
//System.out.print(arr[left]+" ");
//System.out.print(arr[right]+" ");
System.out.println(arr[left]*arr[right]);
right++;
if(right==n)
{
left++;
right=left+1;
}
multiply1(n, left, right);
//arr1[start]=arr[ind]*arr[next];
//multiply1(start+1, ind, next+1);
return 0;
}
#2
0
Recursively multiply an array.
递归地将一个数组。
void multiply(double[] a, double[] b, index){
if(index==a.length){
return;
}
a[index] = a[index]*b[index];
multiply(a, b, index+1);
}
#1
0
int multiply1(int n, int left, int right)
{
if(left==(n-1))
return 1;
//System.out.print(arr[left]+" ");
//System.out.print(arr[right]+" ");
System.out.println(arr[left]*arr[right]);
right++;
if(right==n)
{
left++;
right=left+1;
}
multiply1(n, left, right);
//arr1[start]=arr[ind]*arr[next];
//multiply1(start+1, ind, next+1);
return 0;
}
#2
0
Recursively multiply an array.
递归地将一个数组。
void multiply(double[] a, double[] b, index){
if(index==a.length){
return;
}
a[index] = a[index]*b[index];
multiply(a, b, index+1);
}