51Nod 1090 3个数和为0

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1090

思路:排序 三个for循环 但是要控制循环 不能从头开始遍历 会超时

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 #include<cstring>
 5 using namespace std;
 6 int a[1005];
 7 int main()
 8 {
 9     int n;
10     cin>>n;
11     for(int i=0;i<n;i++) cin>>a[i];
12     sort(a,a+n);
13     int flag=0;
14     for(int i=0;i<n-2;i++){
15         if(a[i]>0) break;
16         for(int j=i;j<n-1;j++){
17             for(int k=j;k<n;k++){
18                 if(i!=j&&j!=k&&i!=k&&a[i]+a[j]+a[k]==0&&a[i]<a[j]&&a[j]<a[k]){
19                     cout<<a[i]<<" "<<a[j]<<" "<<a[k]<<endl;
20                     flag=1;
21                 }
22             }
23         }
24     }
25     if(!flag) cout<<"No Solution"<<endl;
26     return 0;
27 }

 

posted @ 2018-05-19 22:22  wydxry  阅读(261)  评论(0)    收藏  举报
Live2D