[USACO22JAN] Farm Updates G
发布人:shili8
发布时间:2024-11-09 05:08
阅读次数:0
**Farm Updates**
**Problem Description**
在一个农场中,牛奶生产商需要定期更新牛的信息。每个牛都有一个唯一的 ID 号,以及其所产奶量、牛奶价格以及牛的健康状况等信息。牛奶生产商希望能够快速地查找某一特定牛的信息,并且能够对所有牛的信息进行排序和统计。
**Problem Requirements**
1. **查找牛的信息**: 需要能够根据 ID 号快速找到某一特定牛的信息。
2. **排序牛的信息**: 需要能够按照奶量、价格或健康状况等不同维度对所有牛的信息进行排序。
3. **统计牛的信息**: 需要能够统计出所有牛的平均奶量、最高奶量、最低奶量等信息。
**Solution**
为了解决这个问题,我们可以使用一个基于链表的数据结构来存储每个牛的信息。链表的特点是插入和删除操作非常快,适合频繁查找和排序的场景。
### 牛类定义
cppclass Cow {
public:
int id; // 牛的ID号 double milkQuantity; // 牛产奶量 double price; // 牛奶价格 string healthStatus; // 牛的健康状况 Cow(int id, double milkQuantity, double price, string healthStatus) {
this->id = id;
this->milkQuantity = milkQuantity;
this->price = price;
this->healthStatus = healthStatus;
}
};
### 链表类定义
cppclass LinkedList {
private:
struct Node {
Cow cow;
Node* next;
Node(Cow cow) : cow(cow), next(nullptr) {}
};
public:
Node* head; // 链表头结点 LinkedList() : head(nullptr) {}
~LinkedList() {
while (head != nullptr) {
Node* temp = head;
head = head->next;
delete temp;
}
}
void addCow(Cow cow) {
Node* newNode = new Node(cow);
if (head == nullptr) {
head = newNode;
} else {
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
}
}
Cow* findCow(int id) {
Node* current = head;
while (current != nullptr) {
if (current->cow.id == id) {
return ¤t->cow;
}
current = current->next;
}
return nullptr; // 未找到匹配的牛 }
void sortCowByMilkQuantity() {
Node* current = head;
while (current != nullptr) {
Node* nextNode = current->next;
while (nextNode != nullptr) {
if (current->cow.milkQuantity < nextNode->cow.milkQuantity) {
Cow temp = current->cow;
current->cow = nextNode->cow;
nextNode->cow = temp;
}
nextNode = nextNode->next;
}
current = current->next;
}
}
void sortCowByPrice() {
Node* current = head;
while (current != nullptr) {
Node* nextNode = current->next;
while (nextNode != nullptr) {
if (current->cow.price < nextNode->cow.price) {
Cow temp = current->cow;
current->cow = nextNode->cow;
nextNode->cow = temp;
}
nextNode = nextNode->next;
}
current = current->next;
}
}
void sortCowByHealthStatus() {
Node* current = head;
while (current != nullptr) {
Node* nextNode = current->next;
while (nextNode != nullptr) {
if (current->cow.healthStatus < nextNode->cow.healthStatus) {
Cow temp = current->cow;
current->cow = nextNode->cow;
nextNode->cow = temp;
}
nextNode = nextNode->next;
}
current = current->next;
}
}
void printAllCowInfo() {
Node* current = head;
while (current != nullptr) {
cout << "ID: " << current->cow.id << ", Milk Quantity: " << current->cow.milkQuantity << ", Price: " << current->cow.price << ", Health Status: " << current->cow.healthStatus << endl;
current = current->next;
}
}
void printAverageMilkQuantity() {
double sum =0.0;
Node* current = head;
while (current != nullptr) {
sum += current->cow.milkQuantity;
current = current->next;
}
cout << "Average Milk Quantity: " << sum / countCow() << endl;
}
void printHighestMilkQuantity() {
double highest =0.0;
Node* current = head;
while (current != nullptr) {
if (current->cow.milkQuantity > highest) {
highest = current->cow.milkQuantity;
}
current = current->next;
}
cout << "Highest Milk Quantity: " << highest << endl;
}
void printLowestMilkQuantity() {
double lowest =1000000.0; // 初始值设为较大 Node* current = head;
while (current != nullptr) {
if (current->cow.milkQuantity < lowest) {
lowest = current->cow.milkQuantity;
}
current = current->next;
}
cout << "Lowest Milk Quantity: " << lowest << endl;
}
private:
int countCow() {
int count =0;
Node* current = head;
while (current != nullptr) {
count++;
current = current->next;
}
return count;
}
};
### 主函数
cppint main() {
LinkedList linkedList;
// 添加牛的信息 Cow cow1(1,1000.0,5.0, "健康");
Cow cow2(2,2000.0,10.0, "不健康");
Cow cow3(3,3000.0,15.0, "健康");
linkedList.addCow(cow1);
linkedList.addCow(cow2);
linkedList.addCow(cow3);
// 查找牛的信息 Cow* foundCow = linkedList.findCow(2);
if (foundCow != nullptr) {
cout << "Found cow with ID: " << foundCow->id << endl;
} else {
cout << "未找到匹配的牛" << endl;
}
// 排序牛的信息 linkedList.sortCowByMilkQuantity();
cout << "Sorted by milk quantity:" << endl;
linkedList.printAllCowInfo();
linkedList.sortCowByPrice();
cout << "Sorted by price:" << endl;
linkedList.printAllCowInfo();
linkedList.sortCowByHealthStatus();
cout << "Sorted by health status:" << endl;
linkedList.printAllCowInfo();
// 统计牛的信息 linkedList.printAverageMilkQuantity();
linkedList.printHighestMilkQuantity();
linkedList.printLowestMilkQuantity();
return0;
}
### 总结本文介绍了一个基于链表的数据结构来存储每个牛的信息,并提供了相关函数来查找、排序和统计牛的信息。通过使用链表,我们可以快速地插入和删除牛的信息,适合频繁查找和排序的场景。

