• Предмет: Информатика
  • Автор: taniyham
  • Вопрос задан 4 месяца назад

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

Ответы

Ответ дал: YanCrab
1

#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;

}

Вас заинтересует