|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
struct Input {
|
|
|
|
@ -9,20 +10,43 @@ struct Input {
|
|
|
|
|
size_t number_count;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
find_minmax(vector<double> numbers, double& min, double& max, size_t number_count) {
|
|
|
|
|
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
|
|
|
|
if (numbers.empty()) return;
|
|
|
|
|
|
|
|
|
|
min = numbers[0];
|
|
|
|
|
max = numbers[0];
|
|
|
|
|
for (size_t i = 1; i < number_count; i++ ){
|
|
|
|
|
if(numbers[i] < min){
|
|
|
|
|
min = numbers[i];
|
|
|
|
|
for (double num : numbers) {
|
|
|
|
|
if (num < min) min = num;
|
|
|
|
|
if (num > max) max = num;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) {
|
|
|
|
|
if (numbers.empty() || bin_count == 0) return {};
|
|
|
|
|
|
|
|
|
|
double min, max;
|
|
|
|
|
find_minmax(numbers, min, max);
|
|
|
|
|
|
|
|
|
|
vector<size_t> bins(bin_count);
|
|
|
|
|
double bin_size = (max - min) / bin_count;
|
|
|
|
|
|
|
|
|
|
for (double num : numbers) {
|
|
|
|
|
bool found = false;
|
|
|
|
|
for (size_t j = 0; j < bin_count - 1 && !found; ++j) {
|
|
|
|
|
double lo = min + j * bin_size;
|
|
|
|
|
double hi = min + (j + 1) * bin_size;
|
|
|
|
|
if (lo <= num && num < hi) {
|
|
|
|
|
bins[j]++;
|
|
|
|
|
found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (numbers[i] > max){
|
|
|
|
|
max = numbers[i];
|
|
|
|
|
if (!found) {
|
|
|
|
|
bins[bin_count - 1]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bins;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Input input_data() {
|
|
|
|
|
Input in;
|
|
|
|
@ -37,58 +61,29 @@ Input input_data() {
|
|
|
|
|
return in;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
|
|
void show_histogram_text(const vector<size_t>& bins, size_t max_width = 80) {
|
|
|
|
|
if (bins.empty()) return;
|
|
|
|
|
|
|
|
|
|
Input in = input_data();
|
|
|
|
|
size_t max_count = *max_element(bins.begin(), bins.end());
|
|
|
|
|
if (max_count == 0) return;
|
|
|
|
|
|
|
|
|
|
double minc, maxc;
|
|
|
|
|
find_minmax(in.numbers, minc, maxc, in.number_count);
|
|
|
|
|
for (size_t count : bins) {
|
|
|
|
|
if (count < 100) cout << " ";
|
|
|
|
|
if (count < 10) cout << " ";
|
|
|
|
|
|
|
|
|
|
vector<size_t> bins(in.bin_count);
|
|
|
|
|
double bin_size = (maxc - minc) / in.bin_count;
|
|
|
|
|
size_t bin_max_size = 0;
|
|
|
|
|
for (size_t i = 0; i < in.number_count; i++) {
|
|
|
|
|
bool found = false;
|
|
|
|
|
for (size_t j = 0; (j < in.bin_count - 1) && !found; j++) {
|
|
|
|
|
auto lo = minc + j * bin_size;
|
|
|
|
|
auto hi = minc + (j + 1) * bin_size;
|
|
|
|
|
if ((lo <= in.numbers[i]) && (in.numbers[i] < hi)) {
|
|
|
|
|
bins[j]++;
|
|
|
|
|
found = true;
|
|
|
|
|
if (bins[j]> bin_max_size){
|
|
|
|
|
bin_max_size = bins[j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!found) {
|
|
|
|
|
bins[in.bin_count - 1]++;
|
|
|
|
|
if (bins[in.bin_count - 1]> bin_max_size){
|
|
|
|
|
bin_max_size = bins[in.bin_count - 1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
size_t height;
|
|
|
|
|
for(int i = 0; i<in.bin_count; i++){
|
|
|
|
|
if(bins[i]<100){
|
|
|
|
|
cout<<" ";
|
|
|
|
|
if(bins[i]<10){
|
|
|
|
|
cout<<" ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cout<<bins[i]<<"|";
|
|
|
|
|
if(bin_max_size >= MAX_ASTERISK){
|
|
|
|
|
height = floor(static_cast<double>(bins[i])*76.0f/static_cast<double>(bin_max_size));
|
|
|
|
|
} else {
|
|
|
|
|
height = bins[i];
|
|
|
|
|
}
|
|
|
|
|
for(int j = 0; j<height; j++){
|
|
|
|
|
cout<<"*";
|
|
|
|
|
}
|
|
|
|
|
cout << count << "|";
|
|
|
|
|
|
|
|
|
|
cout<<endl;
|
|
|
|
|
size_t bar_length = (count * max_width) / max_count;
|
|
|
|
|
for (size_t i = 0; i < bar_length; ++i) {
|
|
|
|
|
cout << "*";
|
|
|
|
|
}
|
|
|
|
|
cout << endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
auto in = input_data();
|
|
|
|
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
|
|
|
|
show_histogram_text(bins);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|