#include
struct LNode {
int month;
int day;
string name;
double price;
LNode* next;
LNode() : next(nullptr) {}
LNode(int a, int b, string c, double d) : month(a), day(b), name(c), price(d), next(nullptr) {}
};
class List { private:
int _size;
LNode* head;
public:
List()
{
head = new LNode();
_size = 0;
}
~List()
{
while (head)
{
LNode* temp = head->next;
delete head;
head = temp;
}
_size = 0;
}
void insert(int& a, int& b, string& c, double& d)
{
_size++;
LNode* p = new LNode(a, b, c, d);
p->next = head->next;
head->next = p;
}
void insert(int index, int a, int b, string c, double d)
{
if (index <= 0 || index > _size + 1)
return;
_size++;
LNode* p = head;
for (int i = 0; i < index - 1; i++)
p = p->next;
LNode* tmp = new LNode(a, b ,c ,d);
tmp->next = p->next;
p->next= tmp;
}
void print_L()
{
LNode* p = head->next;
while(p != nullptr)
{
cout << p->month << " " << p->day << " ";
p = p->next;
}
cout << endl;
}
int located(int a, int b)
{
LNode* p = head->next;
int e = 1;
if (p == nullptr)
return e;
while (p != nullptr)
{
if (a < p->month)
{
e++;
p = p->next;
if (p == nullptr)
return e;
}
else if (a > p->month)
return e;
else if (a == p->month)
{
if (b < p->day)
{
e++;
p = p->next;
if (p == nullptr)
return e;
}
else if(b > p->day)
return e;
else
return e;
}
}
return -1; // Not found
}
};
int main() {
int m, n;
string str;
string c;
double d;
List L1;
cin >> m >> n;
for (int i = 0; i < m; i++)
{
cin >> str >> c >> d;
int a = stoi(str.substr(2));
int b = stoi(str.substr(0, 2));
int e = L1.located(a, b);
L1.insert(e, a, b, c, d);
}
double sum1 = 0.0;
int count1 = 0;
double sum2 = 0.0;
int count2 = 0;
LNode* pNode1= L1.head->next;
while(count1 < n && pNode1 != nullptr){
if(pNode1 -> name == "keyboard"){
sum1 += pNode1 -> price;
count1++;
}
pNode1 = pNode1 -> next;
}
LNode* pNode2 = L1.head->next;
while(count2 < n && pNode2 != nullptr){
if(pNode2 -> name == "mouse"){
sum2 += pNode2 -> price;
count2++;
}
pNode2 = pNode2 -> next;
}
cout << fixed << setprecision(1) << sum1 / n << endl;
cout << fixed << setprecision(1) << sum2 / n << endl;
return 0;
}
内容由零声教学AI助手提供,问题来源于学员提问




