写一个功能函数实现从数组中找出两个值相加等于某一个值,要求时间复杂度为 n;

来源:互联网 发布:ubuntu 命令行界面 编辑:程序博客网 时间:2024/05/18 01:28
#include<iostream>
usingnamespacestd;
voidFunc(intarray[],intlength,intthenumberint&num1int&num2)
{
       inthead = 0;
       inttail =length- 1;
       while(head != tail)
       {
              inttemp =array[head] +array[tail];
              if(temp ==thenumber)
              {
                     num1=array[head];
                     num2=array[tail];
                     cout<<num1<<'\t'<<num2<<endl;
                     head++;
                     tail--;
                     continue;
              }
              if(temp >thenumber)
                     tail--;
              else
                     head++;
       }
}
intmain()
{
       intabc[] = { 0,1,3,5,6,8,9,11,12,13,14,15,16 };
       intlength =sizeof(abc) /sizeof(abc[0]);
       inta, b;
       Func(abc, length,15, a, b);
       getchar();
   return0;
}
阅读全文
1 0
原创粉丝点击