1090 3个数和为0

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
-1
0
1
2
3
Output示例
-3 0 3
-3 1 2
-2 -1 3
-2 0 2
-1 0 1

二分答案
枚举前两个数 二分第三个数

 1 #include <cctype>
 2 #include <cstdio>
 3 #include <algorithm>
 4 
 5 const int MAXN=1010;
 6 
 7 int n;
 8 
 9 int a[MAXN];
10 
11 bool flag;
12 
13 inline void read(int&x) {
14     int f=1;register char c=getchar();
15     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar());
16     for(;isdigit(c);x=x*10+c-48,c=getchar());
17     x=x*f;
18 }
19 
20 inline bool check(int x) {
21     int l=0,r=n+1;
22     while(l+1<r) {
23         int mid=(l+r)>>1;
24         if(a[mid]>=x) r=mid;
25         else l=mid;
26     }
27     if(a[r]==x) return true;
28     return false;
29 }
30 
31 int hh() {
32     read(n);
33     for(int i=1;i<=n;++i) read(a[i]);
34     std::sort(a+1,a+1+n);
35     for(int i=1;i<=n;++i)
36       for(int j=i+1;j<=n;++j) {
37           int t=-a[i]-a[j];
38           if(t<=a[j]) continue;
39         if(check(t)) {
40               flag=true;
41               printf("%d %d %d\n",a[i],a[j],t);
42         }
43       }
44     if(!flag) printf("No Solution\n");
45     return 0;
46 }
47 
48 int sb=hh();
49 int main(int argc,char**argv) {;}
代码

 

1000以内所有各位数字之为n的正整数为: - 当n = 1时:1, 2, 3, 4, 5, 6, 7, 8, 9 - 当n = 2时:11, 20, 21, 30, 31, 40, 41, 50, 51, 60, 61, 70, 71, 80, 81, 90, 91 - 当n = 3时:101, 110, 111, 120, 121, 130, 131, 140, 141, 150, 151, 160, 161, 170, 171, 180, 181, 190, 191, 200, 201, 210, 211, 220, 221, 230, 231, 240, 241, 250, 251, 260, 261, 270, 271, 280, 281, 290, 291, 300, 301, 310, 311, 320, 321, 330, 331, 340, 341, 350, 351, 360, 361, 370, 371, 380, 381, 390, 391 - 当n = 4时:1001, 1010, 1011, 1020, 1021, 1030, 1031, 1040, 1041, 1050, 1051, 1060, 1061, 1070, 1071, 1080, 1081, 1090, 1091, 1100, 1101, 1110, 1111, 1120, 1121, 1130, 1131, 1140, 1141, 1150, 1151, 1160, 1161, 1170, 1171, 1180, 1181, 1190, 1191, 2000, 2001, 2010, 2011, 2020, 2021, 2030, 2031, 2040, 2041, 2050, 2051, 2060, 2061, 2070, 2071, 2080, 2081, 2090, 2091 - 当n = 5时:10001, 10010, 10011, 10020, 10021, 10030, 10031, 10040, 10041, 10050, 10051, 10060, 10061, 10070, 10071, 10080, 10081, 10090, 10091, 10100, 10101, 10110, 10111, 10120, 10121, 10130, 10131, 10140, 10141, 10150, 10151, 10160, 10161, 10170, 10171, 10180, 10181, 10190, 10191, 11000, 11001, 11010, 11011, 11020, 11021, 11030, 11031, 11040, 11041, 11050, 11051, 11060, 11061, 11070, 11071, 11080, 11081, 11090, 11091 - 当n = 6时:100001, 100010, 100011, 100020, 100021, 100030, 100031, 100040, 100041, 100050, 100051, 100060, 100061, 100070, 100071, 100080, 100081, 100090, 100091, 100100, 100101, 100110, 100111, 100120, 100121, 100130, 100131, 100140, 100141, 100150, 100151, 100160, 100161, 100170, 100171, 100180, 100181, 100190, 100191, 101000, 101001, 101010, 101011, 101020, 101021, 101030, 101031, 101040,这是1000以内所有各位数字之为n的正整数的列表。每个n对应的列表中都包含了所有各位数字之为n的正整数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值