#include <iostream> #include <string> #include <algorithm> #define MAXN 105 using namespace std; struct node { string name_f; string name; int f_c; int age; }; node _m[MAXN]; int find_pre(string _name); int n; bool op(node a,node b); int main() { //freopen("acm.acm","r",stdin); int test; int i; int tem; cin>>test; tem = test; while(test --) { cout<<"DATASET "<<tem - test<<endl; cin>>n; for(i = 0; i < n; ++ i) { cin>>_m[i].name_f; cin>>_m[i].name; cin>>_m[i].f_c; } for(i = 0; i < n; ++ i) { _m[i].age = -1; } for(i = 0;i < n; ++ i) { if(_m[i].name_f == "Ted") { _m[i].age = 100 - _m[i].f_c; } else _m[i].age = find_pre(_m[i].name_f) - _m[i].f_c; } sort(_m,_m+n,op); for(i = 0; i < n; ++ i) { cout<<_m[i].name<<" "<<_m[i].age<<endl; } } } bool op(node a,node b) { if(a.age > b.age) return true; else if(a.age == b.age) { if(a.name < b.name) { return true; } } return false; } int find_pre(string _name) { int i; if(_name == "Ted") { return 100; } for(i = 0; i < n; ++ i) { if(_m[i].name == _name) { if(_m[i].age != -1) { return _m[i].age; } _m[i].age = find_pre(_m[i].name_f) - _m[i].f_c; return _m[i].age; } } }
时间: 2024-11-05 18:35:31