找到符合条件的对数

#include<iostream>
#include<algorithm>
#include<numeric>
using namespace std;

int helper(int a[],int n, int t)
{
  sort(a,a+n);
  int k = 0;
  int left = 0;
  int right = n-1;
  while(left < right)
  {
    int sumtwo = a[left] + a[right];
    if(sumtwo == t)
    {left++;right--;k++;}
    else if(sumtwo>t)
    right--;
    else
     left++;
  }
  return k;
}

int main()
{
    int a[] = {1,2,3,4,5};
    cout<<helper(a,5,6);
}

 

posted on 2014-05-18 21:44  berkeleysong  阅读(124)  评论(0编辑  收藏  举报

导航