序列中求指定和 原

阿豪boy

https://www.patest.cn/contests/pat-a-practise/1048

Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she could only use exactly two coins to pay the exact amount. Since she has as many as 105coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find two coins to pay for it.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (<=105, the total number of coins) and M(<=103, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers no more than 500. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the two face values V1 and V2 (separated by a space) such that V1 + V2 = M and V1 <= V2. If such a solution is not unique, output the one with the smallest V1. If there is no solution, output "No Solution" instead.

Sample Input 1:

```8 15
1 2 8 7 2 4 11 15```

Sample Output 1:

`4 11`

Sample Input 2:

```7 14
1 8 7 2 4 11 15```

Sample Output 2:

`No Solution`

``````#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
int n,sum,t;
vector<int> v;
scanf("%d %d",&n,&sum);
while(n--){
scanf("%d",&t);
v.push_back(t);
}

//由小到大排序
sort(v.begin(),v.end());

int i=0,j=v.size()-1;
while(i<j){
if(v[i]+v[j]==sum) break;
else if(v[i]+v[j]>sum) j--;
else i++;
}

if(i<j)
printf("%d %d\n",v[i],v[j]);
else
printf("No Solution\n");
return 0;
}``````

阿豪boy

itertools 我们知道，迭代器的特点是：惰性求值（Lazy evaluation），即只有当迭代至某个值时，它才会被计算，这个特点使得迭代器特别适合于遍历大文件或无限集合等，因为我们不用一次性将它...

funhacks
2017/02/13
0
0
python中itertools模块介绍---03

product(iterables[,repeat]): 源代码： def product(args,**kwds): pools=map(tuple,args)*kwds.get("repeat",1) result=[[]] for pool in pools: result=[x+[y] for x in result for y in......

2015/03/29
0
0
Dilworth定理 的相关应用

03/29
0
0

Garrry
2015/07/22
0
0
redis指令

2016/10/27
1
0

49.Nginx防盗链 访问控制 解析php相关 代理服务器

12.13 Nginx防盗链 12.14 Nginx访问控制 12.15 Nginx解析php相关配置（502的问题） 12.16 Nginx代理 扩展 502问题汇总 http://ask.apelearn.com/question/9109 location优先级 http://blog....

1
0
Nginx防盗链、访问控制、解析php相关配置、Nginx代理

0
0
spring EL 和资源调用

Canaan_

1
0
memcached命令行、memcached数据导出和导入

Zhouliang6

1
0
Linux定时备份MySQL数据库

1
1