Untitled
Never
#include <iostream> #include <string> #include <algorithm> using namespace std; enum position{ FLOOR, STOMACH, TRASHCAN }; class Food; class Fruit; class Meat; class Food{ protected: string name; int fly; int pos; public: // constructor Food(); Food(string&); // copy constructor Food(const Food&); // functions // eaten: the food is eaten, change its position // thrown: the food is thrown, change its position // on_floor: return whether the food is on the floor // get_name: return the Food's name // get_fly: return the amount of the fly void eaten(); void thrown(); bool on_floor(); string get_name(); int get_fly(); // operators // < : use this operator to sort the food so the output of on-floor food satisfy the requirement // == : determine whether the food's name is same as a string // << : output the food as the mentioned output format bool operator < (const Food&); bool operator == (const string&); friend ostream& operator << (ostream&, Food); }; class Fruit: public Food{ public: Fruit(); Fruit(string&); // five_min_pass: 5 min passes // sickness: return whether the fruit makes someone sick // += : mix the food together void five_min_pass(); bool sickness(); void operator += (Fruit&); void operator += (Meat&); }; class Meat: public Food{ public: Meat(); Meat(string&); // five_min_pass: 5 min passes // sickness: return whether the meat makes someone sick // += : mix the food together void five_min_pass(); bool sickness(); void operator += (Fruit&); void operator += (Meat&); }; Meat meat[105]; Fruit fruit[105]; int meat_count, fruit_count; int get_index(string n, string k){ if(k == "fruit"){ for(int i = 0; i < fruit_count; i++){ if(fruit[i] == n) return i; } } else{ for(int i = 0; i < meat_count; i++){ if(meat[i] == n) return i; } } return 0; } int main(){ int t; cin >> t; while(t--){ int n; cin >> n; while(n--){ string s, n1, n2, k1, k2; cin >> s; if(s == "report"){ cin >> n1 >> k1; if(k1 == "fruit"){ fruit[fruit_count++] = Fruit(n1); } else{ meat[meat_count++] = Meat(n1); } } else if(s == "eat"){ cin >> n1 >> k1; int idx = get_index(n1, k1); if(k1 == "fruit"){ fruit[idx].eaten(); } else{ meat[idx].eaten(); } } else if(s == "throw"){ cin >> n1 >> k1; int idx = get_index(n1, k1); if(k1 == "fruit"){ fruit[idx].thrown(); } else{ meat[idx].thrown(); } } else{ cin >> n1 >> n2 >> k1 >> k2; int idx1 = get_index(n1, k1); int idx2 = get_index(n2, k2); if(k1 == "fruit"){ if(k2 == "fruit"){ fruit[idx1] += fruit[idx2]; } else{ fruit[idx1] += meat[idx2]; } } else{ if(k2 == "fruit"){ meat[idx1] += fruit[idx2]; } else{ meat[idx1] += meat[idx2]; } } } } for(int i = 0; i < fruit_count; i++){ fruit[i].five_min_pass(); } for(int i = 0; i < meat_count; i++){ meat[i].five_min_pass(); } } // sort the food in the order which meets the requirement sort(fruit, fruit + fruit_count); sort(meat, meat + meat_count); // Making the first t food be all the food on the floor. Design the '<' operator to do this. int sick = 0, flag = 0; int fruit_floor = fruit_count, meat_floor = meat_count; for(int i = 0; i < fruit_count; i++){ Fruit f(fruit[i]); cout<<f<<':'<<f.on_floor()<<endl; if(!f.on_floor() && !flag){ fruit_floor = i; flag = 1; } if(f.sickness()) sick++; } flag = 0; for(int i = 0; i < meat_count; i++){ Meat m(meat[i]); cout<<m<<':'<<m.on_floor()<<endl; if(!m.on_floor() && !flag){ meat_floor = i; flag = 1; } if(m.sickness()) sick++; } cout<<fruit_floor<<' '<<meat_floor<<endl; int onfloor = fruit_floor + meat_floor ; cout << sick << "\n" << onfloor << "\n"; int fruit_idx = 0, meat_idx = 0; while(onfloor--){ bool f1 = meat_idx == meat_floor; bool f2 = fruit_idx != fruit_floor; bool f3 = fruit[fruit_idx].get_name() < meat[meat_idx].get_name(); if(f1 || (f2 && f3)){ cout << fruit[fruit_idx++] << "\n"; } else{ cout << meat[meat_idx++] << "\n"; } } /* for(int i=0;i<fruit_count;i++){ cout<<fruit[i]<<":"<<fruit[i].on_floor()<<endl; } for(int i=0;i<meat_count;i++){ cout<<meat[i]<<":"<<meat[i].on_floor()<<endl; }*/ return 0; } /* #include "function.h" #include <iostream> #include <string> #include <algorithm> using namespace std; */ // constructor Food::Food(){ name=""; fly=0; pos=FLOOR; } Food::Food(string& n){ name=n; fly=0; pos=FLOOR; } // copy constructor Food::Food(const Food& rhs){ name=rhs.name; fly=rhs.fly; pos=rhs.pos; } // functions // eaten: the food is eaten, change its position // thrown: the food is thrown, change its position // on_floor: return whether the food is on the floor // get_name: return the Food's name // get_fly: return the amount of the fly void Food::eaten(){ pos=STOMACH; } void Food::thrown(){ pos=TRASHCAN; } bool Food::on_floor(){ return pos==FLOOR; } string Food::get_name(){ return name; } int Food::get_fly(){ return fly; } // operators // < : use this operator to sort the food so the output of on-floor food satisfy the requirement // == : determine whether the food's name is same as a string // << : output the food as the mentioned output format bool Food::operator < (const Food& rhs){ if(this->on_floor()==(rhs.pos==FLOOR)){ int i=0; while(1){ if(name[i]==rhs.name[i])i++; else return name[i]<rhs.name[i]; } } else if(this->on_floor())return 1; else if(rhs.pos==FLOOR)return 0; } bool Food::operator == (const string& rhs){ if(rhs.compare(name)==0)return 1; else return 0; } ostream& operator << (ostream& os, Food rhs){ os<<rhs.get_name(); return os; } Fruit::Fruit(){ Food(); } Fruit::Fruit(string& rhs){ Food(); fly=0; name=rhs; pos=FLOOR; } // five_min_pass: 5 min passes // sickness: return whether the fruit makes someone sick // += : mix the food together void Fruit::five_min_pass(){ if(pos==FLOOR)fly+=4; } bool Fruit::sickness(){ return fly>=10&&pos==STOMACH; } void Fruit::operator += (Fruit& rhs){ name=rhs.get_name()+name; fly+=rhs.get_fly(); } void Fruit::operator += (Meat& rhs){ name=rhs.get_name()+name; fly+=rhs.get_fly(); } Meat::Meat(){ Food(); } Meat::Meat(string& rhs){ Food(); name=rhs; fly=0; pos=FLOOR; } // five_min_pass: 5 min passes // sickness: return whether the meat makes someone sick // += : mix the food together void Meat::five_min_pass(){ if(pos==FLOOR)fly+=5; } bool Meat::sickness(){ return fly>=20&&pos==STOMACH; } void Meat::operator += (Fruit& rhs){ name=rhs.get_name()+name; fly+=rhs.get_fly(); } void Meat::operator += (Meat& rhs){ name=rhs.get_name()+name; fly+=rhs.get_fly(); }