bool find(int *a,int len,int sum)
{
bool found=false;
if(len<2)
return found;
int beg=0;
int end=len-1;
while(beg<end)
{
int cursum=a[beg]+a[end];
if(cursum==sum)
{
cout<<a[beg]<<" "<<a[end]<<endl;
found=true;
break;
}
else if(cursum>sum)
{
--end;
}
else
++beg;
}
return found;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[]={1,4,5,7,8,11,13,15};
int len=sizeof(a)/sizeof(int);
find(a,len,12);
return 0;
}
posted on 2012-05-22 16:46
qiushao 阅读(185)
评论(0) 编辑 收藏 引用