序列中求指定和
序列中求指定和
阿豪boy 发表于9个月前
序列中求指定和
  • 发表于 9个月前
  • 阅读 0
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

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;
}

 

共有 人打赏支持
粉丝 3
博文 464
码字总数 352674
×
阿豪boy
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: