This commit is contained in:
Andrew 2019-11-18 10:01:42 +07:00
commit 2802401eee
79 changed files with 2650 additions and 0 deletions

View file

@ -0,0 +1,15 @@
// ac_1_1.cpp
// Горбацевич Андрей
#include <iostream>
#include <cstdio>
#include <clocale>
#include <string>
using namespace std;
int main()
{
setlocale(LC_ALL, "russian");
string n, d;
cin >> n >> d;
cout << "Привет, " << d << " " << n << "!";
}

View file

@ -0,0 +1,15 @@
// ac_1_2.cpp
// Горбацевич Андрей
#include <iostream>
#include <cstdio>
#include <clocale>
#include <cmath>
using namespace std;
int main()
{
setlocale(LC_ALL, "russian");
double a;
cin >> a;
cout << a*a*a*sqrt(2.f)/12;
}

View file

@ -0,0 +1,15 @@
// ac_1_3.cpp
// Горбацевич Андрей
#include <iostream>
#include <cstdio>
#include <clocale>
#include <cmath>
using namespace std;
int main()
{
setlocale(LC_ALL, "russian");
double a;
cin >> a;
cout << 2*a/9.81;
}

View file

@ -0,0 +1,19 @@
// ac_1_4.cpp
// Горбацевич Андрей
#include <iostream>
#include <cstdio>
#include <clocale>
#include <cmath>
using namespace std;
int main()
{
setlocale(LC_ALL, "russian");
int d,h,m;
cin >> m;
d = m / (60 * 24);
m = m - (d * 60 * 24);
h = m / 60;
m = m - (h * 60);
printf("%d сутки(ок), %d часа(ов), %d минут(а)", d, h, m);
}

View file

@ -0,0 +1,36 @@
// ac_10_30.cpp
// Горбацевич Андрей
#include <iostream>
#include <cstring>
#define MAX_STRING 5000
using namespace std;
bool is_palindrome(const char *s) {
bool flag = true;
size_t i = 0;
while (flag && i < strlen(s)/2) {
char cc = *(s + i);
char lc = *(s + strlen(s)-i-1);
flag = (cc == lc);
i++;
}
return flag;
}
void read_string(istream &reader, char *where, char delimiter) {
char c;
size_t i = 0;
while ((c = reader.get()) != delimiter && reader.good()) {
*(where + i++) = c;
}
*(where + i) = '\0';
}
int main() {
char *s = new char[MAX_STRING];
cout << "Input >>>";
read_string(cin, s, '\n');
cout << "String is" << (is_palindrome(s)? "" : "n't") << " palindrome" << endl;
return 0;
}

View file

@ -0,0 +1,36 @@
// ac_10_31.cpp
// Горбацевич Андрей
#include <iostream>
#include <cstring>
#define MAX_STRING 5000
using namespace std;
void del_first_last(char *s) {
if (strlen(s) < 2) {
*s = '\0';
}
else {
strncpy(s, s + 1, strlen(s) - 2);
*(s + strlen(s) - 2) = '\0';
}
}
void read_string(istream &reader, char *where, char delimiter) {
char c;
size_t i = 0;
while ((c = reader.get()) != delimiter && reader.good()) {
*(where + i++) = c;
}
*(where + i) = '\0';
}
int main() {
char *s = new char[MAX_STRING];
cout << "Input >>>";
read_string(cin, s, '\n');
cout << "Before: " << s << endl;
del_first_last(s);
cout << "After: " << s << endl;
return 0;
}

View file

@ -0,0 +1,35 @@
// ac_10_32.cpp
// Горбацевич Андрей
#include <iostream>
#include <cstring>
#define MAX_STRING 5000
using namespace std;
void capitalize(char *s) {
*s = (char)toupper(*s);
for (size_t i = 1; i < strlen(s); i++) {
if (*(s + i) == ' ') {
*(s + i + 1) = (char)toupper(*(s + i + 1));
}
}
}
void read_string(istream &reader, char *where, char delimiter) {
char c;
size_t i = 0;
while ((c = reader.get()) != delimiter && reader.good()) {
*(where + i++) = c;
}
*(where + i) = '\0';
}
int main() {
char *s = new char[MAX_STRING];
cout << "Input >>>";
read_string(cin, s, '\n');
cout << "Before: " << s << endl;
capitalize(s);
cout << "After: " << s << endl;
return 0;
}

