(使用STL自带的排序功能进行排序7.3.12)UVA 11057 Exact Sum(在一组数据中找到这么两个数,使得他们的和等于指定数&&且这两个数之间的差值最小)

/*
 * UVA_11057.cpp
 *
 *  Created on: 2013年11月4日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <algorithm>


using namespace std;

const int maxn = 100010;

int leftt;
int rightt;
int a[maxn];
int n,m;

/**
 * 在一组数据中找到这么两个数,使得他们的和等于指定数&&且这两个数之间的差值最小
 */
void search(int l,int r){
	while(l < r){
		if(a[l] + a[r] == m){
			leftt = a[l];
			rightt = a[r];

			++l;
			--r;
		}else if(a[l] + a[r] < m){
			++l;
		}else if(a[l] + a[r] > m){
			--r;
		}
	}
}

int main(){
	while(scanf("%d",&n)!=EOF){
		leftt = 0;//这里不要定义成left,否则可能会出现什么。。。ambiguous的错误(这是因为你在这里定义的变量与标准库或者是其他地方的变量冲突了...)
		rightt = 0;

		int i;
		for(i = 0 ; i < n ; ++i){
			scanf("%d",&a[i]);
		}

		sort(a,a+n);

		scanf("%d",&m);

		search(0,n-1);

		printf("Peter should buy books whose prices are %d and %d.\n\n",leftt,rightt);
	}

	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Python,如果你使用priority_queue存储的是对象,你可以通过自定义排序函来实现对priority_queue的排序。你可以使用heapq模块的方法来实现堆操作,并使用自定义的排序函指定对象的优先级。 举个例子,假设你有一个存储Person对象的priority_queue,你可以定义一个比较函指定Person对象的排序规则。比较函应该返回一个负、零或正,分别表示第一个对象小于、等于或大于第二个对象。 下面是一个示例代码,演示了如何在Python使用priority_queue对Person对象进行排序: ```python import heapq class Person: def __init__(self, name, age): self.name = name self.age = age def __lt__(self, other): # 自定义对象的排序规则,比较年龄大小 return self.age < other.age # 创建一个存储Person对象的priority_queue priority_queue = [] # 添加一些Person对象到priority_queue heapq.heappush(priority_queue, Person('Alice', 25)) heapq.heappush(priority_queue, Person('Bob', 30)) heapq.heappush(priority_queue, Person('Charlie', 20)) # 从priority_queue弹出对象,按照自定义排序规则输出 while priority_queue: person = heapq.heappop(priority_queue) print(person.name, person.age) ``` 输出结果会按照Person对象的age属性进行排序,从小到大输出。在这个例子,我们重载了Person类的`__lt__`方法来定义对象的排序规则,使得priority_queue按照年龄的大小进行排序。 希望这个例子能帮助你理解如何在Python使用priority_queue对对象进行排序。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [STL priority_queue自定义排序实现方法详解](https://blog.csdn.net/weixin_39930557/article/details/111521369)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [[python3] 堆 优先队列(priorityqueue) heapq模块](https://blog.csdn.net/qq_35630119/article/details/119520033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Python(一)数据结构和算法的20个练习题问答](https://blog.csdn.net/weixin_39882948/article/details/110994081)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值