| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 667 人关注过本帖
标题:递归-查找数组中的元素,怎么改啊
只看楼主 加入收藏
想你的天空
Rank: 2
等 级:新手上路
威 望:5
帖 子:610
专家分:0
注 册:2004-12-30
收藏
 问题点数:0 回复次数:1 
递归-查找数组中的元素,怎么改啊
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<conio.h>
#include<vector>
using namespace std;
bool find(int a[],int xx,int n,int size)
{
 bool findd = false;
 if(a[n]==xx)
  findd = true;
 else if((n>size)&&(a[n]!=xx))
   findd = false;
 else
  find(a,xx,++n,size);
 return findd;
}
int main()
{  
 int a[]={1,2,3,4};
 cout<<find(a,2,0,3)<<endl;
 getch();
 return 0;
}

//问题是只能找出第1个元素
搜索更多相关主题的帖子: 递归 元素 
2005-08-27 08:55
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
#include<cstdlib>

#include<iostream>

using namespace std;



int find(int * p, int searchValue, int startIndex, int size)

{

    if(startIndex>size-1 || startIndex<0)

        return -1;

    else if(p[startIndex]==searchValue)

    {

        return startIndex;

    }

    else

    find(p, searchValue, ++startIndex, size);

}



int main()

{  

    int a[]={1,2,8,4};

    int indexOfSearchedValue = find(a,4,0,4);

    cout<<indexOfSearchedValue<<endl;

    system("pause");

    return 0;

}

[此贴子已经被作者于2005-8-27 10:04:32编辑过]



自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-08-27 09:58
快速回复:递归-查找数组中的元素,怎么改啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014418 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved