#include <vector> #include <iostream> using namespace std; struct Input { vector<double> vec; size_t korz{}; }; Input input_data() { Input in; size_t n, korz; cerr << "Number of elem "; cin >> n; in.vec.resize(n); for (size_t i = 0; i < n; i++) cin >> in.vec[i]; cerr << "Enter bin count: "; cin >> in.korz; return in; } void find_minmax(vector<double> vec, double& min, double& max) { min = vec[0]; max = vec[0]; for (double x : vec) { if (x < min) { min = x; } else { max = x; } } } int main() { auto in = input_data(); vector<size_t> bins(in.korz); double mn, mx; find_minmax(in.vec, mn, mx); float bin_size = (mx - mn) / in.korz; for (size_t i = 0; i < in.vec.size(); i++) { bool fl = false; for (size_t j = 0; (j < in.korz - 1) && !fl; j++) { auto lo = mn + j * bin_size; auto hi = mn + (j + 1) * bin_size; if ((lo <= in.vec[i]) && (in.vec[i] < hi)) { bins[j]++; fl = true; } } if (!fl) { bins[in.korz - 1]++; } } bool gigant = false; auto spaces = 0; size_t mx_count = 0; for (auto x : bins) { if (x > 76) { gigant = true; } if (x > mx_count) { mx_count = x; } auto len = 0; while (x > 0) { x /= 10; len++; } if (len > spaces) { spaces = len; } } if (spaces == 1) { for (size_t i = 0; i < bins.size(); i++) { cout << " " << bins[i] << "|"; if (gigant) { if (bins[i] == mx_count) { for (size_t j = 0; j < 76; j++) { cout << "*"; } } else { for (size_t j = 0; j < 76 * static_cast<double>(bins[i]) / mx_count; j++) { cout << "*"; } } } else { for (size_t j = 0; j < bins[i]; j++) { cout << "*"; } cout << endl; } } } else { for (size_t i = 0; i < bins.size(); i++) { int len = 1; int k = bins[i]; for (; k /= 10; ++len); while (len < spaces) { cout << " "; len++; } cout << bins[i]; cout << "|"; if (gigant) { if (bins[i] == mx_count) { for (size_t j = 0; j < 76; j++) { cout << "*"; } } else { for (size_t j = 0; j < (76 * static_cast<double>(bins[i]) / mx_count - 1); j++) { cout << "*"; } } } else { for (size_t j = 0; j < bins[i]; j++) { cout << "*"; } } cout << endl; } } }