Вот код на питоне:
N = int(input())
objects = []
for i in range(N):
objects.append(list(map(int, input().split(
rezult = []
summ = 0
price = 0
while summ <= 40:
Max = 0
index = -1
for i in objects:
if i[1] > Max and i[0] + summ <= 40 and not objects.index(i) in rezult:
Max = i[1]
index = objects.index(i)
if index == -1:
break
rezult.append(index)
summ += objects[index][0]
price += objects[index][1]
for i in rezult:
print(i + 1)
print(summ, price)
Объяснение:
При необходимости могу пояснить построчно.
#include <iostream>
#include <cmath>
signed main()
{
setlocale(LC_ALL, "Rus");
int cnt, countBoys = 0, countGirls = 0, sumHeightBoys = 0, sumHeightGirls = 0;
std::cout << "Введите количество детей в классе: ";
std::cin >> cnt;
int* height = new int[cnt];
for (int i = 0; i < cnt; i++)
std::cout << "Рост ребёнка №" << i + 1 << ": ";
std::cin >> height[i];
if (height[i] < 0) {
countBoys++;
sumHeightBoys += height[i];
}
else {
countGirls++;
sumHeightGirls += height[i];
std::cout << "Средний рост мальчиков: " << abs(sumHeightBoys) / countBoys << std::endl;
std::cout << "Средний рост девочек: " << sumHeightGirls / countGirls << std::endl;
return 0;
Вот код на питоне:
N = int(input())
objects = []
for i in range(N):
objects.append(list(map(int, input().split(
rezult = []
summ = 0
price = 0
while summ <= 40:
Max = 0
index = -1
for i in objects:
if i[1] > Max and i[0] + summ <= 40 and not objects.index(i) in rezult:
Max = i[1]
index = objects.index(i)
if index == -1:
break
rezult.append(index)
summ += objects[index][0]
price += objects[index][1]
for i in rezult:
print(i + 1)
print(summ, price)
Объяснение:
При необходимости могу пояснить построчно.
#include <iostream>
#include <cmath>
signed main()
{
setlocale(LC_ALL, "Rus");
int cnt, countBoys = 0, countGirls = 0, sumHeightBoys = 0, sumHeightGirls = 0;
std::cout << "Введите количество детей в классе: ";
std::cin >> cnt;
int* height = new int[cnt];
for (int i = 0; i < cnt; i++)
{
std::cout << "Рост ребёнка №" << i + 1 << ": ";
std::cin >> height[i];
if (height[i] < 0) {
countBoys++;
sumHeightBoys += height[i];
}
else {
countGirls++;
sumHeightGirls += height[i];
}
}
std::cout << "Средний рост мальчиков: " << abs(sumHeightBoys) / countBoys << std::endl;
std::cout << "Средний рост девочек: " << sumHeightGirls / countGirls << std::endl;
return 0;
}