View file

@ -0,0 +1,34 @@
// ac_11_33.cpp
// Горбацевич Андрей
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void read_string(istream &reader, string &where, char delimiter) {
char c;
while ((c = reader.get()) != delimiter && reader.good()) {
where += c;
}
}
int main() {
vector<string> strs;
ifstream inf("in.txt");
if (!inf.is_open())
{
cerr << "Unable to open file" << endl;
return 1;
}
while (!inf.eof()) {
string s;
read_string(inf, s, '\n');
strs.push_back(s);
}
for (auto it = strs.end(); it-- != strs.begin();) {
cout << *it << endl;
}
return 0;
}

View file

@ -0,0 +1,52 @@
// ac_11_34.cpp
// Горбацевич Андрей
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int count_words(const string &str) {
bool inSpaces = true;
int numWords = 0;
auto it = begin(str);
while (*it != '\0')
{
if (isspace(*it))
{
inSpaces = true;
}
else if (inSpaces)
{
numWords++;
inSpaces = false;
}
++it;
}
return numWords;
}
void read_string(istream &reader, string &where, char delimiter) {
char c;
while ((c = reader.get()) != delimiter && reader.good()) {
where += c;
}
}
int main() {
ifstream inf("in.txt");
if (!inf.is_open())
{
cerr << "Unable to open file" << endl;
return 1;
}
while (!inf.eof()) {
string s;
read_string(inf, s, '\n');
cout << count_words(s) << " words: " << s << endl;
}
return 0;
}

View file

@ -0,0 +1,37 @@
// ac_11_35.cpp
// Горбацевич Андрей
#include <iostream>
#include <fstream>
#include <string>
#define MAX_STRING 5000
using namespace std;
string get_short_name(const string &full_name) {
char s[MAX_STRING], n[MAX_STRING], f[MAX_STRING], out[MAX_STRING];
sscanf(full_name.c_str(), "%s %s %s", s, n, f);
sprintf(out, "%s %c.%c.", s, n[0], f[0]);
return string(out);
}
void read_string(istream &reader, string &where, char delimiter) {
char c;
while ((c = reader.get()) != delimiter && reader.good()) {
where += c;
}
}
int main() {
ifstream inf("in.txt");
if (!inf.is_open())
{
cerr << "Unable to open file" << endl;
return 1;
}
while (!inf.eof()) {
string s;
read_string(inf, s, '\n');
cout << get_short_name(s) << endl;
}
return 0;
}

View file

@ -0,0 +1,33 @@
// ac_2_5.cpp
// Горбацевич Андрей
#include <stdio.h>
#include <cmath>
#include <clocale>
#include <afxres.h>
using namespace std;
int main() {
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
setlocale(LC_ALL, "ru_RU.UTF-8");
int h, m;
scanf("%d %d", &h, &m);
h += m > 59;
if (h > 24) {
printf("Exception: hours > 24");
return 0;
}
printf("%02d:%02d\n", h, m);
if (h >= 6 && h < 12) {
printf("Доброе утро!");
}
else if (h >= 12 && h < 18) {
printf("Добрый день!");
}
else if (h >= 18 && h < 24) {
printf("Добрый вечер!");
}
else if (h >= 0 && h < 6){
printf("Доброй ночи!");
}
}

View file

@ -0,0 +1,25 @@
// ac_2_6.cpp
// Горбацевич Андрей
#include <stdio.h>
#include <cmath>
#include <clocale>
#include <afxres.h>
using namespace std;
int main() {
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
setlocale(LC_ALL, "ru_RU.UTF-8");
int c;
scanf("%d", &c);
int ln = c % 10;
if ((ln == 0) || (ln >= 5 && ln <= 9) || (c % 100 == 11)) {
printf("В аудитории %d студентов", c);
}
else if (ln == 1) {
printf("В аудитории %d студент", c);
}
else if (ln > 1 && ln < 5) {
printf("В аудитории %d студента", c);
}
}

