• 主页

# 关于二分法的点小问题

longjushentan 2009-08-04 10:39:04
``````int SerchUserNum(CStringArray& UserNumArray, int nlow, int nhigh, CString strUserNum)
{
int nMid = (nlow+nhigh)/2;

if (atoi(strUserNum) == atoi(UserNumArray.GetAt(nMid)))
{
TRACE("´æÔÚ");
return atoi(strUserNum);
}
else if (atoi(strUserNum) > atoi(UserNumArray.GetAt(nMid)))
{
return (SerchUserNum(UserNumArray,nMid,nhigh,strUserNum));
}
else if (atoi(strUserNum) < atoi(UserNumArray.GetAt(nMid)))
{
return (SerchUserNum(UserNumArray,nlow,nMid,strUserNum));
}
}``````

...全文
29 点赞 收藏 4

4 条回复

longjushentan 2009-08-04
``````int SerchUserNum(CStringArray& UserNumArray, int nlow, int nhigh, CString strUserNum)
{
int nMid = (nlow+nhigh)/2;
if (nMid == nhigh)
{
if (atoi(strUserNum) == atoi(UserNumArray.GetAt(nMid)))
{
return 0;
}
else
return -1;
}

if (atoi(strUserNum) == atoi(UserNumArray.GetAt(nMid)))
{
return 0;
}
else if (atoi(strUserNum) > atoi(UserNumArray.GetAt(nMid)))
{
return (SerchUserNum(UserNumArray,nMid+1,nhigh,strUserNum));
}
else if (atoi(strUserNum) < atoi(UserNumArray.GetAt(nMid)))
{
return (SerchUserNum(UserNumArray,nlow,nMid-1,strUserNum));
}
return -1;
}``````

flyskytoday 2009-08-04

else if (atoi(strUserNum) > atoi(UserNumArray.GetAt(nMid)))
{
return (SerchUserNum(UserNumArray,nMid+1,nhigh,strUserNum));//+1
}
else if (atoi(strUserNum) < atoi(UserNumArray.GetAt(nMid)))
{
return (SerchUserNum(UserNumArray,nlow,nMid-1,strUserNum));//-1
}

flyskytoday 2009-08-04

else if (atoi(strUserNum) < atoi(UserNumArray.GetAt(nMid)))
{
return (SerchUserNum(UserNumArray,nlow,nMid,strUserNum));
}
return 0;//加一个返回，表示找不到
}

millky 2009-08-04

VC/MFC

1.5w+

VC/MFC相关问题讨论