code: min,max
Этот коммит содержится в:
86
main.cpp
86
main.cpp
@@ -1,70 +1,97 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
const size_t SCREEN_WIDTH = 80;
|
const size_t SCREEN_WIDTH = 80;
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
struct Input
|
||||||
|
{
|
||||||
|
vector<double> numbers;
|
||||||
|
size_t bin_count{};
|
||||||
|
};
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
|
Input input_data()
|
||||||
{
|
{
|
||||||
|
|
||||||
cout << "Enter A and B: ";
|
|
||||||
int a, b;
|
|
||||||
cin >> a >> b;
|
|
||||||
|
|
||||||
size_t number_count, bin_count;
|
size_t number_count, bin_count;
|
||||||
|
|
||||||
cerr << "Enter number count: ";
|
cerr << "Enter number count: ";
|
||||||
cin >> number_count;
|
cin >> number_count;
|
||||||
cout << "A + B = " << a + b << '\n'
|
|
||||||
<< "A - B = " << a - b << '\n';
|
|
||||||
|
|
||||||
|
Input in;
|
||||||
|
in.numbers.resize(number_count);
|
||||||
|
|
||||||
cerr << "Enter numbers: ";
|
cerr << "Enter numbers: ";
|
||||||
|
|
||||||
vector<double> numbers(number_count);
|
vector<double> numbers(number_count);
|
||||||
|
// vector<double> numbers(number_count);
|
||||||
|
|
||||||
for (int i = 0; i < number_count; i++)
|
for (int i = 0; i < number_count; i++)
|
||||||
|
for (size_t i = 0; i < number_count; i++)
|
||||||
{
|
{
|
||||||
cin >> numbers[i];
|
cin >> numbers[i];
|
||||||
|
cin >> in.numbers[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
cerr << "Enter bin count: ";
|
cerr << "Enter bin count: ";
|
||||||
cin >> bin_count;
|
cin >> bin_count;
|
||||||
|
cerr << " number of baskets: ";
|
||||||
|
cin >> in.bin_count;
|
||||||
|
|
||||||
vector<double> bins(bin_count);
|
vector<double> bins(bin_count);
|
||||||
vector<int> count(number_count);
|
vector<int> count(number_count);
|
||||||
vector<double> height(bin_count);
|
vector<double> height(bin_count);
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
int max_count = bins[0];
|
int max_count = bins[0];
|
||||||
double min = numbers[0];
|
double min = numbers[0];
|
||||||
double max = numbers[0];
|
double max = numbers[0];
|
||||||
|
void find_minmax(const vector<double> &numbers, double &min, double &max)
|
||||||
|
{
|
||||||
|
min = numbers[0];
|
||||||
for (double x : numbers)
|
for (double x : numbers)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (x < min)
|
@ -46,32 +55,44 @@ int main()
|
||||||
{
|
|
||||||
min = x;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (x > max)
|
|
||||||
{
|
|
||||||
max = x;
|
max = x;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
double bin_size = (max - min) / bin_count;
|
double bin_size = (max - min) / bin_count;
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Input in = input_data();
|
||||||
|
|
||||||
for (size_t i = 0; i < number_count; i++)
|
for (size_t i = 0; i < number_count; i++)
|
||||||
|
vector<int> bins(in.bin_count);
|
||||||
|
vector<double> height(in.bin_count);
|
||||||
|
|
||||||
|
int max_count = bins[0];
|
||||||
|
double min = in.numbers[0];
|
||||||
|
double max = in.numbers[0];
|
||||||
|
|
||||||
|
find_minmax(in.numbers, min, max);
|
||||||
|
|
||||||
|
double bin_size = (max - min) / in.bin_count;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < in.numbers.size(); i++)
|
||||||
{
|
{
|
||||||
bool found = false;
|
bool found = false;
|
||||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
|
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
|
||||||
|
for (size_t j = 0; (j < in.bin_count - 1) && !found; j++)
|
||||||
{
|
{
|
||||||
auto lo = min + j * bin_size;
|
auto lo = min + j * bin_size;
|
||||||
auto hi = min + (j + 1) * bin_size;
|
auto hi = min + (j + 1) * bin_size;
|
||||||
|
|
||||||
if ((lo <= numbers[i]) && (numbers[i] < hi))
|
if ((lo <= numbers[i]) && (numbers[i] < hi))
|
||||||
|
if ((lo <= in.numbers[i]) && (in.numbers[i] < hi))
|
||||||
{
|
{
|
||||||
bins[j]++;
|
bins[j]++;
|
||||||
count[j]++;
|
count[j]++;
|
||||||
@@ -72,46 +99,37 @@ int main()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!found )
|
||||||
if (!found)
|
if (!found)
|
||||||
{
|
{
|
||||||
bins[bin_count - 1]++;
|
bins[bin_count - 1]++;
|
||||||
count[bin_count - 1]++;
|
count[bin_count - 1]++;
|
||||||
|
bins[in.bin_count - 1]++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < bin_count; i++)
|
for (int i = 0; i < bin_count; i++)
|
||||||
|
for (int i = 0; i < in.bin_count; i++)
|
||||||
{
|
{
|
||||||
if (bins[i] > max_count)
|
if (bins[i] > max_count)
|
||||||
{
|
{
|
||||||
max_count = bins[i];
|
@ -79,12 +100,17 @@ int main()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < bin_count; i++)
|
for (int i = 0; i < bin_count; i++)
|
||||||
|
for (int i = 0; i < in.bin_count; i++)
|
||||||
{
|
{
|
||||||
height[i] = (MAX_ASTERISK * count[i]) / max_count;
|
height[i] = (MAX_ASTERISK * count[i]) / max_count;
|
||||||
|
if (bins[i] > MAX_ASTERISK)
|
||||||
|
{
|
||||||
|
height[i] = (MAX_ASTERISK * bins[i]) / max_count;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
height[i] = bins[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < bin_count; i++)
|
for (int i = 0; i < bin_count; i++)
|
||||||
|
for (int i = 0; i < in.bin_count; i++)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (bins[i] < 100)
|
if (bins[i] < 100)
|
||||||
{
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bins[i] < 10)
|
|
||||||
{
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
|
|
||||||
cout << bins[i] << "|";
|
|
||||||
|
|
||||||
for (int j = 0; j < height[i]; j++)
|
|
||||||
{
|
|
||||||
cout << "*";
|
|
||||||
}
|
|
||||||
cout << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user