7-1挑选苹果

7-1 挑选苹果

果园采摘了n个苹果,分别放在若干个篮筐中。现给出n个苹果所在篮筐的情况,请找出每个篮筐中重量最重的苹果。定义一个苹果类Apple,有编号(id)、重量(weight)、直径(diameter)成员变量。

输入格式:

首先输入一个整型数n(1<=n<=999999),表示n个苹果。 紧跟着n行输入,每一行格式为:篮筐号,苹果编号(id),重量(weight),直径(diameter)。 篮筐号为整数,取值区间为[1,999999],id为字符串,weight、diameter为正整数。

输出格式:

按篮筐号从小到大排序,输出每个篮筐中重量最重的苹果信息。题目保证每个篮筐中只有一个重量最重的苹果。

输入样例:

7
1 N000001 175 77
2 N000002 180 83
2 N000003 160 66
1 N000004 160 63
1 N000005 165 68
4 N000006 183 85
2 N000007 170 74

输入样例:

1 N000001 175 77
2 N000002 180 83
4 N000006 183 85

C++:

#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
class Apple
{
private:
    
public:
    string id = "";
	int weight = 0;
	int diameter = 0;
	Apple();
	Apple(string , int, int);
	void printout();
	~Apple();
};

Apple::Apple()
{
}

Apple::~Apple()
{
}

Apple::Apple(const string x, const int y, const int z)
{
    this->id = x;
	this->weight = y;
	this->diameter = z;
}

void Apple::printout()
{
	cout<<this->id<<" ";
	cout<<this->weight<<" ";
	cout<<this->diameter<<endl;
}

int fi_Apple(const int n,const string s,vector<Apple>*A)
{
	for(int i=0;i<n;i++)
	{
		if(s == (*A)[i].id)
		{
			return i;
		}
	}
}

int main()
{
	int N = 0;
	cin >> N;
	map<int, string> Map;
	vector<Apple> List;
	int basket = 0;
	string s = "";
	int w = 0;
	int d = 0;
	for (int i = 0; i < N; i++)
	{
		cin >> basket;
		cin >> s;
		cin >> w >> d;
		List.push_back(Apple(s, w, d));		

		
		if (Map.find(basket) == Map.end())
		{
			Map[basket] = s;
		}
		else
		{
			if (w > List[fi_Apple(N , Map[basket], &List)].weight)
			{
				Map[basket] = s;
			}
		}
	}
	map<int, string>::iterator iter;
	for (iter = Map.begin(); iter != Map.end(); iter++)
	{
		cout << iter->first << " ";
		List[fi_Apple(N, iter->second, &List)].printout();
	}
	return 0;
}

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值