Багаж пассажира характеризуется количеством вещей (целый тип) и общим весом вещей (вещественный тип). Дан список из сведений о багаже 10 пассажиров.
Найти багаж, средний вес одной вещи, в котором отличается не более, чем на 0,3 кг от общего среднего веса одной вещи по всему списку.
C/C++
C++ написать код
#include
#include
#include
#include
#include
using namespace std;
struct Luggage {
unsigned quantity;
size_t id;
double weight;
Luggage() : quantity(0), id(0), weight(0) {}
double average()const {
return weight / quantity;
}
};
class Passengers {
public:
void add(const Luggage& lug) {
box.push_back(lug);
}
double average()const {
auto full_quantity = 0U;
auto full_weight = 0.0;
for (const auto& [q, id, w] : box) {
full_quantity += q;
full_weight += w;
}
return full_weight / full_quantity;
}
vector find_difference(const double& weight)const {
vector query;
const auto avg = average();
for (const auto& item : box) {
if (fabs(item.average() - avg) luggage.quantity;
cout > luggage.weight;
luggage.id = id;
return luggage;
}
int main() {
constexpr size_t n = 3;
Passengers passengers;
for (size_t i = 0; i < n; ++i) {
auto luggage = input(i + 1);
passengers.add(luggage);
}
auto avg = passengers.average();
cout
#include
#include
using namespace std;
struct PassLuggage
{
int itemCount; //Количество предметов
float weidth; //Общий вес
PassLuggage* next; //Указатель на следующий элемент списка
};
//Вывод списка в консоль
void outPut(PassLuggage* uk)
{
for (int i = 0; i < 10 && uk->next != NULL; i++)
{
cout next = el;
if (i + 1 == 0)
{
el->itemCount = NULL;
el->weidth = NULL;
el->next = NULL;
}
el->itemCount = rand() % 50 + 1;
el->weidth = rand() % 100 + 10;
el->next = NULL;
uk = el;
}
uk = arr;
outPut(uk);
}
//Ручной способ заполнения списка
void keyboardInput(PassLuggage* arr, PassLuggage* uk)
{
cout > arr->itemCount;
cout > arr->weidth;
arr->next = NULL;
for (int i = 0; i < 10; i++)
{
PassLuggage* el = new PassLuggage;
uk->next = el;
if (i + 1 == 0)
{
el->itemCount = NULL;
el->weidth = NULL;
el->next = NULL;
}
cout > el->itemCount;
cout > el->weidth;
el->next = NULL;
uk = el;
}
uk = arr;
outPut(uk);
}
//Способы заполнения списка (случайный и ручной)
void inputValues(PassLuggage* arr, PassLuggage* uk)
{
srand(time(NULL));
int quest;
while (1)
{
cout quest;
if (quest == 1)
{
Random(arr, uk);
uk = arr;
break;
}
if (quest == 2)
{
keyboardInput(arr, uk);
uk == arr;
break;
}
}
}
//Функция нахождения среднего значения веса одной вещи
float averageWeight(PassLuggage* uk)
{
float allWeigth = 0;
int allItems = 0;
while (uk->next != NULL)
{
allWeigth += uk->weidth;
allItems += uk->itemCount;
uk = uk->next;
}
return allWeigth / allItems;
}
int main()
{
PassLuggage* arr = new PassLuggage;
PassLuggage* uk = arr;
inputValues(arr, uk);
float medWeigth = averageWeight(uk);
uk = arr; //Возвращаем на начало списка
cout next != NULL; i++)
{
if (medWeigth + 0.3 >= uk->weidth / uk->itemCount && medWeigth - 0.3 weidth / uk->itemCount) //Дипазон отслонения веса +- 0.3
{
cout
Алексей Смирнов
спасибо.
Похожие вопросы
- Написать код для задачи C++
- Помогите пожалуйста написать код.(C++)
- Написал код для языка C, но работает не правильно
- Помогите пожалуйста написать код на c++, выводящий имя, фамилию и дату рождения нескольких человек
- Написать код для задачи на C++
- Помогите пожалуйста написать код на C++
- Написать код C++
- Написать код на языке C++
- Задание на c++ ответить на вопросы и написать код
- Прошу помогите написать код на c++, нужно сдать сегодня ?