您的代码已经修改完成,实现了所需的输入输出。以下是修改后的代码:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct LNode {
// 存储数据
int month;
int day;
string name;
double price;
};
class List {
public:
int _size; // 当前元素个数
int capacity; // 顺序表容量
LNode* data; // 数据数组
public:
List(int cap) : _size(0), capacity(cap), data(new LNode[cap]) {}
~List() { delete[] data; }
void insert(int index, int a, int b, const string& c, double d) {
if (index < 0 || index > _size) {
return;
}
if (_size == capacity) {
resize(); // 扩容
}
for (int i = _size - 1; i >= index; --i) {
data[i + 1] = data[i]; // 元素后移
}
data[index].month = a;
data[index].day = b;
data[index].name = c;
data[index].price = d;
++_size;
}
private:
void resize() {
capacity *= 2;
LNode* newData = new LNode[capacity];
for (int i = 0; i < _size; ++i) {
newData[i] = data[i];
}
delete[] data;
data = newData;
}
};
int main() {
int m, n;
cin >> m >> n;
List L1(m);
for (int i = 0; i < m; ++i) {
string str, c;
double d;
cin >> str >> c >> d;
int a = stoi(str.substr(0, 2));
int b = stoi(str.substr(3, 2));
L1.insert(i, a, b, c, d);
}
double sum1 = 0;
int count1 = 0;
double sum2 = 0;
int count2 = 0;
for (int i = 0; i < m; ++i) {
if (count1 >= n) {
break;
}
else {
if (L1.data[i].name == "keyboard") {
sum1 += L1.data[i].price;
++count1;
}
}
}
for (int i = 0; i < m; ++i) {
if (count2 >= n) {
break;
}
else {
if (L1.data[i].name == "mouse") {
sum2 += L1.data[i].price;
++count2;
}
}
}
cout << fixed << setprecision(1) << sum1 / n << endl
<< fixed << setprecision(1) << sum2 / n << endl;
return 0;
}
您现在可以使用上述修改后的代码来实现所需的输入输出。希望对您有所帮助!
内容由零声教学AI助手提供,问题来源于学员提问




