51NOD-1090 3个数和为0
来源:互联网 发布:ebody哪些好看知乎 编辑:程序博客网 时间:2024/06/08 02:46
1090 3个数和为0
基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
收藏
关注
给出一个长度为N的无序数组,数组中的元素为整数,有正有负包括0,并互不相等。从中找出所有和 = 0的3个数的组合。如果没有这样的组合,输出No Solution。如果有多个,按照3个数中最小的数从小到大排序,如果最小的数相等则按照第二小的数排序。
Input
第1行,1个数N,N为数组的长度(0 <= N <= 1000)第2 - N + 1行:A[i](-10^9 <= A[i] <= 10^9)
Output
如果没有符合条件的组合,输出No Solution。如果有多个,按照3个数中最小的数从小到大排序,如果最小的数相等则继续按照第二小的数排序。每行3个数,中间用空格分隔,并且这3个数按照从小到大的顺序排列。
Input示例
7-3-2-10123
Output示例
-3 0 3-3 1 2-2 -1 3-2 0 2-1 0 1
这道题我的做法是暴力解法:
#include<cstdio>//我的暴力解法 #include<algorithm>using namespace std;int main(){long long int a[1001];int n;while(~scanf("%d",&n)){int k=1;for(int i=0; i<n; i++){scanf("%lld",&a[i]);}sort(a,a+n);for(int i=0; i<n-3; i++){for(int j=i+1; j<n-2; j++){for(int l=j+1; l<n;l++){if(a[i]+a[j]+a[l]==0){printf("%lld %lld %lld\n",a[i],a[j],a[l]);k=0;}}}}if(k){printf("No Solution\n");}}return 0;}
#include <cstdio>//简单的二分解法 #include <stack>#include <queue>#include <cmath>#include <vector>#include <cstring>#include <algorithm>using namespace std;#define CLR(a,b) memset(a,b,sizeof(a))#define INF 0x3f3f3f3f#define LL long longint main(){int n;int num[1011];scanf ("%d",&n);for (int i = 1 ; i <= n ; i++)scanf ("%d",&num[i]);sort(num+1,num+1+n);num[n+1] = INF;//如果二分搜索溢出防止其取 0 bool flag = false;for (int i = 1 ; i < n ; i++){for (int j = i + 1 ; j <= n ; j++){if (*lower_bound(num+j+1,num+1+n,-num[i]-num[j]) == -num[i]-num[j]){printf ("%d %d %d\n",num[i],num[j],-num[i]-num[j]);flag = true;}}}if (!flag)puts("No Solution");return 0;}
阅读全文
0 0
- 51Nod-1090-3个数和为0
- 51NOD-1090 3个数和为0
- 3个数和为0 51Nod
- 51nod 1090 3个数和为0
- 51nod 1090 3个数和为0
- 51nod 1090 3个数和为0 (二分_stl)
- 51 nod 1090 3个数和为0
- 51nod 1090 3个数的和为0
- 51nod 1090 3个数和为0
- 51Nod 1090 3个数和为0
- 51nod 1090 3个数和为0(二分)
- 51nod-1090 3个数和为0
- 51nod-【1090 3个数和为0】
- 51nod 1090 3个数和为0
- 51NOD 1090 3个数和为0
- 51Nod 1090 3个数和为0 (二分
- 51nod 1090 3个数和为0(二分)
- 【51Nod】1090 3个数和为0
- 一个最简单的循环
- python里实现插入排序算法
- Linux学习路程(2)命令行模式
- [kuangbin带你飞]专题七 线段树 C
- Spring Data
- 51NOD-1090 3个数和为0
- 日本有剧情的av番号
- ZOJ 1156
- 二分图多重匹配
- pata1081(应该是long long类型却写成int类型也会超时)
- C# Dictionary 遍历删除
- 刷紫书第四章例题(例题4-1,4-2,4-3)
- XCode8升级到Xcode9(操作系统为iOS11)后原来的工程中遇到的问题
- SpringData之repository接口详解