Сommit
0b0a09baf1
@ -0,0 +1,109 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
size_t bin_count, numbers_count;
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
|
||||
cerr << "Enter number count:";
|
||||
cin >> numbers_count;
|
||||
|
||||
vector<double> numbers ( numbers_count );
|
||||
for (size_t i=0; i < numbers_count; i++){
|
||||
cin >> numbers[i];
|
||||
}
|
||||
cin >> bin_count;
|
||||
|
||||
vector<size_t> bins ( bin_count );
|
||||
double minx = numbers[0];
|
||||
double maxx = numbers[0];
|
||||
|
||||
for (double x : numbers){
|
||||
if (x < minx){
|
||||
minx = x;
|
||||
} else if (x > maxx){
|
||||
maxx = x;
|
||||
}
|
||||
}
|
||||
|
||||
double bin_size = (maxx - minx) / bin_count;
|
||||
for (size_t i=0; i < numbers_count; i++ ){
|
||||
bool found = false;
|
||||
for (size_t j = 0; ( j < bin_count - 1 ) && !found; j++ ){
|
||||
auto lo = minx + j * bin_size;
|
||||
auto hi = minx + ( j + 1 ) * bin_size;
|
||||
if (lo <= numbers[i] && ( numbers[i] < hi )){
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if ( !found ){
|
||||
bins[bin_count-1]++;
|
||||
}
|
||||
}
|
||||
|
||||
size_t maxbin = bins[0];
|
||||
for (size_t i=1; i < bin_count; i++){
|
||||
if (maxbin < bins[i]){
|
||||
maxbin = bins[i];
|
||||
}
|
||||
}
|
||||
if (maxbin <= MAX_ASTERISK){
|
||||
for (size_t i=0; i < bin_count; i++){
|
||||
if ((bins[i] < 1000)&&(bins[i] > 99)){
|
||||
cout << bins[i] << "|";
|
||||
for ( size_t j=0; j < bins[i]; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
} else if ((bins[i] < 100)&&(bins[i]>9)) {
|
||||
cout << " " << bins[i] << "|";
|
||||
for ( size_t j=0; j < bins[i]; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
} else if ( bins[i] < 10 ){
|
||||
cout << " " << bins[i]<< "|";
|
||||
for ( size_t j=0; j < bins[i]; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (size_t i=0; i < bin_count; i++){
|
||||
size_t heightG= MAX_ASTERISK * (static_cast<double>(bins[i]) / maxbin);
|
||||
|
||||
if ((bins[i] < 1000)&&(bins[i] > 99)){
|
||||
cout << bins[i] << "|";
|
||||
for ( size_t j=0; j < heightG; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
} else if ((bins[i] < 100)&&(bins[i]>9)) {
|
||||
cout << " " << bins[i] << "|";
|
||||
for (size_t j=0; j < heightG; j++){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
} else if (bins[i] < 10){
|
||||
cout << " " << bins[i]<< "|";
|
||||
|
||||
for (size_t j=0; j < heightG; j++){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Загрузка…
Ссылка в новой задаче