现在的位置: 首页 > 综合 > 正文

hdu – 1263 水果

2019年02月10日 ⁄ 综合 ⁄ 共 1374字 ⁄ 字号 评论关闭

题意:给定水果交易记录,按要求格式输出即可

题解:

用STL中的MAP

简单模拟;

为水果的产地和名字创建一个类, 这样水果就是唯一的, 然后映射这个水果的数量, 按要求输出即可;

//code

#include<iostream>
#include<map>
#include<cstring>
#include<cstdio>
using namespace std;


struct Fruit{
	
	
	char name[100];
	char from[100];
	friend bool operator == (Fruit a, Fruit b)
	{
		if(strcmp(a.name, b.name) == 0 && strcmp(a.from, b.from) == 0) return true;
		else return false;
	}
	friend bool operator < (Fruit a, Fruit b)
	{
		if(strcmp(a.from, b.from) == 0)
		{
			if(strcmp(a.name, b.name) == -1) return true;
			else return false;
		}
		else if(strcmp(a.from, b.from) == -1) return true;
		else return false;
	}
};


int main()
{
	
	int Case;
	scanf("%d", &Case);
	while(Case --)
	{
		int num;
		scanf("%d", &num);
		map<Fruit, int>myfruit;
		map<Fruit, int>::iterator it, tmp;
		char tmpname[100],tmpfrom[100], tmpcnt; 
		for(int i=0; i<num; i++)
		{
			scanf("%s%s%d", tmpname, tmpfrom, &tmpcnt);
			Fruit tmpfruit;
			strcpy(tmpfruit.from, tmpfrom);
			strcpy(tmpfruit.name, tmpname);
			for(it=myfruit.begin(); it!=myfruit.end(); it++)
			{
				if(it->first == tmpfruit)
				{
					it->second+=tmpcnt;
				}
			}
			if(it == myfruit.end()) myfruit.insert(map<Fruit, int> :: value_type(tmpfruit, tmpcnt));
		}
		
		
		for(it=myfruit.begin(); it!=myfruit.end(); it++)
		{
			if(it == myfruit.begin())
			{
				tmp = it;
				printf("%s\n",it->first.from);
				printf("   |----%s(%d)\n", it->first.name, it->second);
			}
			else
			{
				if(strcmp(it->first.from, tmp->first.from) == 0)
				{
					printf("   |----%s(%d)\n", it->first.name, it->second);
				}
				else
				{
					tmp = it;
					printf("%s\n",it->first.from);
					printf("   |----%s(%d)\n", it->first.name, it->second);
				}
			}
		}
		if(Case) printf("\n");
	}
	return 0;
}
【上篇】
【下篇】

抱歉!评论已关闭.