View file

@ -0,0 +1,34 @@
// ac_2_7.cpp
// Горбацевич Андрей
#include <stdio.h>
#include <cmath>
#include <clocale>
#include <afxres.h>
using namespace std;
int main() {
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
setlocale(LC_ALL, "ru_RU.UTF-8");
double op1, op2;
char f;
printf("<first operand> <operator> <second operand> >>>");
scanf("%lf %s %lf", &op1, &f, &op2);
switch (f) {
case '+':
printf("%lf\n", op1 + op2);
break;
case '-':
printf("%lf\n", op1 - op2);
break;
case '*':
printf("%lf\n", op1 * op2);
break;
case '/':
printf("%lf\n", op1 / op2);
break;
default:
printf("Incorrect operator");
break;
}
}

View file

@ -0,0 +1,25 @@
// ac_3_10.cpp
// Горбацевич Андрей
#include <iostream>
#include <cmath>
using namespace std;
bool in_D(double x, double y)
{
double R = x*x + y*y;
bool up = (y >= 0);
bool l = (x <= 0);
bool r = (x >= 0);
bool fp = l and (R <= 1);
bool sp = r and (R >= 0.36) and (R <= 1);
return up and (fp or sp);
}
int main()
{
double x, y;
cout << "x, y >>>";
cin >> x >> y;
double P = (in_D(x, y)? x+y : 0 );
cout << "P(x,y)= " << P << endl;
}

View file

@ -0,0 +1,21 @@
// ac_3_11.cpp
// Горбацевич Андрей
#include <iostream>
#include <cmath>
using namespace std;
bool in_D(double x, double y)
{
bool moreTwo = (y >= 2);
bool inAbs = (y >= 2*abs(x));
return moreTwo or inAbs;
}
int main()
{
double x, y, a;
cout << "x, y, a >>>";
cin >> x >> y >> a;
double S = ((in_D(x, y) and (a < 2))? (pow(a, -x)) : log(1 + x*x));
cout << "S(x,y,a)= " << S << endl;
}

View file

@ -0,0 +1,20 @@
// ac_3_9.cpp
// Горбацевич Андрей
#include <iostream>
#include <clocale>
#include <cmath>
using namespace std;
bool in_D(double x, double y)
{
return ((y >= abs(x)) && (x*x+y*y <= 1));
}
int main()
{
double x, y;
cout << "x, y >>>";
cin >> x >> y;
double z = (in_D(x, y)? sin(x) : x + 2 * y );
cout << "z(x,y)= " << z << endl;
}

View file

@ -0,0 +1,25 @@
// ac_4_12.cpp
// Горбацевич Андрей
#include <iostream>
#include <cmath>
using namespace std;
double ak(double x, int k) {
return sqrt(x*x + pow(sin(k*M_PI_4), 2));
}
double sum(double x, int N) {
double out = 0;
for (int k = 1; k <= N; k++) {
out += ak(x, k);
}
return out;
}
int main() {
double x;
int N;
cout << "x, N >>>";
cin >> x >> N;
cout << "S= " << sum(x, N) << endl;
}

View file

@ -0,0 +1,30 @@
// ac_4_13.cpp
// Горбацевич Андрей
#include <iostream>
#include <cmath>
using namespace std;
double xmi(double x, int N) {
double out = 0;
for (int i = 1; i <= N; i++) {
out += 1/pow(x, i);
}
return out;
}
double xi(double x, int N) {
double out = 1;
for (int i = 0; i <= N; i++) {
out *= (x - i);
}
return out;
}
int main() {
double x;
int N;
cout << "x, N >>>";
cin >> x >> N;
cout << "S= " << xmi(x, N) << endl;
cout << "P= " << xi(x, N) << endl;
}

View file

@ -0,0 +1,25 @@
// ac_4_14.cpp
// Горбацевич Андрей
#include <iostream>
#include <cmath>
using namespace std;
double inline ai(int i) {
return ((i%3 == 0)? (i / 3.) : (i / (i - 3.)));
}
double sai(int i) {
double out = 0;
for (int k = 1; k <= i; k++) {
auto fr = ai(k);
out += pow(fr, 2);
}
return out;
}
int main() {
int N;
cout << "N >>>";
cin >> N;
cout << "S= " << sai(N) << endl;
}

