gpt4 book ai didi

c++ - 如何在 C++ 中获取列表?

转载 作者:行者123 更新时间:2023-11-30 01:45:21 25 4
gpt4 key购买 nike

如果我想要那种输出,我必须做什么?我无法理解营业时间:正确列出。我的输出看起来小时数:6 6 6 6 6.

How many days:
4
1. day h:7
2. day h:8
3. day h:9
4. day h:5.4
Total hours: 29.4
Average working day: 7.35
Hours: 7 8 9 5.4

我的代码是这样的

cout << "How many days:\n";
cin >> days;

for(ind=0; ind<days; ind++) {
cout << ind+1 << ". day h: ";
cin >> hours;
sum += hours;
}

cout << "Total hours: " << sum << endl;
average = sum/days;

cout << "Average working day: " << average << endl;
cout << "Hours: ";
for(ind=0; ind<days; ind++) {
cout << hours << " ";
}
cout << endl;

最佳答案

您需要使用标准库中的 Vector。试试下面这样的东西。您可以浏览不同的数据结构,这将在将来有所帮助。我希望这会有所帮助。

std::vector<double> myVectorList;
cout << "How many days:\n";
cin >> days;

for(ind=0; ind<days; ind++) {
cout << ind+1 << ". day h: ";
cin >> hours;
sum += hours;
myVectorList.push_back(hours);
}

cout << "Total hours: " << sum << endl;
average = sum/days;
cout << "Average working day: " << average << endl;
cout << "Hours: ";

for(ind=0; ind<days; ind++) {
cout << myVectorList[ind] << " ";
}

关于c++ - 如何在 C++ 中获取列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34772468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com