C/C++

Нужен Код на С++

Имеются данные о цене на хлеб в течение 12 месяцев в 6 городах России. Определить в каком городе и в каком месяце наблюдалось наибольшее увеличение цены на хлеб по сравнению с предыдущим месяцем. Буду очень благодарен
ПП
Пара П
115
Remove "system32"
Руслан Изекеев
Руслан Изекеев
1 043
Лучший ответ
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <string>
#include <array>
using namespace std;
class Bread {
public:
Bread() = default;
Bread(const string& city, const array<double, 12U>& months)
: city(city), months(months) {}
pair<size_t, double> max_height()const {
pair<size_t, double> box;
box.first = 1U;
box.second = months[1] - months[0];
for (auto i = 2U; i < months.size(); ++i) {
auto dx = months[i] - months[i - 1];
if (dx > box.second) {
box.first = i;
box.second = dx;
}
}
return box;
}
const string& location()const {
return city;
}
private:
string city;
array<double, 12U> months;
friend bool operator>(const Bread& a, const Bread& b) {
return a.max_height() > b.max_height();
}
friend istream& operator>>(istream& inp, Bread& b) {
cout << "Город: ";
getline(inp, b.city);
cout << "Цены за 12 месяцев: ";
for (auto& month : b.months) inp >> month;
inp.ignore(numeric_limits<size_t>::max(), '\n');
return inp;
}
};
int main() {
system("chcp 1251 > nul");
const string months[]{
"январь",
"февраль",
"март",
"апрель",
"май",
"июнь",
"июль",
"август",
"сентябрь",
"октябрь",
"ноябрь",
"декабрь"
};
array<Bread, 6U> cities{};
for (auto& city : cities) cin >> city;
sort(cities.begin(), cities.end(), greater<Bread>());
const auto& [month, diffirence] = cities.front().max_height();
if (diffirence > 0) {
cout << '\n'
<< cities.front().location() << ' '
<< months[month] << ' '
<< fixed << setprecision(2U) << diffirence << '\n';
} else {
puts("На протяжении года цены на хлеб ежемесячно снижались!");
}
system("pause > nul");
}
Игорь Уманцев
Игорь Уманцев
75 774
на кого учитесь?
Сергей Лаптев на пахана ))