3-sum问题 http://en.wikipedia.org/wiki/3SUM #includecstdio#includecstring#includealgorithmusing namespace std;#define N 1010int a[N];int main(){int i,j;int n;while(scanf("%d",n)){ if(n==0)break; for(i=0;in;i++) scanf("%d",a[i]); sort(a,a+
3-sum问题
http://en.wikipedia.org/wiki/3SUM
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1010
int a[N];
int main(){
int i,j;
int n;
while(scanf("%d",&n)){
if(n==0)break;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
bool flag=0;
for(i=n-1;i>=0;i--){
for(j=0;j<n;j++){
if(i==j)continue;
int l=j+1,r=n-1;
if(l==i)l++;
if(r==i)r--;
while(l<r){
if(a[j]+a[l]+a[r]==a[i]){
flag=1;
break;
}
else if(a[j]+a[l]+a[r]>a[i])
r--;
else l++;
if(l==i)l++;
if(r==i)r--;
}
if(flag)break;
}
if(flag)break;
}
if(flag) printf("%d\n",a[i]);
else printf("no solution\n");
}
return 0;
}