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_27.cpp
// Горбацевич Андрей
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
inline int sign(int i) {
return (i < 0? -1 : 1);
}
int count_sign_changes(const vector<int> &vec) {
int c = 0;
for (size_t i = 1; i < vec.size(); i++) {
c += sign(vec[i-1]) != sign(vec[i]);
}
return c;
}
vector<int> read_vec(istream &reader) {
vector<int> out;
while (!reader.eof() && reader.good()) {
int i;
reader >> i;
out.push_back(i);
}
return out;
}
// -5 1 1 -5 1
int main() {
ifstream inf("in.txt");
if (!inf.is_open())
{
cerr << "Unable to open file" << endl;
return 1;
}
vector<int> v = read_vec(inf);
int c = count_sign_changes(v);
cout << "Count of changes: " << c << endl;
return 0;
}

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;
}

View file

@ -0,0 +1,48 @@
// ac_9_29.cpp
// Горбацевич Андрей
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
void move_positive_first(vector<float> &vec) {
bool flag;
do {
flag = false;
for (size_t i = 0; i < vec.size() - 1; i++) {
if (vec[i] < 0 && vec[i+1] > 0) {
swap(vec[i], vec[i+1]);
flag = true;
}
}
} while (flag);
}
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);
move_positive_first(v);
cout << "Resulting vector: ";
for (float f : v) {
cout << f << " ";
}
cout << endl;
return 0;
}