题目:http://vj.bit-studio.cn/contest/209424#problem/C
方法:本来用把产地与水果名字用结构体包装起来,然后再把数量与结构体map起来,纠结于map怎么排序,后来发现用结构体更容易
代码:
#include<iostream> #include<map> #include<cstdio> #include<cstdlib> using namespace std; typedef struct flu { char name[81]; char place[81]; int shuliang; }flue; bool cmp(const flue&f1, const flue&f2) { return f1.name > f2.name; } int main() { int n; scanf("%d", &n); for (int l = 0; l < n; l++) { if (l != 0)cout << endl; int m; scanf("%d", &m); flue *fff=new flue [m]; for (int j = 0; j < m; j++) { flue temp; scanf("%s", temp.name); scanf("%s", temp.place); scanf("%d", &temp.shuliang); bool flag = false; for (int k = 0; k < j - 1; k++) { if ((strcmp(temp.name, fff[k].name) == 0) && (strcmp(temp.place, fff[k].place)) == 0) { flag = true; fff[k].shuliang += temp.shuliang; m--; j--; } } if(flag==false)fff[j] = temp; } for (int i = 0; i < m; i++) { for (int j = i+1; j < m; j++) { if (strcmp(fff[i].place, fff[j].place)) { if (strcmp(fff[i].place, fff[j].place) > 0) { swap(fff[i], fff[j]); } } else { if (strcmp(fff[i].name, fff[j].name) > 0) { swap(fff[i], fff[j]); } } } } char tem[81]; strcpy(tem , fff[0].place); cout << fff[0].place << endl; for (int i = 0; i < m; i++) { if (strcmp(fff[i].place, tem) == 0) { cout << " |----" << fff[i].name << "(" << fff[i].shuliang << ")" << endl; } else { strcpy(tem, fff[i].place); cout << fff[i].place << endl; cout << " |----" << fff[i].name << "(" << fff[i].shuliang << ")" << endl; } } } }