
散列解法:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,m;
std::ios::sync_with_stdio(false);
cin>>n>>m;
int ha[1005]={ 0};
for(int i=0;i<n;i++)
{
int temp;cin>>temp;
ha[temp]++;
}
for(int i=1;i<=m/2;i++)
{
if(ha[i]&&ha[m-i])
{
if(i==m-i&&ha[i]<2)continue;
cout<<i<<" "<<m-i;return 0;
}
}
cout<<"No Solution";
return 0;
}

two pointer解法:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,m;
std::ios::sync_with_stdio(false);
cin>>n>>m;
int temp[n+1];
for(int i=0;i<n;i++)cin>>temp[i];
sort(temp,temp+n);
int i=0,j=n-1;
while(i<j)
{
if(temp[i]+temp[j]==m)
{
cout<<temp[i]<<" "<<temp[j];return 0;
}
else if(temp[i]+temp[j]>m)j--;
else i++;
}
cout<<"No Solution";
return 0;
}

还没有评论,来说两句吧...