View file

@ -0,0 +1,22 @@
// ac_5_15.cpp
// Горбацевич Андрей
#include <iostream>
using namespace std;
double df(int N) {
double out = 1.;
double os = 0.;
for (int i = 1; i <= N; i++) {
os += i*i;
out *= os;
}
return out;
}
int main() {
int N;
cout << "N >>>";
cin >> N;
cout.setf(ios_base::fixed);
cout << "PS= " << df(N) << endl;
}

View file

@ -0,0 +1,32 @@
// ac_5_16.cpp
// Горбацевич Андрей
#include <iostream>
#include <cmath>
using namespace std;
int fk(int i) {
if ((i == 0) || (i == 1))
return 1;
int out = 1;
for (int j = 2; j <= i; j++) {
out *= j;
}
return out;
}
double df(int N, int M) {
double out = 0;
int fak = fk(M);
for (int k = M; k <= N; k++) {
out += k*k*log(fak);
fak *= k+1;
}
return out;
}
int main() {
int N, M;
cout << "N, M >>>";
cin >> N >> M;
cout << "S= " << df(N, M) << endl;
}

View file

@ -0,0 +1,29 @@
// ac_5_17.cpp
// Горбацевич Андрей
#include <iostream>
#include <cmath>
#include <set>
using namespace std;
double df(int N) {
double out = 0.;
double i_ = 1;
for (int i = 1; i <= N; i++) {
i_ *= i;
double j_ = 1.;
double out_ = 1.;
for (int j = 1; j <= i; j++) {
j_ *= j;
out_ *= j_/i_;
}
out += out_;
}
return out;
}
int main() {
int N;
cout << "N >>>";
cin >> N;
cout << "SP= " << df(N) << endl;
}

View file

@ -0,0 +1,24 @@
// ac_6_18.cpp
// Горбацевич Андрей
#include <iostream>
#include <cmath>
using namespace std;
double inline funk(int i) {
return 1./i;
}
int main() {
double eps;
cout << "eps=";
cin >> eps;
int i = 1;
double cur = funk(i);
double prev = 1e6*1.;
while (abs(cur-prev)>=eps) {
prev = cur;
cur = funk(++i);
}
cout << "a(i=" << i << ")=" << cur;
return 0;
}

View file

@ -0,0 +1,23 @@
// ac_6_19.cpp
// Горбацевич Андрей
#include <iostream>
using namespace std;
double inline funk(int i) {
return 1./i;
}
int main() {
double A;
cout << "A=";
cin >> A;
int i = 1;
double cur = funk(i);
double prev = 1e6*1.;
while (!((prev > A) && (A >= cur))) {
prev = cur;
cur = funk(++i);
}
cout << "a(i=" << i << ")=" << cur;
return 0;
}

View file

@ -0,0 +1,28 @@
// ac_6_20.cpp
// Горбацевич Андрей
#include <iostream>
#include <cmath>
using namespace std;
double inline funk(int i) {
double out = 1;
for (int k = 1; k <= i; k++) {
out *= (1 - 1 / sqrt(k + i));
}
return out;
}
int main() {
double eps;
cout << "eps=";
cin >> eps;
int i = 0;
double aip = funk(++i);
double ai = funk(++i);
while (abs(ai-aip)>=eps) {
aip = ai;
ai = funk(++i);
}
cout << "a(i=" << i << ")=" << ai;
return 0;
}

View file

@ -0,0 +1,21 @@
// ac_7_21.cpp
// Горбацевич Андрей
#include <iostream>
using namespace std;
int main() {
int N;
cout << "N >>>";
cin >> N;
int *A = new int[N];
for (int i = 0; i < N; i++) {
cout << "A[" << i << "] >>>";
cin >> *(A + i);
}
cout << "A = ";
for (int i = 0; i < N; i++) {
cout << *(A + i) << " ";
}
return 0;
}

View file

