C/C++

Составить программу на c++ УСПОЛЬЗУЯ МАССИВ!!!

Составить программу на c++ УСПОЛЬЗУЯ МАССИВ. В массиве хранится информация о стоимости каждой из M книг. Определить количество самых дешевых книг (с одинаковой минимальной ценой).
Тим Dz
Тим Dz
128
#include <algorithm>
#include <iostream>
#include <ctime>
using namespace std;

int main() {
const int SIZE = 50;
int prices[SIZE];
for (int& i : prices) i = rand() % 1501 + 500;
cout << count(prices, prices + SIZE, *min_element(prices, prices + SIZE)) << "\n";
}
IJ
Ibragim Jum
6 243
Лучший ответ
#include <algorithm>
#include <array>
#include <iomanip>
#include <iostream>
#include <random>
#include <string>
using namespace std;
class Money {
public:
Money() : rub(0), kop(0) {}
Money(int rub, int kop) : rub(rub), kop(kop) {}
Money(const string& value) {
rub = stoi(value);
kop = stoi(string(value.begin() + value.find('.'), value.end()));
}
string money()const {
auto str = to_string(rub) + ".";
if (kop < 10) str += "0";
str += to_string(kop);
return str;
}
private:
int rub;
int kop;
friend bool operator<(const Money& a, const Money& b) {
if (a.rub < b.rub) return true;
else if (a.rub == b.rub) return a.kop < b.kop;
return false;
}
friend bool operator==(const Money& a, const Money& b) {
return a.rub == b.rub && a.kop == b.kop;
}
friend ostream& operator<<(ostream& out, const Money& m) {
return out << m.money();
}
};
Money pricebook(int max, int min) {
static mt19937 gen{ random_device()() };
static uniform_int_distribution<> rub(min, max);
static uniform_int_distribution<> kop(0, 99);
int r = 0, k = 0;
do {
r = rub(gen);
k = kop(gen);
} while (r + k == 0);
return { r, k};
}
int main() {
const int m = 1000;
array<Money, m> books;
for (auto& book : books) book = pricebook(2, 1);
for (const auto& book : books) cout << setw(5) << book;
puts("");
auto& min = *min_element(books.begin(), books.end());
cout << "Min: " << min << '\n';
auto n = count(books.begin(), books.end(), min);
cout << "Count: " << n << '\n';
system("pause > nul");
}
Саня Евдокимов
Саня Евдокимов
67 615