Question: Output Based on the fruit sales table in a specific format
Format: sort by origin, and then sort by fruit name in the same origin.
Note: first, design multi-level sorting
Second, the same fruit in the same origin may appear multiple times, so you need to traverse the input fruit and find it once.
Third, here
# Include <iostream> # include <string> # include <algorithm> using namespace STD; int flag; // number of fruit types with different names in different regions struct node {string name, ID; int sum; bool operator <(const node & N) const {If (ID = n. ID) return name <n. name; else return id <n. id ;}} node [105]; void print (INT m) {for (INT I = 0; I <flag;) {string id = node [I]. ID; cout <id <Endl; cout <"| ----" <node [I]. name <'(' <node [I]. sum <')' <<Endl; I ++; while (ID = node [I]. ID & I <m) {cout <"| ----" <node [I]. name <'(' <node [I]. sum <')' <Endl; I ++ ;}} int main () {int n, m; CIN >>n; string name, ID; int sum; while (n --) {CIN> m; flag = 0; For (INT I = 0; I <m; I ++) {CIN> Name> ID> sum; Int J; For (j = 0; j <I; j ++) // search for the previous {If (name = node [J]. name & id = node [J]. ID) {node [J]. sum + = sum; break;} I F (I = J) // It is not found in the previous fruit, so it is a new fruit node [flag]. id = ID, node [flag]. name = Name, node [flag ++]. sum = sum;} Sort (node, node + flag); print (m); If (n! = 0) cout <Endl; // empty rows in the middle of the two cases} return 0 ;}