考试时的代码:
#include <iostream>
using namespace std;
struct Weapon{
int oid;
int priority;
int time;
int type;
Weapon *next;
int leftTime;
};
struct People{
int uid;
int type;
Weapon *list;
Weapon *finish;
int listNum;
int workingonOid;
};
struct Shop{
People *peopleType;
People *peopleUid;
int limit;
int num;
int total;
int state[105];
Weapon *minWeapon;
};
void add(Shop *shop, Weapon *weapon){
shop->total++;
shop->state[weapon->oid] = 0;
if(weapon->priority < shop->minWeapon->priority){
shop->minWeapon = weapon;
}
if(shop->total > shop->limit){
Weapon *min = shop->minWeapon;
People *people = shop->peopleType + min->type;
Weapon *curr = people->list->next;
Weapon *prev = people->list;
while(curr->oid != min->oid){
prev = curr;
curr = curr->next;
}
prev->next = curr->next;
people->listNum--;
shop->total--;
shop->state[min->oid] = -2;
min->priority = 10000;
if(people->workingonOid == min->oid){
people->workingonOid = 0;
}
}
People *people = shop->peopleType + weapon->type;
people->listNum++;
People *same = shop->peopleUid + people->uid;
same->listNum++;
Weapon *prev = people->list;
Weapon *curr = prev->next;
while(curr != nullptr && curr->priority > weapon->priority){
prev = curr;
curr = curr->next;
}
prev->next = weapon;
weapon->next = curr;
}
void searchPeople(Shop *shop, int uid){
People *people = shop->peopleUid + uid;
people = shop->peopleType + people->type;
if(people->workingonOid == 0){
cout << "worker " << people->uid << " resting" << endl;
}else{
cout << "worker " << people->uid << " doing order " << people->workingonOid << endl;
}
}
void searchList(Shop *shop, int oid){
if(shop->state[oid] == 1){
cout << "order " << oid << " done" << endl;
}else if(shop->state[oid] == 0){
cout << "order " << oid << " pending" << endl;
}else if(shop->state[oid] == -1){
cout << "order " << oid << " doing" << endl;
}else if(shop->state[oid] == -2){
cout << "order " << oid << " discarded" << endl;
}
}
void searchLists(Shop *shop, int uid){
People *people = shop->peopleUid + uid;
people = shop->peopleType + people->type;
Weapon *prev = people->finish;
Weapon *curr = prev->next;
if(curr == nullptr){
cout << endl;
return;
}else{
while(curr != nullptr){
if(shop->state[curr->oid] == 1){
cout << curr->oid;
if(curr->next != nullptr){
cout << " ";
}
}
prev = curr;
curr = curr->next;
}
cout << endl;
}
}
void working(Shop *shop){
People *peopleList = shop->peopleUid;
for(int i = 1; i <= shop->num; i++){
People *currPeople = peopleList + i;
currPeople = shop->peopleType + currPeople->type;
if(currPeople->workingonOid == 0){
if(currPeople->listNum > 0){
Weapon *ready = currPeople->list->next;
currPeople->workingonOid = ready->oid;
ready->leftTime -= 1;
shop->state[ready->oid] = -1;
}
}else{
Weapon *now = currPeople->list->next;
while(now->oid != currPeople->workingonOid){
now = now->next;
}
if(now->leftTime == 0){
shop->state[now->oid] = 1;
currPeople->listNum--;
shop->total--;
if(now->priority == shop->minWeapon->priority){
shop->minWeapon->priority = 10000;
}
Weapon *curr = currPeople->finish;
while(curr->next != nullptr){
curr = curr->next;
}
curr->next = now;
Weapon *prev = currPeople->list;
curr = prev->next;
while (curr != now){
prev = curr;
curr = curr->next;
}
prev->next = curr -> next;
if(currPeople->listNum > 0){
curr = currPeople->list->next;
currPeople->workingonOid = curr->oid;
curr->leftTime -= 1;
shop->state[curr->oid] = -1;
}else{
currPeople->workingonOid = 0;
}
}else{
now->leftTime -= 1;
}
}
Weapon *temp = currPeople->list->next;
while(temp != nullptr){
if(temp->priority < shop->minWeapon->priority){
shop->minWeapon = temp;
}
temp = temp->next;
}
}
}
int main(){
int num, limit;
cin >> num >> limit;
Shop *shop = new Shop;
shop->num = num;
shop->limit = limit;
shop->peopleType = new People[105];
shop->peopleUid = new People[num + 1];
shop->total = 0;
Weapon *min = new Weapon;
min->next = nullptr;
min->priority = 10000;
shop->minWeapon = min;
int uid, type;
for(int i = 0; i < num; i++){
cin >> uid >> type;
People *curr = shop->peopleType + type;
curr->uid = uid;
curr->type = type;
curr->listNum = 0;
curr->workingonOid = 0;
Weapon *dummy1 = new Weapon;
dummy1->next = nullptr;
dummy1->oid = -1;
dummy1->type = type;
curr->list = dummy1;
Weapon *dummy2 = new Weapon;
dummy2->next = nullptr;
dummy2->oid = -1;
dummy2->type = type;
curr->finish = dummy2;
curr = shop->peopleUid + uid;
curr->uid = uid;
curr->type = type;
curr->listNum = 0;
curr->workingonOid = 0;
curr->list = nullptr;
curr->finish = nullptr;
}
int N;
cin >> N;
string op;
while(N > 0){
working(shop);
cin >> op;
if(op == "add"){
Weapon *weapon = new Weapon;
cin >> weapon->oid >> weapon->priority >> weapon->time >> weapon->type;
weapon->next = nullptr;
weapon->leftTime = weapon->time;
add(shop, weapon);
}else if(op == "queryUser"){
int uid;
cin >> uid;
searchPeople(shop, uid);
}else if(op == "queryOrder"){
int oid;
cin >> oid;
searchList(shop, oid);
}else if(op == "queryOrders"){
int uid;
cin >> uid;
searchLists(shop, uid);
}
N--;
}
return 0;
}
struct之外的部分不说了,因为结构错乱,函数也很冗余。
用OO避免上述问题后的代码:
#include <iostream>
#include <vector>
using namespace std;
class Weapon{
public:
explicit Weapon(int o, int p, int ti, int ty): oid(o), priority(p), time(ti), type(ty){
leftTime = time;
state = 0;
};
int getType(){
return type;
}
int getOid(){
return oid;
}
int getState(){
return state;
}
int getPriority(){
return priority;
}
int getLeftTime(){
return leftTime;
}
void setState(int i){
state = i;
}
void decreaseLeftTime(){
leftTime--;
}
private:
int oid;
int priority;
int time;
int type;
int leftTime;
int state;
};
class People{
public:
explicit People(int u, int t): uid(u), type(t){
workingonOid = 0;
};
int getUid(){
return uid;
}
int getWorkingOnOid(){
return workingonOid;
}
int getType(){
return type;
}
void setWorkingOnOid(int w){
workingonOid = w;
}
private:
int uid;
int type;
int workingonOid{0};
};
class Shop{
public:
explicit Shop(int l, int n): limit(l), num(n){};
void addPeople(People *p){
workers.push_back(p);
}
void addWeapon(Weapon *w){
int now = getUnfinished();
if(now >= limit){
discardMin();
}
weapons.push_back(w);
}
int getUnfinished(){
int n = 0;
for(int i = 0; i < weapons.size(); i++){
if(weapons[i]->getState() == 0){
n++;
}
}
return n;
}
Weapon* findCurr(int oid){
for(int i = 0; i < weapons.size(); i++){
if(weapons[i]->getOid() == oid){
return weapons[i];
}
}
}
void discardMin(){
Weapon *min = new Weapon(0,10000, 0, 0);
for(int i = 0; i < weapons.size(); i++){
if(weapons[i]->getState() == 0 && weapons[i]->getPriority() < min->getPriority()){
min = weapons[i];
}
}
min->setState(-2);
return;
}
Weapon* maxReady(int type){
Weapon *max = new Weapon(0,0,0,0);
for(int i = 0; i < weapons.size(); i++){
if(weapons[i]->getState() == 0 && weapons[i]->getType() == type){
if(weapons[i]->getPriority() > max->getPriority()){
max = weapons[i];
}
}
}
return max;
}
void getWork(){
People *curr;
Weapon *current;
for(int i = 0; i < workers.size(); i++){
curr = workers[i];
if(curr->getWorkingOnOid() == 0){
current = maxReady(curr->getType());
if(current->getOid() != 0){
current->setState(-1);
curr->setWorkingOnOid(current->getOid());
}
}else{
current = findCurr(curr->getWorkingOnOid());
current->decreaseLeftTime();
if(current->getLeftTime() == 0){
current->setState(1);
finished.push_back(current);
current = maxReady(curr->getType());
curr->setWorkingOnOid(current->getOid());
current->setState(-1);
}
}
}
}
void searchPeople(int uid){
People *curr;
for(int i = 0; i < workers.size(); i++){
if(workers[i]->getUid() == uid){
curr = workers[i];
break;
}
}
if(curr->getWorkingOnOid() == 0){
cout << "worker " << uid << " resting" << endl;
}else{
cout << "worker " << uid << " doing order " << curr->getWorkingOnOid() << endl;
}
}
void searchLists(int uid){
int currType = 0;
for(int i = 0; i < workers.size(); i++){
if(workers[i]->getUid() == uid){
currType = workers[i]->getType();
break;
}
}
for(int i = 0; i < finished.size(); i++){
if(finished[i]->getType() == currType){
cout << finished[i]->getOid() << " ";
}
}
cout << endl;
}
void searchList(int oid){
Weapon *curr;
for(int i = 0; i < weapons.size(); i++){
if(weapons[i]->getOid() == oid){
curr = weapons[i];
}
}
if(curr->getState() == 1){
cout << "order " << oid << " done" << endl;
}else if(curr->getState() == 0){
cout << "order " << oid << " pending" << endl;
}else if(curr->getState() == -1){
cout << "order " << oid << " doing" << endl;
}else if(curr->getState() == -2){
cout << "order " << oid << " discarded" << endl;
}
}
private:
vector<People *> workers;
vector<Weapon *> weapons;
vector<Weapon *> finished;
Weapon* w[100];
int limit;
int num;
};
int main(){
int num, limit;
cin >> num >> limit;
Shop *shop = new Shop(limit, num);
int uid, type;
for(int i = 0; i < num; i++){
cin >> uid >> type;
People *p = new People(uid, type);
shop->addPeople(p);
}
int N;
cin >> N;
string op;
while(N > 0){
shop->getWork();
cin >> op;
if(op == "add"){
int o, p, time, type;
cin >> o >> p >> time >> type;
Weapon *w = new Weapon(o, p, time, type);
shop->addWeapon(w);
}else if(op == "queryUser"){
int uid;
cin >> uid;
shop->searchPeople(uid);
}else if(op == "queryOrder"){
int oid;
cin >> oid;
shop->searchList(oid);
}else if(op == "queryOrders"){
int uid;
cin >> uid;
shop->searchLists(uid);
}
N--;
}
return 0;
}
一些注意事项:
- C++11 引入的 initializer_list不能进行私有变量初始化,所以注意在构造函数中做好初始化工作
- 对于临时变量,如Weapon *max, *min;做好初始化工作,否则会有乱七八糟的数据
- 不要害怕循环,你是在写考试,不是在降低复杂度
- 好好读题,尤其是明显设置了区分度的部分。
两个bug: 1、记得时间为0选择另一个任务时,修改该任务的状态! 2、记得丢弃时是在已有的中选择,新加入的无论如何都会加!
记录一个还比较强的用例: 3 2 1 1 2 2 3 3 15 add 1 5 8 1 queryOrder 1 add 2 6 8 1 queryOrder 1 add 3 2 10 3 queryOrder 3 queryOrder 2 add 6 1 5 3 queryOrder 6 add 5 10 2 3 add 10 11 2 3 add 7 12 2 3 queryOrder 1 queryOrder 2 queryOrder 5
再更: 1、实际考试中全部写public:基本上就是构造函数,和manager里的逻辑实现函数。 2、有些方法可以写在类里面,耦合性更强。
#include <iostream>
#include <vector>
using namespace std;
class Weapon{
public:
int oid;
int priority;
int time;
int type;
int leftTime;
int state;
explicit Weapon(int o, int p, int ti, int ty): oid(o), priority(p), time(ti), type(ty){
leftTime = time;
state = 0;
};
};
class People{
public:
int uid;
int type;
int workingonOid{0};
explicit People(int u, int t): uid(u), type(t){
workingonOid = 0;
};
};
class Shop{
public:
vector<People *> workers;
vector<Weapon *> weapons;
vector<Weapon *> finished;
Weapon* w[100];
int limit;
int num;
explicit Shop(int l, int n): limit(l), num(n){};
void addPeople(People *p){
workers.push_back(p);
}
void addWeapon(Weapon *w){
int now = getUnfinished();
if(now >= limit){
discardMin();
}
weapons.push_back(w);
}
int getUnfinished(){
int n = 0;
for(int i = 0; i < weapons.size(); i++){
if(weapons[i]->state == 0){
n++;
}
}
return n;
}
Weapon* findCurr(int oid){
for(int i = 0; i < weapons.size(); i++){
if(weapons[i]->oid == oid){
return weapons[i];
}
}
}
void discardMin(){
Weapon *min = new Weapon(0,10000, 0, 0);
for(int i = 0; i < weapons.size(); i++){
if(weapons[i]->state == 0 && weapons[i]->priority < min->priority){
min = weapons[i];
}
}
min->state = -2;
return;
}
Weapon* maxReady(int type){
Weapon *max = new Weapon(0,0,0,0);
for(int i = 0; i < weapons.size(); i++){
if(weapons[i]->state == 0 && weapons[i]->type == type){
if(weapons[i]->priority > max->priority){
max = weapons[i];
}
}
}
return max;
}
void getWork(){
People *curr;
Weapon *current;
for(int i = 0; i < workers.size(); i++){
curr = workers[i];
if(curr->workingonOid == 0){
current = maxReady(curr->type);
if(current->oid != 0){
current->state = -1;
curr->workingonOid = current->oid;
}
}else{
current = findCurr(curr->workingonOid);
current->leftTime--;
if(current->leftTime == 0){
current->state = 1;
finished.push_back(current);
current = maxReady(curr->type);
curr->workingonOid = current->oid;
current->state = -1;
}
}
}
}
void searchPeople(int uid){
People *curr;
for(int i = 0; i < workers.size(); i++){
if(workers[i]->uid == uid){
curr = workers[i];
break;
}
}
if(curr->workingonOid == 0){
cout << "worker " << uid << " resting" << endl;
}else{
cout << "worker " << uid << " doing order " << curr->workingonOid<< endl;
}
}
void searchLists(int uid){
int currType = 0;
for(int i = 0; i < workers.size(); i++){
if(workers[i]->uid== uid){
currType = workers[i]->type;
break;
}
}
for(int i = 0; i < finished.size(); i++){
if(finished[i]->type == currType){
cout << finished[i]->oid<< " ";
}
}
cout << endl;
}
void searchList(int oid){
Weapon *curr;
for(int i = 0; i < weapons.size(); i++){
if(weapons[i]->oid == oid){
curr = weapons[i];
}
}
if(curr->state == 1){
cout << "order " << oid << " done" << endl;
}else if(curr->state == 0){
cout << "order " << oid << " pending" << endl;
}else if(curr->state == -1){
cout << "order " << oid << " doing" << endl;
}else if(curr->state == -2){
cout << "order " << oid << " discarded" << endl;
}
}
};
int main(){
int num, limit;
cin >> num >> limit;
Shop *shop = new Shop(limit, num);
int uid, type;
for(int i = 0; i < num; i++){
cin >> uid >> type;
People *p = new People(uid, type);
shop->addPeople(p);
}
int N;
cin >> N;
string op;
while(N > 0){
shop->getWork();
cin >> op;
if(op == "add"){
int o, p, time, type;
cin >> o >> p >> time >> type;
Weapon *w = new Weapon(o, p, time, type);
shop->addWeapon(w);
}else if(op == "queryUser"){
int uid;
cin >> uid;
shop->searchPeople(uid);
}else if(op == "queryOrder"){
int oid;
cin >> oid;
shop->searchList(oid);
}else if(op == "queryOrders"){
int uid;
cin >> uid;
shop->searchLists(uid);
}
N--;
}
return 0;
}
|