sincerit-1263 水果

1263 水果
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11439 Accepted Submission(s): 4586

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)

使用优先队列, 对结构体排序
// 在结构体外写重载方法
bool operator <(const node& a, const node& b){
if (a.origin != b.origin) return a.origin > b.origin;
else if (a.origin == b.origin) return a.fruit > b.fruit;
}
// 在结构体内重载符号
bool operator < (const node& x)const{
if (origin == x.origin) return fruit > x.fruit;
if (origin != x.origin) return origin > x.origin;
}
定义 priority_queue pq;

#include <iostream>
#include <stdio.h>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
struct node{
  string origin;
  string fruit;
  int data;
  bool operator < (const node& x)const{
      if (origin == x.origin) return fruit > x.fruit;
      if (origin != x.origin) return origin > x.origin; 
  }
};
bool cmp(node a, node b) {
  if (a.origin == b.origin && a.fruit == b.fruit) return true;
  return false;
}
/*
bool operator <(const node& a, const node& b){
    if (a.origin != b.origin) return a.origin > b.origin;
    else if (a.origin == b.origin) return a.fruit > b.fruit;
}*/
int main() {
  int n, m;
  cin >> n;
  while (n--) {
    cin >> m;
    string origin, fruit;
    int x;
    priority_queue<node> pq;
    for (int i = 0; i < m; i++) {
       cin >> fruit >> origin >> x;
       node k;
       k.origin = origin; k.fruit = fruit; k.data = x;
       pq.push(k);
    }
    int k = 0;
    node info[105];
    info[0] = pq.top();
    pq.pop();
    while (pq.size()) {
      if (cmp(pq.top(), info[k]))  info[k].data += pq.top().data;
      else info[++k] = pq.top();
      pq.pop();
    }
    origin = info[0].origin;
    cout << origin << "\n";
    cout << "   |----" << info[0].fruit << "(" << info[0].data << ")\n";
    for (int i = 1; i <= k; i++) {
      if (info[i].origin == origin) {
        cout << "   |----" << info[i].fruit << "(" << info[i].data << ")\n";
      } else {
        cout << info[i].origin << "\n";
        cout << "   |----" << info[i].fruit << "(" << info[i].data << ")\n";
        origin = info[i].origin;
      }
      
    }
    if (n) cout << "\n";
  }
  return 0;
}

猜你喜欢

转载自blog.csdn.net/sincerit/article/details/83064667
今日推荐