Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

这个递归程序出了点问题,请牛人帮忙解决

Posted by 2008061617 at 2010-09-20 08:46:39
#include<iostream>
using namespace std;
int search(int arr[],int key,int high,int low);
    int main()
	{
	    int n, searchkey,ch[1000];
	    while(1)
	    {
	    cin>>n;
	 	cout<<"请输入数组"<<endl;
	 	for(int i=0;i<n;i++)
		{
	      	cin>>ch[i];			
		}
	 	cout<<"请输入要查找的关键字"<<endl;
		 cin>>searchkey; 
		 int high=n-1;
		 int low=0;
		 。 
		  //searchresult=-1;            
     	int	 searchresult=search(ch,searchkey,high,low);
		 cout<<"要查询的索引为:"<<searchresult<<endl;
		}
		 system("pause");
		}
		int search(int arr[],int key,int high,int low)
		{
			    int   mid=(low+high)/2;   
		        if(high<low)
				{
		          	return -1;	
			    }         
			    else if(arr[mid]==key)
                {
                    return mid;
                }
                else if(key<arr[mid])
                {
                    return search(arr,key,low,mid-1);
                }
                else if(key>arr[mid])
                {
                    return search(arr,key,mid+1,high);
                }
               //// cout<<mid<<endl;
        }

如果测试数据是3(1,4,5)然后关键字是2,程序没有运行结果,自动跳走

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator