【发布时间】:2020-08-01 04:15:10
【问题描述】:
如果我的数组是 9 8 10 8 并且我想搜索 10 并且在递归中 ans 将返回 1 然后它将使用 ans+1 进行广告并返回 2,那么有人可以解释变量 ans 如何返回数组的索引。
enter code here
int firstIndex(int input[], int size, int x) {
if(size==0)
{
返回 -1;
}
if(input[0]==x)
{ 返回 0;
}
int ans=firstIndex( input+1, size-1, x);
if(ans!=-1)
{
return ans+1;
}
else
{
return ans;
}
}
【问题讨论】:
标签: recursion