水果 HDU 1263

Problem Description
夏天来了~~好开心啊,呵呵,好多好多水果~~
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)
题目

这个倒是没有什么说的,依旧是结构体+自定义排序。

就是要注意同一产地的同一水果它可能出现很多次,然后我们就得在前面已经输入的水果在找一次。

注意最后的格式,输出一定要标准。

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

struct Node {
    char na[100], s[100];
    int x;
} a[1000];

int cmp(Node x, Node y) {
    if (strcmp(x.s, y.s))
        return strcmp(x.s, y.s) < 0;
    return strcmp(x.na, y.na) < 0;
}
int T, n, cnt, fg;
char d[100], Min[100];

int main() {
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &n);
        getchar();
        for (int i = 1; i <= n; i++) {
            scanf("%s%s%d", a[i].na, a[i].s, &a[i].x);
        }
        sort(a + 1, a + n + 1, cmp);

        cnt = 0, fg = 1;
        strcpy(d, a[0].s);
        strcpy(Min, a[0].na);
        for (int i = 1; i <= n; i++) {
            if (strcmp(d, a[i].s)) {
                strcpy(d, a[i].s);
                strcpy(Min, a[i].na);
                fg = 1;
                cnt = 0;
            }
            if (!strcmp(d, a[i].s)) {
                if (fg) {
                    printf("%s\n", d);
                    fg = 0;
                }
                if (!strcmp(Min, a[i].na)) {
                    while (!strcmp(Min, a[i].na) && !strcmp(d, a[i].s)) {
                        cnt += a[i].x;
                        i++;
                    }
                    printf("   |----%s(%d)\n", Min, cnt);
                    strcpy(Min, a[i].na);
                    i--;
                    cnt = 0;
                }
            }
        }
        if (T)
            cout << endl;
    }

    return 0;
}
结构体

 

posted @ 2021-01-15 12:36  月亮茶  阅读(49)  评论(0编辑  收藏  举报