暑期集训1:C++&STL 练习题C:HDU-1263

2018学校暑期集训第一天——C++与STL

练习题C ——   HDU - 1263 

C - 亡失流转的孤独

夏天来了~~好开心啊,呵呵,好多好多水果~~ 
Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样Joe就可以很容易掌握所有水果的销售情况了. 

Input

第一行正整数N(0<N<=10)表示有N组测试数据. 
每组测试数据的第一行是一个整数M(0<M<=100),表示工有M次成功的交易.其后有M行数据,每行表示一次交易,由水果名称(小写字母组成,长度不超过80),水果产地(小写字母组成,长度不超过80)和交易的水果数目(正整数,不超过100)组成. 

Output

对于每一组测试数据,请你输出一份排版格式正确(请分析样本输出)的水果销售情况明细表.这份明细表包括所有水果的产地,名称和销售数目的信息.水果先按产地分类,产地按字母顺序排列;同一产地的水果按照名称排序,名称按字母顺序排序. 
两组测试数据之间有一个空行.最后一组测试数据之后没有空行. 

Sample Input

1
5
apple shandong 3
pineapple guangdong 1
sugarcane guangdong 1
pineapple guangdong 3
pineapple guangdong 1

Sample Output

guangdong
   |----pineapple(5)
   |----sugarcane(1)
shandong
   |----apple(3)

我的答案:使用结构体和sort函数,极其显示出本菜鸡的弱……

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
using namespace std;

struct Point {
	string place;
	string fruit;
	int number;
}p[105];

bool com(Point lhs, Point rhs)
{
	if ((lhs.place).compare(rhs.place) < 0)
		return true;
	else if (lhs.place == rhs.place)
			if ((lhs.fruit).compare(rhs.fruit) < 0)
				return true;
	return false;
}


int main(void)
{
	int n;
	cin >> n;
	while (n--) {
		int t;
		cin >> t;
		int count = 0;
		for (int i = 0; i < t - count; ) {
			bool judge = 1;
			cin >> p[i].fruit >> p[i].place >> p[i].number;
			for (int j = 0; j < i; j++) {
				if (p[i].fruit == p[j].fruit && p[i].place == p[j].place) {
					count++;
					p[j].number += p[i].number;
					judge = 0;
					break;
				}
			}
			if (judge == 0)
				continue;
			i++;
		}
		
		sort(p, p + t - count, com);

		cout << p[0].place << endl;
		cout << "   |----" << p[0].fruit << "(" << p[0].number << ")" << endl;
		for (int i = 1; i < t - count; i++) {
			if(p[i].place==p[i-1].place)
				cout << "   |----" << p[i].fruit << "(" << p[i].number << ")" << endl;
			else {
				cout << p[i].place << endl;
				cout << "   |----" << p[i].fruit << "(" << p[i].number << ")" << endl;
			}
		}
		if (n >= 1)
			cout << endl;
	}

	return 0;
}

教练答案:使用map合理处理……这题还是要有很好的思路才行……

#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

int main(int argc, char *argv[]) {
    map<string, map<string, int> > mp;
    map<string, map<string, int> >::iterator outerIt;
    map<string, int>::iterator innerIt;
    string fruit, location;
    int t, m, n;

    scanf("%d", &t);
    for (int i = 0; i < t; i++) {
        scanf("%d", &m);
        while (m--) {
            cin >> fruit >> location >> n;
            mp[location][fruit] += n;
        }
        for (outerIt = mp.begin(); outerIt != mp.end(); outerIt++) {
            cout << outerIt->first << endl;
            for (innerIt = outerIt->second.begin(); innerIt != outerIt->second.end(); innerIt++) {
                cout << "   |----" << innerIt->first << "(" << innerIt->second << ")" << endl;
            }
        }
        if (i < t - 1) {
            cout << endl;
        }
        mp.clear();
    }

    return 0;
}

 

全部评论

相关推荐

头像
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务