@ -0,0 +1,30 @@
// ac_7_22.cpp
// Горбацевич Андрей
#include <iostream>
#include <ctime>
using namespace std;
int main() {
srand(time(nullptr) / 2);
int N;
cout << "N >>>";
cin >> N;
int *rez = new int[N];
int *data = new int[100]();
for (int i = 0; i < N; i++) {
*(rez + i) = 1 + rand() % 100 + 1;
}
for (int i = 0; i < N; i++) {
*(data + (*(rez + i) - 1)) += 1;
}
cout << "rez = ";
for (int i = 0; i < N; i++) {
cout << *(rez + i) << " ";
}
cout << endl << endl;
for (int i = 0; i < 100; i++) {
cout << "Count of `" << i + 1 << "`: " << *(data + i) << endl;
}
return 0;
}

View file

@ -0,0 +1,54 @@
// ac_7_23.cpp
// Горбацевич Андрей
#include <iostream>
using namespace std;
int *get_array(size_t size);
void print_array(const int *arr, size_t size);
size_t remove_zeros(int *arr, size_t n);
int main()
{
size_t len;
cout << "Input length >>>";
cin >> len;
cout << "Input array >>>";
int *arr = get_array(len);
size_t zc = remove_zeros(arr, len);
cout << "Zeros count: " << zc << endl;
cout << "Resulting array: ";
print_array(arr, len-zc);
}
int *get_array(size_t size) {
int *array = new int[size]();
for (size_t i = 0; i < size; i++) {
cin >> *(array + i);
}
return array;
}
void print_array(const int *arr, size_t size) {
for (size_t i = 0; i < size; i++) {
cout << *(arr + i) << " ";
}
cout << endl;
}
size_t remove_zeros(int *arr, size_t n) {
size_t out = 0;
bool flag;
do {
flag = false;
for (size_t i = 0; i < n-1; i++) {
if (*(arr + i) == 0 && *(arr + i + 1) != 0) {
swap(*(arr + i), *(arr + i + 1));
out++;
flag = true;
break;
}
}
} while (flag);
return out;
}

View file

@ -0,0 +1,30 @@
// ac_8_24.cpp
// Горбацевич Андрей
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> two_mins(const vector<int> &vec) {
vector<int> v = vec;
sort(begin(v), end(v));
v.resize(2);
return v;
}
int main() {
int N;
cout << "N >>>";
cin >> N;
vector<int> v;
v.reserve(N);
cout << "v[...] >>>";
for (int i = 0; i < N; i++) {
int j;
cin >> j;
v.push_back(j);
}
vector<int> nv = two_mins(v);
cout << "Two minimals is " << nv[0] << " and " << nv[1] << endl;
return 0;
}

View file

@ -0,0 +1,31 @@
// ac_8_25.cpp
// Горбацевич Андрей
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> max_diff(const vector<int> &vec) {
vector<int> v = vec;
sort(begin(v), end(v));\
swap(*(v.end() - 1), v[1]);
v.resize(2);
return v;
}
int main() {
int N;
cout << "N >>>";
cin >> N;
vector<int> v;
v.reserve(N);
cout << "v[...] >>>";
for (int i = 0; i < N; i++) {
int j;
cin >> j;
v.push_back(j);
}
vector<int> nv = max_diff(v);
cout << "Biggest diff is between " << nv[0] << " and " << nv[1] << " (eq. " << nv[0] - nv[1] << ")" << endl;
return 0;
}

View file

@ -0,0 +1,40 @@
// ac_8_26.cpp
// Горбацевич Андрей
#include <iostream>
#include <vector>
using namespace std;
vector<int> count_less(const vector<int> &vec, int h) {
vector<int> v;
v.reserve(vec.size());
for (size_t c = 0; c < vec.size(); c++) {
if (vec[c] < h) {
v.push_back(c);
}
}
return v;
}
int main() {
int N;
cout << "N >>>";
cin >> N;
vector<int> heights;
heights.reserve(N);
cout << "heights[...] >>>";
for (int i = 0; i < N; i++) {
int j;
cin >> j;
heights.push_back(j);
}
int h;
cout << "h >>>";
cin >> h;
vector<int> nv = count_less(heights, h);
cout << "Lesser indexes: ";
for (int ch : nv) {
cout << ch << " ";
}
cout << endl;
return 0;
}

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