Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
прога экзамен.docx
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
30 Кб
Скачать
☆

Int main()

{

num *head, *newList, *last, *current;

int n;

/*

где-то здесь идёт задавание списка, но оно не требуется в задании

*/

return 0;

}

// 26. Написать фрагмент программы, подсчитывающий среднее геометрическое двусвязного списка целых чисел,

// не включая в расчет максимальный и минимальный элементы. Оформить функцию добавления элемента в список.

#include <iostream>

#include <cmath>

using namespace std;

struct num

{

int a;

num *next, *prev;

};

void add_element(int &n, num **head, num **newList, num **last, num **current)

{

int tmp;

if((*head) != NULL)

{

(*newList) = new num;

cin >> (*newList) -> a;

(*newList) -> next = NULL;

(*newList) -> prev = (*current);

(*current) -> next = (*newList);

(*last) = (*newList);

(*current) = (*newList);

}

else

{

(*newList) = new num;

cin >> (*newList) -> a;

(*head) = (*newList);

(*newList) -> next = NULL;

(*newList) -> prev = NULL;

(*current) = (*newList);

(*last) = (*head);

}

n++;

}

void quest(num **head, num **newList, num **last, num **current)

{

(*newList)=(*head);

int min = (*head)->a, max = (*head)->a, count = 0;

long sr = 1;

while((*newList))

{

if ((*newList)->a < min)

min = (*newList)->a;

if ((*newList)->a > max)

max = (*newList)->a;

(*newList)=(*newList)->next;

}

(*newList)=(*head);

while((*newList))

if (!((*newList)->a == max || (*newList)->a == min))

{

sr *= (*newList)->a;

count++;

}

double q = pow(double(sr), 1.0/double(count));

cout << q << endl;

}

Int main()

{

num *head, *newList, *last, *current;

int n;

/*

где-то здесь идёт задавание списка, но оно не требуется в задании

*/

return 0;

}

// 5. Известны данные о тираже и стоимости журналов. Написать фрагмент программы, находящий среднюю цену журналов,

//тираж которых меньше 10 000 экземпляров. Данные журналов должны храниться в стеке (односвязный список, добавление в начало).

//Оформить функцию добавления в список.

#include <iostream>

using namespace std;

struct j

{

int tir, cost;

j *next;

};

void newel(j **head, j **newj, j **curr, j **last)

{

if ((*head) == nullptr)

{

(*newj) = new j;

cout << "Enter tir and cost" << endl;

cin >> (*newj) -> tir >> (*newj) -> cost;

cout << endl;

(*head) = (*newj);

(*newj) -> next = nullptr;

(*curr) = (*newj);

(*last) = (*head);

} else

{

(*newj) = new j;

cout << "Enter tir and cost" << endl;

cin >> (*newj) -> tir >> (*newj) -> cost;

cout << endl;

(*newj) -> next = (*head);

(*head) = (*newj);

(*curr) = (*newj);

}

}

void quest(j *head)

{

j *i = head;

double srcost = 0;

int count = 0;

while (i)

{

if (i -> tir < 10000)

{

srcost += i -> cost;

count++;

}

i = i -> next;

}

srcost /= double(count);

cout << srcost << endl;

}

Int main()

{

j *head = nullptr, *last = nullptr, *curr = nullptr, *newj = nullptr;

newel(&head, &newj, &curr, &last);

newel(&head, &newj, &curr, &last);

newel(&head, &newj, &curr, &last);

quest(head);

}

// 6. Написать фрагмент программы, удаляющий из двусвязного списка целых чисел все вхождения

// максимального и минимального элементов.

#include <iostream>

using namespace std;

struct num

{

int a;

num *next, *prev;

};

void delete_element(int &n, num **head, num **newList, num **last, num **current)

{

(*newList)=(*head);

int min = (*head)->a, max = (*head)->a;

while((*newList))

{

if ((*newList)->a < min)

min = (*newList)->a;

if ((*newList)->a > max)

max = (*newList)->a;

(*newList)=(*newList)->next;

}

(*newList)=(*head);

while((*newList))

{

if ((*newList)->a == max || (*newList)->a == min)

if((*newList)==(*head)) //если первый

{

(*head)=(*head)->next;

if((*head))

(*head)->prev=NULL;

else

(*last)=NULL;

if((*current)==(*newList))

(*current)=(*head);

delete (*newList);

} else

if ((*newList)==(*last)) //если последний

{

(*last)=(*last)->prev;

if((*last))

(*last)->next=NULL;

if((*current)==(*newList))

(*current)=(*last);

delete (*newList);

} else //если где-то посередине

{

(*newList)->prev->next=(*newList)->next;

(*newList)->next->prev=(*newList)->prev;

(*current)=(*newList)->prev;

delete (*newList);

}

(*newList)=(*newList)->next;

}

n--;

}