Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

86 строки
2.0 KiB
C++

#include <iostream>
#include <vector>
using namespace std;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_LENGTH = SCREEN_WIDTH - 3 - 1;
struct Input {
vector<double> numbers;
size_t bin_count{};
};
Input
input_data(){
size_t number_count;
cin >> number_count;
Input in;
in.numbers.resize(number_count);
vector<double> numbers(number_count);
for (size_t i = 0; i < number_count; i++) {
cin >> in.numbers[i];
}
cin >> in.bin_count;
return in;
}
int
main() {
Input in = input_data();
auto min = in.numbers[0];
auto max = in.numbers[0];
for (auto x : in.numbers) {
if (x < min) {
min = x;
}
else if (x > max) {
max = x;
}
}
double bin_size = (max - min) / in.bin_count;
vector<size_t> bins(in.bin_count);
for (auto i = 0; i < in.number_count; i++) {
bool found = false;
for (auto j = 0; (j < in.bin_count - 1) && !found ; j++) {
if ((min + j * bin_size <= in.numbers[i]) && (in.numbers[i] < min + (j + 1) * bin_size)) {
bins[j] += 1;
found = true;
}
}
if (!found) bins[in.bin_count - 1]++;
}
auto max_count = bins[0];
for (auto x : bins) {
if (x > max_count) {
max_count = x;
}
}
for (i = 0; i < in.bin_count; i++) {
j = 100;
while (bins[i] < j) {
cout << " ";
j /= 10;
}
cout << bins[i] << "|";
if (max_count > MAX_LENGTH) {
auto count = bins[i];
size_t height = MAX_LENGTH * (static_cast<double>(count) / max_count);
j = 0;
while (j < height) {
cout << "*";
j++;
}
}
else {
j = 0;
while (j < bins[i]) {
cout << "*";
j++;
}
}
cout << endl;
}
return 0;
}