| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1326 人关注过本帖
标题:c++简单程序
取消只看楼主 加入收藏
xxjjkk
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-12-18
结帖率:100%
收藏
 问题点数:0 回复次数:0 
c++简单程序
#include <iostream>

using namespace std;

const int size = 10;

void search(const int a[],int first,int last,int key,bool& found,int& laction);

int main()

{
   
    int n;
   
    int a[size];
   
    int first;
   
    int last;
   
    int laction;
   
    bool found;
   
    int key;
   
    for(n=0;n<size;n++)
        
    {
        
        a[n]=n+1;
        
        cout<<a[n]<<"  ";
        
    }
   
    cout<<endl;
   
   
   
   
   
    last=size-1;
   
    cin>>key;
   
    search(a,0,last,key,found,laction);
   
    if(found)
        
        cout<<key<<"laction is :"<<laction<<endl;
   
    else
        
        cout<<key<<"is not laction"<<endl;
   
   
   
    return 0;
   
}



void search(const int a[],int first,int last,int key,bool& found,int& laction)

{
   
    int mid;
   
    if(first > last)
        
        found = false;
   
    else
        
    {
        
        mid=(first + last)/2;
        
        if(key = a[mid])
            
        {
            
            found=true;
            
            laction=mid;
            
        }
        
        else if (key > a[mid])
            
            search(a,mid+1,last,key,found,laction);
        
        else if(key < a[mid])
            
            search(a,first,mid-1,key,found,laction);
        
    }
   
}
这个是递归的二分法,我的输出总是4,求解决。。。
搜索更多相关主题的帖子: include search 
2011-12-20 15:42
快速回复:c++简单程序
数据加载中...
 
   



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

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