
Користувач вводить прибуток фірми за кожен місяць протягом року. Потім користувач вводить діапазон
(наприклад, 3 і 6 - пошук між 3-ім і 6-им місяцем). Необхідно визначити місяць, в якому прибуток був мінімальним з урахуванням обраного діапазону. С++

Ответы на вопрос

#include <iostream>
#include <limits.h>
using namespace std;
int main() {
int profit[12];
int startMonth, endMonth;
// Enter the profit for each month
cout << "Enter the profit for each month: " << endl;
for (int i = 0; i < 12; i++) {
cin >> profit[i];
// Check if the profit is a positive number
if (profit[i] < 0) {
cout << "Invalid input. Profit should be a positive number." << endl;
return 0;
}
}
// Enter the range start and end months
cout << "Enter the start month: ";
cin >> startMonth;
cout << "Enter the end month: ";
cin >> endMonth;
// Check if the start and end months are within the range of 1 to 12
if (startMonth < 1 || startMonth > 12 || endMonth < 1 || endMonth > 12) {
cout << "Invalid input. Month should be between 1 and 12." << endl;
return 0;
}
// Check if the start month is less than or equal to the end month
if (startMonth > endMonth) {
cout << "Invalid input. Start month should be less than or equal to the end month." << endl;
return 0;
}
// Initialize the minimum profit and the month with the minimum profit
int minProfit = INT_MAX;
int minMonth = -1;
// Find the month with the minimum profit within the given range
for (int i = startMonth - 1; i < endMonth; i++) {
if (profit[i] < minProfit) {
minProfit = profit[i];
minMonth = i + 1;
}
}
// Print the month with the minimum profit
cout << "The month with the minimum profit is: " << minMonth << endl;
return 0;
}



Ось програмний код на C++, який вирішує ваше завдання:
cpp#include <iostream>
#include <vector>
int main() {
std::vector<double> incomes;
// Ввід прибутку за кожен місяць
for (int i = 1; i <= 12; ++i) {
double income;
std::cout << "Введіть прибуток за місяць " << i << ": ";
std::cin >> income;
incomes.push_back(income);
}
// Ввід діапазону
int startMonth, endMonth;
std::cout << "Введіть початковий та кінцевий місяць діапазону: ";
std::cin >> startMonth >> endMonth;
// Перевірка правильності введених значень діапазону
if (startMonth < 1 || startMonth > 12 || endMonth < 1 || endMonth > 12 || startMonth > endMonth) {
std::cout << "Некоректний діапазон місяців.\n";
return 0;
}
// Знаходження мінімального прибутку в обраному діапазоні
double minIncome = incomes[startMonth - 1];
int minIncomeMonth = startMonth;
for (int i = startMonth; i <= endMonth; ++i) {
if (incomes[i - 1] < minIncome) {
minIncome = incomes[i - 1];
minIncomeMonth = i;
}
}
// Виведення результату
std::cout << "Мінімальний прибуток у діапазоні з місяця " << startMonth << " до " << endMonth
<< " був у місяці " << minIncomeMonth << " і становив " << minIncome << ".\n";
return 0;
}
Цей код запитує у користувача прибуток за кожен місяць року та діапазон місяців. Потім він знаходить мінімальний прибуток у заданому діапазоні і виводить його значення та номер місяця.


Топ вопросов за вчера в категории Информатика







Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili