找礼物 C++


——————————————————————————————————

题目描述

新年到了,突然间,就在那美丽的一霎那,你好友和你(K个人)的周围满是礼物,你发扬你帅气的风格,让你的好友先拿,但是每个人只能拿当前离自己最近的礼物[当然如果有并列的多个礼物离你的距离相等(精确到小数后四位,所有运算均为去尾),这些礼物就都属于这个人]。现在你们所在的位置是原点(0,0),每个礼物的位置用坐标表示。现在告诉你每个礼物的坐标,还有每个礼物是谁送的。要你找出你的礼物离你多远,你能拿到多少礼物,这些礼物是谁送的。如果你拿不到礼物,请输出“555…”

——————————————————————————————————

输入输出格式

输入格式:
第一行:N 和 K 分别表示礼物的个数和人数。
第二到N+1行:每行先是赠送礼品人的姓名,然后是礼物的坐标(x,y)。
数据间空格分割

输出格式:
第一行: D 和 U 表示礼物距你多远(只要去尾后的整数)和你能拿到多少礼物。
第二到U+1行:每行一个人名,表示送礼的人。<按照输入的顺序输出>

——————————————————————————————————

输入输出样例

sample 1

输入样例1:

5 2
Jason 1 1
Herry 4 4
Patty 3 4
Tom 2 10
Peter 5 10

输出样例1:

5 1
Patty

sample 2

输入样例2:

6 2
Jim 1 -1
Flord 3 -3
Joseph -1 1
Steve 3 3
Tiger 2 -10
User 10 20

输出样例2:

4 2
Flord
Steve

——————————————————————————————————


——————————————————————————————————

分析

题目简洁易懂,输入,排序,模拟,输出
输入时求出距离(dis),根据dis和姓名进行排序,然后模拟每个人可以拿到的礼物,最后输出可以得到的礼物的距离和数量,并循环输出送礼物的人名

——————————————————————————————————

Code

#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
using namespace std;
struct liwu
{
	string name;
	int x,y,xuhao;
	long long d;
}w[100000];
void qsort(int left,int right)
{
	int i,j,xh;
	long long m;
	i=left;
	j=right;
	m=w[(i+j)/2].d;
	xh=w[(i+j)/2].xuhao;
	do
	{
		while(w[i].d<m||w[i].d==m&&w[i].xuhao<xh)
			i++;
		while(w[j].d>m||w[j].d==m&&w[j].xuhao>xh)
			j--;
		if(i<=j)
		{
			swap(w[i],w[j]);
			i++;
			j--;
		}
	}while(i<=j);
	if(j>left)
		qsort(left,j);
	if(i<right)
		qsort(i,right);
}
long long total=0,a[1000],dis;
int main()
{
	int i,j,n,k;
	cin>>n>>k;
	for(i=0;i<n;i++)
	{
		cin>>w[i].name>>w[i].x>>w[i].y;
		w[i].xuhao=i+1;
		w[i].d=(sqrt((double)w[i].x*w[i].x+(double)w[i].y*w[i].y))*10000;
	}
	qsort(0,n-1);
	dis=w[0].d;
	total=1;
	a[0]=0;
	for(i=1;i<n;i++)
	{
		if(w[i].d==w[i-1].d)
			a[total++]=i;
		else
		{
			k--;
			if(k==0)
				break;
			a[0]=i;
			total=1;
			dis=w[i].d;
		}
	}
	if(k>1)
		cout<<"555...";
	else
	{
		cout<<dis/10000<<" "<<total<<endl;
		for(i=0;i<total;i++)
			cout<<w[a[i]].name<<endl;
	}
	return 0;
}

· 这里我手打了一个快排,实际上用系统自带sort(,,cmp)即可
——————————————————————————————————

### C++ 实现补发礼物功能 在设计C++中的补发礼物功能时,可以考虑创建一个礼品管理系统。该系统能够记录已发送的礼物以及未成功送达的情况,并提供重新尝试发送的功能。 #### 数据结构定义 为了管理礼物的信息,首先需要定义相应的数据结构: ```cpp struct Gift { std::string id; std::string recipientId; std::string senderId; bool isSentSuccessfully; Gift(std::string _id, std::string _recipientId, std::string _senderId) : id(_id), recipientId(_recipientId), senderId(_senderId), isSentSuccessfully(false) {} }; ``` #### 礼物服务类 接着构建一个处理礼物逻辑的服务类 `GiftService` ,其中包含了发送礼物的方法和重试机制: ```cpp class GiftService { public: void sendGift(const Gift& gift); void resendUnsentGifts(); private: std::vector<Gift> unsentGifts_; }; void GiftService::sendGift(const Gift& gift) { // Simulate sending process here. // If failed to send, add it into the list of unsent gifts. if (!simulateSendProcess()) { // Assume this function simulates actual network operation or other operations that may fail. unsentGifts_.push_back(gift); } } void GiftService::resendUnsentGifts() { for(auto& gift : unsentGifts_) { if(simulateSendProcess()){ gift.isSentSuccessfully = true; } } // Remove successfully resent items from vector after attempting resends. unsentGifts_.erase( std::remove_if(unsentGifts_.begin(), unsentGifts_.end(), [](const Gift& g){return g.isSentSuccessfully;}), unsentGifts_.end()); } ``` 上述代码展示了如何通过维护一个待发送列表来追踪未能初次成功的礼物传输请求,并提供了再次尝试这些失败项的机会[^1]。 #### 使用示例 最后,在主函数或其他调用位置实例化并使用此服务来进行礼物的操作: ```cpp int main(){ GiftService service; // Create some test gifts and attempt initial delivery. service.sendGift(Gift("gift_001", "user_A", "user_B")); service.sendGift(Gift("gift_002", "user_C", "user_D")); // Resend any previously unsuccessful deliveries. service.resendUnsentGifts(); return 0; } ``` 这种模式不仅适用于简单的命令行应用程序,也可以扩展应用于更复杂的应用场景中,比如网络应用或分布式系统内的消息传递等。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值