|
|
|
@ -2,155 +2,117 @@
|
|
|
|
|
#include <vector>
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
|
|
|
|
|
|
|
size_t number_count;
|
|
|
|
|
cerr<<"enter number count"<<endl;
|
|
|
|
|
cin>>number_count;
|
|
|
|
|
|
|
|
|
|
vector<double> numbers(number_count);
|
|
|
|
|
cerr<<"put elements with enter"<<endl;
|
|
|
|
|
for(int i = 0; i<number_count; i++){
|
|
|
|
|
cin>>(numbers[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t bin_count;
|
|
|
|
|
cerr<<"enter bin count"<<endl;
|
|
|
|
|
cin>>bin_count;
|
|
|
|
|
|
|
|
|
|
vector<size_t> bins(bin_count);//ñ÷åò÷èêè â êîðçèíàõ . íà÷ çíà÷åíèÿ íóëè
|
|
|
|
|
|
|
|
|
|
double min = numbers[0];//ìàêñ è ìèí ÷èñëà â ïîòîêå
|
|
|
|
|
double max = numbers[0];
|
|
|
|
|
for(double x: numbers){
|
|
|
|
|
if(x<min){
|
|
|
|
|
struct Input {
|
|
|
|
|
vector<double> numbers;
|
|
|
|
|
size_t bin_count{};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Input
|
|
|
|
|
input_data() {
|
|
|
|
|
Input in;
|
|
|
|
|
|
|
|
|
|
cerr << "Enter number count: ";
|
|
|
|
|
size_t number_count;
|
|
|
|
|
cin >> number_count;
|
|
|
|
|
|
|
|
|
|
in.numbers.resize(number_count);
|
|
|
|
|
for (size_t i = 0; i < number_count; i++) {
|
|
|
|
|
cin >> in.numbers[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cerr << "Enter number bin: ";
|
|
|
|
|
cin >> in.bin_count;
|
|
|
|
|
|
|
|
|
|
return in;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
|
|
|
|
min = numbers[0];
|
|
|
|
|
max = numbers[0];
|
|
|
|
|
for (double x : numbers)
|
|
|
|
|
{
|
|
|
|
|
if (x < min)
|
|
|
|
|
{
|
|
|
|
|
min = x;
|
|
|
|
|
} else if(x>max) {
|
|
|
|
|
}
|
|
|
|
|
else if (x > max)
|
|
|
|
|
{
|
|
|
|
|
max = x;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
vector<size_t>
|
|
|
|
|
make_histogram(const vector<double>& numbers, size_t bin_count) {
|
|
|
|
|
double min, max;
|
|
|
|
|
find_minmax(numbers, min, max);
|
|
|
|
|
|
|
|
|
|
double bin_size = (max - min)/bin_count;
|
|
|
|
|
vector<size_t> bins(bin_count);
|
|
|
|
|
double bin_size = (max - min) / bin_count;
|
|
|
|
|
|
|
|
|
|
for(size_t i = 0; i<number_count; i++){
|
|
|
|
|
for (size_t i = 0; i < numbers.size(); i++) {
|
|
|
|
|
bool found = false;
|
|
|
|
|
for(size_t j = 0; (j<bin_count-1)&&!found; j++){
|
|
|
|
|
auto lo = min+j*bin_size;
|
|
|
|
|
auto hi = min+(j+1)*bin_size;
|
|
|
|
|
if(lo<=numbers[i] && numbers[i]<hi){
|
|
|
|
|
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
|
|
|
|
|
auto lo = min + j * bin_size;
|
|
|
|
|
auto hi = min + (j + 1) * bin_size;
|
|
|
|
|
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
|
|
|
|
bins[j]++;
|
|
|
|
|
found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(!found){
|
|
|
|
|
bins[bin_count-1]++;
|
|
|
|
|
if (!found) {
|
|
|
|
|
bins[bin_count - 1]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double max_b = bins[0];//ïîèñê ìàêñ êîëè÷åñòâà ÷èñåë â êîðçèíå
|
|
|
|
|
for(double x: bins){
|
|
|
|
|
if(x>max_b){
|
|
|
|
|
max_b = x;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//çàùèòà
|
|
|
|
|
double sum=0;
|
|
|
|
|
double sr;
|
|
|
|
|
for(int i=0; i<bin_count; i++){
|
|
|
|
|
sum=sum+bins[i];
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
sr=sum/bin_count;
|
|
|
|
|
return bins;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (max_b <= 76){
|
|
|
|
|
for (int i = 0; i < bin_count; i++){
|
|
|
|
|
if (bins[i] < 10){
|
|
|
|
|
cout << " ";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
cout << " ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cout<<bins[i];
|
|
|
|
|
cout<<"|";
|
|
|
|
|
|
|
|
|
|
if (bins[i]<sr){
|
|
|
|
|
for (int j = 0; j < bins[i]; j++){
|
|
|
|
|
cout << "*";
|
|
|
|
|
}
|
|
|
|
|
for (int m = 0; m < sr-bins[i]; m++){
|
|
|
|
|
cout << "-";
|
|
|
|
|
}
|
|
|
|
|
void show_histogram_text(const vector<size_t>& bins, size_t max_count, size_t MAX_ASTERISK, size_t bin_count){
|
|
|
|
|
for (size_t i = 0; i < bin_count; i++){
|
|
|
|
|
if (bins[i]<10) {
|
|
|
|
|
cout << " " << bins[i] << "|";
|
|
|
|
|
}
|
|
|
|
|
else if (bins[i]<100) {
|
|
|
|
|
cout << " " << bins[i] << "|";
|
|
|
|
|
}
|
|
|
|
|
else if (bins[i]<1000) {
|
|
|
|
|
cout << bins[i] << "|";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bins[i]>sr){
|
|
|
|
|
for (int j = 0; j < sr; j++){
|
|
|
|
|
cout << "*";
|
|
|
|
|
}
|
|
|
|
|
for (int m = 0; m < bins[i]-sr; m++){
|
|
|
|
|
cout << "+";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cout << endl;
|
|
|
|
|
size_t height;
|
|
|
|
|
|
|
|
|
|
if (max_count<=MAX_ASTERISK) {
|
|
|
|
|
height = bins[i];
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
|
|
|
|
}
|
|
|
|
|
for (size_t j = 0; j < height; j++) {
|
|
|
|
|
cout << "*";
|
|
|
|
|
}
|
|
|
|
|
cout << endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else{
|
|
|
|
|
for (int i = 0; i < bin_count; i++){
|
|
|
|
|
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_b);
|
|
|
|
|
if (bins[i] < 10){
|
|
|
|
|
cout << " ";
|
|
|
|
|
}
|
|
|
|
|
else if (bins[i] < 100) {
|
|
|
|
|
cout << " ";
|
|
|
|
|
}
|
|
|
|
|
cout<<bins[i];
|
|
|
|
|
cout<<"|";
|
|
|
|
|
|
|
|
|
|
if (bins[i]<sr){//çàùèòà
|
|
|
|
|
for (int j = 0; j < height; j++){
|
|
|
|
|
cout << "*";
|
|
|
|
|
}
|
|
|
|
|
for (int m = 0; m < sr-height; m++){
|
|
|
|
|
cout << "-";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bins[i]>sr){//çàùèòà
|
|
|
|
|
for (int j = 0; j < sr; j++){
|
|
|
|
|
cout << "*";
|
|
|
|
|
}
|
|
|
|
|
for (int m = 0; m < height-sr; m++){
|
|
|
|
|
cout << "+";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cout << endl;
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
|
|
|
|
|
|
|
auto in = input_data();
|
|
|
|
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
size_t max_count = bins[0];
|
|
|
|
|
for (size_t i = 0; i < bins.size(); i++) {
|
|
|
|
|
if (bins[i] > max_count) {
|
|
|
|
|
max_count = bins[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
show_histogram_text(bins, max_count, MAX_ASTERISK, in.bin_count);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|