51Nod

来源:互联网 发布:更改防火墙端口 编辑:程序博客网 时间:2024/05/16 17:43

给出一个长度为N的无序数组,数组中的元素为整数,有正有负包括0,并互不相等。从中找出所有和 = 0的3个数的组合。如果没有这样的组合,输出No Solution。如果有多个,按照3个数中最小的数从小到大排序,如果最小的数相等则按照第二小的数排序。
Input
第1行,1个数N,N为数组的长度(0 <= N <= 1000) 
第2 - N + 1行:Aii(-10^9 <= Aii <= 10^9)
Output
如果没有符合条件的组合,输出No Solution。 
如果有多个,按照3个数中最小的数从小到大排序,如果最小的数相等则继续按照第二小的数排序。每行3个数,中间用空格分隔,并且这3个数按照从小到大的顺序排列。
Sample Input
7-3-2-10123
Sample Output
-3 0 3-3 1 2-2 -1 3-2 0 2-1 0 1

#include<cstdio>#include<algorithm>#include<iostream>using namespace std;long long int a[1521];//N只有1000个,所以可以用暴力解决//int main(){int N;cin >> N;int ans = 1;for(int i = 1 ;i <= N; i++){cin >> a[i];}sort(a+1,a+N+1);//默认从小到大排序// for(int i = 1 ;i <= N; i++){for(int j = i+1 ; j <= N;j++){for(int k = j + 1 ;k <= N;k++){if(a[i] + a[j] + a[k] == 0){cout << a[i] <<" "<<a[j]<<" "<< a[k] <<endl; ans++;}}}}if(ans == 1)cout <<"No Solution"<<endl;return 0;} 

原创粉丝点击