合理吗?

d51020 2010-05-15 08:25:10
家来看看顺序合理吗?

大#include "stdafx.h"

using namespace System;
//对数组进行二叉搜索
void ShowOutput(int arr[],int low,int mid,int high,int size1)
{
for(int i=0;i<size1;i++){
if(i<low||i>high)
Console::Write(L" ");
else if(i==mid)
Console::Write(L"{0}* ",arr[i].ToString("00"));
else
Console::Write(L"{0} ",arr[i].ToString("00"));
}
Console::WriteLine();
}

int BinarySearch(int arr[],int key,int size2)
{
int low=0,high=size2-1,middle;
while(low<=high){
middle=(low+high)/2;//如结果是偶数,取2个较小的索引值
ShowOutput(arr,low,middle,high,size2);
if(key==arr[middle])
return middle;
else if(key<arr[middle])
high=middle-1;
else
low=middle+1;
}
return -1;
}

int main(array<System::String ^> ^args)
{
int a[]={0,2,4,6,8,10,12,14,16,18,20,22,24,26,28};
int size3=sizeof(a)/sizeof(int);
Console::Write(L"Please enter a search key:");
int searchKey=Int32::Parse(Console::ReadLine());
Console::WriteLine(L"\nPortions of array searched");
int element=BinarySearch(a,searchKey,size3);
if(element!=-1)
Console::WriteLine(L"\nFound value in element {0}",element.ToString());
else
Console::WriteLine(L"\nValue not found");
return 0;
}//vs2008
...全文
89 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
d51020 2010-05-20
  • 打赏
  • 举报
回复
这段代码运行完全正常。代码顺序很重要,不能错的否则程序不能正常运行。我这样做感觉最精简。
canxue418 2010-05-20
  • 打赏
  • 举报
回复
帮你顶下!
皇城龙三 2010-05-19
  • 打赏
  • 举报
回复
顺序无所谓,关键看运行起来正常不?

帮你顶一下
d51020 2010-05-19
  • 打赏
  • 举报
回复
没人顶啊,给分的呀

7,540

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