51nod 1090 3个数和为0

暴力水过

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+10;

int s[maxn];

bool b_s(int l,int r,int t)
{
    int ans = -1;
    while (l <= r)
    {
        int mid = (l+r)/2;
        if(s[mid] >= t)
            ans = mid,r=mid-1;
        else
            l=mid+1;
    }
    if(s[ans] == t)
        return 1;
    else
        return 0;
}

int main ()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&s[i]);
    }
    sort(s+1,s+n+1);
    //for(int i=1;i<=n;i++)
       // printf("%d ",s[i]);
       // puts("");
    int ans = 0;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            int t = -(s[i] + s[j]);
            bool flag = b_s(j+1,n,t);
            if(flag)
                printf("%d %d %d\n",s[i],s[j],t),ans++;
        }
    }
    if(ans == 0)
        puts("No Solution");

}

 

posted @ 2017-08-17 15:54  Draymonder  阅读(126)  评论(0编辑  收藏  举报