AcWing 1532. 找硬币
原题链接
简单
作者:
满目星河_0
,
2021-06-04 21:49:38
,
所有人可见
,
阅读 276
排序+双指针实现
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e5+10;
int num[N];
bool judge(int a,int b){
int i=1,j=a;
while(i<j){
if(num[i]+num[j]>b){
j--;
}
else if(num[i]+num[j]<b){
i++;
}
else{
cout<<num[i]<<" "<<num[j]<<endl;
return true;
}
}
return false;
}
int main(){
int a,b;
scanf("%d%d",&a,&b);
for(int i=1;i<=a;i++){
scanf("%d",&num[i]);
}
sort(num+1,num+a+1);
if(!judge(a,b)) cout<<"No Solution"<<endl;
return 0;
}