This commit is contained in:
Andrew 2019-11-18 11:02:11 +07:00
parent 0d75f30a49
commit 4744ebb565
69 changed files with 7 additions and 5 deletions

View file

@ -0,0 +1,43 @@
// ac_9_28.cpp
// Горбацевич Андрей
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;
float nearest_int_dist(float x) {
return abs(round(x) - x);
}
vector<float> read_vec(istream &reader) {
vector<float> out;
while (!reader.eof() && reader.good()) {
float i;
reader >> i;
out.push_back(i);
}
return out;
}
// 0.1 -0.1 0.4 4.0
int main() {
ifstream inf("in.txt");
if (!inf.is_open())
{
cerr << "Unable to open file" << endl;
return 1;
}
vector<float> v = read_vec(inf);
size_t index = -1;
float smallest = 1e38f;
for (size_t i = 0; i < v.size(); i++) {
if (nearest_int_dist(v[i]) < smallest) {
smallest = nearest_int_dist(v[i]);
index = i;
}
}
cout << "Nearest to decimal form: " << v[index] << endl;
return 0;
}