我正在尝试用C编写递归函数,
给定一个数组,数组的大小以及int int的元素,我想找到int target的最后一次出现的索引。
#include <stdio.h>
int rLookupAr(int array[], int size, int target);
int main()
{
int numArray[80];
int target, i, size;
printf("Enter array size: \n");
scanf("%d", &size);
printf("Enter %d numbers: \n", size);
for (i=0; i < size; i++)
scanf("%d", &numArray[i]);
printf("Enter the target number: \n");
scanf("%d", &target);
printf("rLookupAr(): %d", rLookupAr(numArray, size, target));
return 0;
}
int rLookupAr(int array[], int size, int target)
{
}
然而,我被困了好几个小时。
我是递归函数的初学者,非常感谢任何帮助!
一些例子:
Enter array size:
5
Enter 5 numbers:
2 1 3 2 4
Enter the target number:
2
rLookupAr(): 3
答案 0 :(得分:2)
package baleli.urdupoetrybytariq;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import baleli.urdupoetrybytariq.R;
/**
* Created by SONU on 16/09/15.
*/
public class DummyFragment extends Fragment {
private View view;
private String title;//String for tab title
private static RecyclerView recyclerView;
public DummyFragment() {
}
public DummyFragment(String title) {
this.title = title;//Setting tab title
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.dummy_fragment, container, false);
setRecyclerView();
return view;
}
//Setting recycler view
private void setRecyclerView() {
recyclerView = (RecyclerView) view
.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView
.setLayoutManager(new LinearLayoutManager(getActivity()));//Linear Items
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 0; i < 50; i++) {
arrayList.add(title+"" + i);//Adding items to recycler view
}
RecyclerView_Adapter adapter = new RecyclerView_Adapter(getActivity(), arrayList);
recyclerView.setAdapter(adapter);// set adapter on recyclerview
}
}
数组中查找值与通过选中package baleli.urdupoetrybytariq;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
/**
* Created by SONU on 31/08/15.
*/
public abstract class DemoViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public DemoViewHolder(View view) {
super(view);
this.title = (TextView) view.findViewById(R.id.cardTitle);
}
}
然后检查[0...N]
来查找值相同,然后通过检查{{{ 1}}然后检查[0]
...希望你看到一种模式出现。[1...N]
,您先检查[1]
,然后使用范围[2..N]
重复,然后选中N
然后重复与[N]
。我希望能让你在没有我的情况下找到解决方案。
答案 1 :(得分:1)
您可以将rLookupAr()
功能定义如下。查看完整的工作代码here:
int rLookupAr(int array[], int size, int target)
{
if(size < 1) return -1;
size--;
if(array[size] == target) return size;
return rLookupAr(array, size,target);
}
注意:如果在rLookupAr()
数组中找不到-1
值,则 target
函数将返回array
。
<强>输出强>:
Enter array size: 5
Enter 5 numbers: 2 1 3 2 4
Enter the target number: 2
rLookupAr(): 3
以下是完整的代码:
#include <stdio.h>
int rLookupAr(int array[], int size, int target);
int main()
{
int numArray[80];
int target, i, size;
printf("Enter array size: \n");
scanf("%d", &size);
printf("Enter %d numbers: \n", size);
for (i=0; i < size; i++)
scanf("%d", &numArray[i]);
printf("Enter the target number: \n");
scanf("%d", &target);
printf("rLookupAr(): %d", rLookupAr(numArray, size, target));
return 0;
}
int rLookupAr(int array[], int size, int target)
{
if(size < 1) return -1;
size--;
if(array[size] == target) return size;
return rLookupAr(array, size,target);
}
答案 2 :(得分:0)
请在下面找到递归函数:
int rLookupAr (int array[], int size, int target)
{
if(size<=0) return -1;
if(array[size-1] == target)
return size-1;
else
return rLookupAr (array, size-1, target); //recurse
}
基本上这些函数从最后开始,因为正在寻找最后一次出现。根据是否找到目标,它会进一步递减数组。
完整代码:
#include <stdio.h>
int rLookupAr (int array[], int size, int target);
int
main ()
{
int numArray[80];
int target, i, size;
printf ("Enter array size: \n");
scanf ("%d", &size);
printf ("Enter %d numbers: \n", size);
for (i = 0; i < size; i++)
scanf ("%d", &numArray[i]);
printf ("Enter the target number: \n");
scanf ("%d", &target);
printf ("rLookupAr(): %d", rLookupAr (numArray, size, target));
return 0;
}
int
rLookupAr (int array[], int size, int target)
{
if(size<=0) return -1;
if(array[size-1] == target)
return size-1;
else
return rLookupAr (array, size-1, target); //recurse
}
输出:
Enter array size:
5
Enter 5 numbers:
2
1
3
2
4
Enter the target number:
2
rLookupAr(): 3
答案 3 :(得分:0)
以下是一个单一陈述的解决方案:
int rLookupAr(int array[], int size, int target) {
return size-- <= 0 ? -1 : array[size] == target ? size : rLookup(array, size, target);
}
对于像这样的简单案例使用递归解决方案仅仅是作为练习。 C编译器不需要执行尾递归优化来将此代码转换为循环,因此这种递归实现很容易导致堆栈溢出,适用于中等大小的数组。
答案 4 :(得分:0)
我是递归函数的初学者
递归函数通过线性约简得到一个坏名称。
而是在2中解决问题以实现log2(n)的递归深度。
int rLookupAr(int array[], int size, int target) {
if (size < 1) {
return -1;
}
int mid = size/2;
int right = rLookupAr(array + mid, size - mid, target);
if (right != -1) {
return right + mid;
}
int left = rLookupAr(array, mid, target);
return left;
}
答案 5 :(得分:0)
int rLookupAr(int input[], int size, int x) {
/* Don't write main().
Don't read input, it is passed as function argument.
Return output and don't print it.
Taking input and printing output is handled automatically.
*/
if(size==0)
{
return -1;
}
//return -1;
int ans=lastIndex(input+1,size-1,x);
if(ans!=-1)
{
return ans+1;
}
else{
if(input[0]==x)
{
return ans+1;
}
else
{
return -1;
}
}
}
答案 6 :(得分:0)
int lastIndex(int input[], int size, int x)
{
if (size == 0)
{
return -1;
}
int answer = lastIndex(input + 1, size - 1, x);
if (answer != -1)
{
return answer + 1;
}
if (input[0] == x)
{
return 0;
}
else
{
return -1;
}
}
int main()
{
int input[] = {9, 8, 10, 8};
int x = 8;
int size = 4;
cout << lastIndex(input, size, x);
return 0;
}