initial
This commit is contained in:
commit
2802401eee
79 changed files with 2650 additions and 0 deletions
15
1sem/adaptation courses/1/1.cpp
Normal file
15
1sem/adaptation courses/1/1.cpp
Normal 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 << "!";
|
||||
}
|
||||
15
1sem/adaptation courses/1/2.cpp
Normal file
15
1sem/adaptation courses/1/2.cpp
Normal 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;
|
||||
}
|
||||
15
1sem/adaptation courses/1/3.cpp
Normal file
15
1sem/adaptation courses/1/3.cpp
Normal 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;
|
||||
}
|
||||
19
1sem/adaptation courses/1/4.cpp
Normal file
19
1sem/adaptation courses/1/4.cpp
Normal 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);
|
||||
}
|
||||
36
1sem/adaptation courses/10/30.cpp
Normal file
36
1sem/adaptation courses/10/30.cpp
Normal 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;
|
||||
}
|
||||
36
1sem/adaptation courses/10/31.cpp
Normal file
36
1sem/adaptation courses/10/31.cpp
Normal 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;
|
||||
}
|
||||
35
1sem/adaptation courses/10/32.cpp
Normal file
35
1sem/adaptation courses/10/32.cpp
Normal 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;
|
||||
}
|
||||
34
1sem/adaptation courses/11/33.cpp
Normal file
34
1sem/adaptation courses/11/33.cpp
Normal 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;
|
||||
}
|
||||
52
1sem/adaptation courses/11/34.cpp
Normal file
52
1sem/adaptation courses/11/34.cpp
Normal 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;
|
||||
}
|
||||
37
1sem/adaptation courses/11/35.cpp
Normal file
37
1sem/adaptation courses/11/35.cpp
Normal 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;
|
||||
}
|
||||
33
1sem/adaptation courses/2/5.cpp
Normal file
33
1sem/adaptation courses/2/5.cpp
Normal 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("Доброй ночи!");
|
||||
}
|
||||
}
|
||||
25
1sem/adaptation courses/2/6.cpp
Normal file
25
1sem/adaptation courses/2/6.cpp
Normal 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);
|
||||
}
|
||||
}
|
||||
34
1sem/adaptation courses/2/7.cpp
Normal file
34
1sem/adaptation courses/2/7.cpp
Normal 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;
|
||||
}
|
||||
}
|
||||
25
1sem/adaptation courses/3/10.cpp
Normal file
25
1sem/adaptation courses/3/10.cpp
Normal 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;
|
||||
}
|
||||
21
1sem/adaptation courses/3/11.cpp
Normal file
21
1sem/adaptation courses/3/11.cpp
Normal 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;
|
||||
}
|
||||
20
1sem/adaptation courses/3/9.cpp
Normal file
20
1sem/adaptation courses/3/9.cpp
Normal 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;
|
||||
}
|
||||
25
1sem/adaptation courses/4/12.cpp
Normal file
25
1sem/adaptation courses/4/12.cpp
Normal 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;
|
||||
}
|
||||
30
1sem/adaptation courses/4/13.cpp
Normal file
30
1sem/adaptation courses/4/13.cpp
Normal 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;
|
||||
}
|
||||
25
1sem/adaptation courses/4/14.cpp
Normal file
25
1sem/adaptation courses/4/14.cpp
Normal 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;
|
||||
}
|
||||
22
1sem/adaptation courses/5/15.cpp
Normal file
22
1sem/adaptation courses/5/15.cpp
Normal 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;
|
||||
}
|
||||
32
1sem/adaptation courses/5/16.cpp
Normal file
32
1sem/adaptation courses/5/16.cpp
Normal 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;
|
||||
}
|
||||
29
1sem/adaptation courses/5/17.cpp
Normal file
29
1sem/adaptation courses/5/17.cpp
Normal 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;
|
||||
}
|
||||
24
1sem/adaptation courses/6/18.cpp
Normal file
24
1sem/adaptation courses/6/18.cpp
Normal 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;
|
||||
}
|
||||
23
1sem/adaptation courses/6/19.cpp
Normal file
23
1sem/adaptation courses/6/19.cpp
Normal 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;
|
||||
}
|
||||
28
1sem/adaptation courses/6/20.cpp
Normal file
28
1sem/adaptation courses/6/20.cpp
Normal 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;
|
||||
}
|
||||
21
1sem/adaptation courses/7/21.cpp
Normal file
21
1sem/adaptation courses/7/21.cpp
Normal 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;
|
||||
}
|
||||
30
1sem/adaptation courses/7/22.cpp
Normal file
30
1sem/adaptation courses/7/22.cpp
Normal 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;
|
||||
}
|
||||
54
1sem/adaptation courses/7/23.cpp
Normal file
54
1sem/adaptation courses/7/23.cpp
Normal 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;
|
||||
}
|
||||
30
1sem/adaptation courses/8/24.cpp
Normal file
30
1sem/adaptation courses/8/24.cpp
Normal 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;
|
||||
}
|
||||
31
1sem/adaptation courses/8/25.cpp
Normal file
31
1sem/adaptation courses/8/25.cpp
Normal 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;
|
||||
}
|
||||
40
1sem/adaptation courses/8/26.cpp
Normal file
40
1sem/adaptation courses/8/26.cpp
Normal 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;
|
||||
}
|
||||
43
1sem/adaptation courses/9/27.cpp
Normal file
43
1sem/adaptation courses/9/27.cpp
Normal 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;
|
||||
}
|
||||
43
1sem/adaptation courses/9/28.cpp
Normal file
43
1sem/adaptation courses/9/28.cpp
Normal 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;
|
||||
}
|
||||
48
1sem/adaptation courses/9/29.cpp
Normal file
48
1sem/adaptation courses/9/29.cpp
Normal 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;
|
||||
}
|
||||
13
1sem/programming basics/1/0.cpp
Normal file
13
1sem/programming basics/1/0.cpp
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
// pb_1_0.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <clocale>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
setlocale(LC_ALL, "russian");
|
||||
cout << "Hello World!\n";
|
||||
printf("Привет Мир!");
|
||||
}
|
||||
126
1sem/programming basics/10/100.cpp
Normal file
126
1sem/programming basics/10/100.cpp
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
// pb_10_25.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
const double EPS = 0.001;
|
||||
const int RIGHT_UP = 1,
|
||||
RIGHT_DW = 2,
|
||||
LEFT_DW = 3,
|
||||
LEFT_UP = 4;
|
||||
|
||||
double inline v_len(double x, double y) {
|
||||
return sqrt(pow(x, 2) + pow(y, 2));
|
||||
}
|
||||
|
||||
int inline get_quad(double x, double y) {
|
||||
if (x >= 0 && y >= 0) {
|
||||
return RIGHT_UP;
|
||||
}
|
||||
if (x >= 0 && y <= 0) {
|
||||
return RIGHT_DW;
|
||||
}
|
||||
if (x <= 0 && y <= 0) {
|
||||
return LEFT_DW;
|
||||
}
|
||||
if (x <= 0 && y >= 0) {
|
||||
return LEFT_UP;
|
||||
}
|
||||
}
|
||||
|
||||
double *read_array(int len) {
|
||||
auto *p = new double[len]();
|
||||
cout << "Input " << len << " numbers >>>";
|
||||
for (int i = 0; i < len; i++) {
|
||||
cin >> *(p + i);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
void print_array(const double *p, int len) {
|
||||
cout << "array[] = ";
|
||||
for (int i = 0; i < len; i++) {
|
||||
cout << *(p + i) << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void sort(double *p, int len, bool asc) {
|
||||
bool mod;
|
||||
do {
|
||||
mod = false;
|
||||
for (int i = 0; i < len-1; i++) {
|
||||
if (*(p + i) > *(p + i + 1) && asc) {
|
||||
swap(*(p + i), *(p + i + 1));
|
||||
mod = true;
|
||||
}
|
||||
else if (*(p + i) < *(p + i + 1) && !asc) {
|
||||
swap(*(p + i), *(p + i + 1));
|
||||
mod = true;
|
||||
}
|
||||
}
|
||||
} while (mod);
|
||||
}
|
||||
|
||||
double *my_task(const double *A, const double *B, int len) {
|
||||
auto *C = new double[len]();
|
||||
for (int i = 0; i < len; i++) {
|
||||
*(C + i) = v_len(*(A + i), *(B + i));
|
||||
}
|
||||
return C;
|
||||
}
|
||||
|
||||
void find_nearest_triplet(const double *A, const double *B, int len) {
|
||||
double dist = 0;
|
||||
vector<int> triplet;
|
||||
for (int i = 0; i < len; i++) {
|
||||
dist = v_len(*(A + i), *(B + i));
|
||||
triplet.push_back(i);
|
||||
for (int j = 0; j < len; j++) {
|
||||
if (triplet.size() > 2) break;
|
||||
if (i == j) continue;
|
||||
if (abs(v_len(*(A + j), *(B + j)) - dist) < EPS) {
|
||||
if (get_quad(*(A + j), *(B + j)) != get_quad(*(A + i), *(B + i))) {
|
||||
triplet.push_back(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (triplet.size() == 3) {
|
||||
cout << "Three equally-distant dots:" << endl;
|
||||
for (int pos : triplet) {
|
||||
cout << "\t" << pos << "(" << *(A + pos) << ", " << *(B + pos) << ")" << endl;
|
||||
}
|
||||
cout << "With distance = " << dist << endl;
|
||||
return;
|
||||
}
|
||||
triplet.clear();
|
||||
}
|
||||
cout << "No three equally-distant dots found." << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int len;
|
||||
bool asc;
|
||||
cout << "len >>>";
|
||||
cin >> len;
|
||||
cout << "asc[0, 1] >>>";
|
||||
cin >> asc;
|
||||
double *A = read_array(len);
|
||||
double *B = read_array(len);
|
||||
double *C = my_task(A, B, len);
|
||||
cout << "Before sort():" << endl;
|
||||
print_array(A, len);
|
||||
print_array(B, len);
|
||||
print_array(C, len);
|
||||
sort(A, len, asc);
|
||||
sort(B, len, asc);
|
||||
sort(C, len, asc);
|
||||
cout << "After sort():" << endl;
|
||||
print_array(A, len);
|
||||
print_array(B, len);
|
||||
print_array(C, len);
|
||||
|
||||
find_nearest_triplet(A, B, len);
|
||||
}
|
||||
18
1sem/programming basics/2/0.cpp
Normal file
18
1sem/programming basics/2/0.cpp
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// pb_2_0.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <stdio.h>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
long a, b;
|
||||
printf("Input `a` and `b` >>>");
|
||||
scanf("%ld %ld", &a, &b);
|
||||
long sum = a + b;
|
||||
long diff = a - b;
|
||||
long multiplication = a * b;
|
||||
printf("Sum(%%ld): %ld \nMultiplication(%%10ld): %10ld \nDifference(%%-10ld): %-10ld\n",
|
||||
sum, multiplication, diff);
|
||||
double mean = (float)(a + b) / 2.f;
|
||||
printf("Mean:\n%%f: %f\n%%15.4f: %15.4f\n%%-15.4f: %-15.4f\n%%0.15f: %0.15f\n%%30.15f: %30.15f",
|
||||
mean, mean, mean, mean, mean);
|
||||
}
|
||||
16
1sem/programming basics/2/1.cpp
Normal file
16
1sem/programming basics/2/1.cpp
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// pb_2_1.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <stdio.h>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
double x1, y1, x2, y2, x3, y3;
|
||||
printf("x1 y1 x2 y2 x3 y3 >>>");
|
||||
scanf("%lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3);
|
||||
double S = ((x1 - x3)*(y2 - y3) - (x2 - x3)*(y2 - y3)) / 2.f;
|
||||
// 1 | x1-x3 y1-y3 |
|
||||
// S = --- | |
|
||||
// 2 | x2-y3 y2-y3 |
|
||||
printf("S=%f", abs(S));
|
||||
}
|
||||
14
1sem/programming basics/2/2.cpp
Normal file
14
1sem/programming basics/2/2.cpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// pb_2_2.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <stdio.h>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int deg;
|
||||
printf("deg >>>");
|
||||
scanf("%d", °);
|
||||
int elapsedHours = deg / 30;
|
||||
int elapsedMinutes = elapsedHours*60 + deg / 6;
|
||||
printf("Elapsed hours: %d \nElapsed minutes: %d", elapsedHours, elapsedMinutes);
|
||||
}
|
||||
37
1sem/programming basics/3/0.cpp
Normal file
37
1sem/programming basics/3/0.cpp
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
// pb_3_0.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <stdio.h>
|
||||
#include <cmath>
|
||||
#include <clocale>
|
||||
#include <afxres.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
SetConsoleCP(CP_UTF8);
|
||||
SetConsoleOutputCP(CP_UTF8);
|
||||
setlocale(LC_ALL, "ru_RU.UTF-8");
|
||||
|
||||
double phi = 1.618033988749894848204586834365638117720309179805762862135;
|
||||
|
||||
cout.width(20);
|
||||
cout << phi << endl;
|
||||
|
||||
cout.precision(20);
|
||||
cout << phi << endl;
|
||||
|
||||
cout.precision(3);
|
||||
cout.width(20);
|
||||
cout.fill('_');
|
||||
cout << phi << endl;
|
||||
|
||||
cout.fill('_');
|
||||
cout.width(30);
|
||||
cout.setf(ios_base::left);
|
||||
cout << phi << endl;
|
||||
cout.fill('_');
|
||||
cout.width(30);
|
||||
cout.setf(ios_base::right);
|
||||
cout << phi << endl;
|
||||
}
|
||||
20
1sem/programming basics/3/1.cpp
Normal file
20
1sem/programming basics/3/1.cpp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// pb_3_1.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 x1, y1, x2, y2, x3, y3;
|
||||
printf("x1 y1 x2 y2 x3 y3 >>>");
|
||||
scanf("%lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3);
|
||||
bool ool = ((x2-x1)*(y3-y1) == (y2-y1)*(x3-x1));
|
||||
printf(ool? "YES" : "NO");
|
||||
}
|
||||
27
1sem/programming basics/4/1_60.cpp
Normal file
27
1sem/programming basics/4/1_60.cpp
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
// pb_4_1_60.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
#include <clocale>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
|
||||
double f1(double x)
|
||||
{
|
||||
x = x + 3.14;
|
||||
return x;
|
||||
}
|
||||
double f2(double x)
|
||||
{
|
||||
x = sqrt(x*x + 2);
|
||||
return x;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
double x;
|
||||
cout << "X=?\n";
|
||||
cin >> x;
|
||||
cout << "y=" << f1(x) + f1(x*x) + f2(x) + f2(x-1) << endl;
|
||||
}
|
||||
|
||||
// 1 11.4263
|
||||
// 2 16.4615
|
||||
31
1sem/programming basics/4/2_40.cpp
Normal file
31
1sem/programming basics/4/2_40.cpp
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
// pb_4_2_40.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
#include <clocale>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
|
||||
double f1(double x)
|
||||
{
|
||||
x = log(x*x) - 8.3e3;
|
||||
return x;
|
||||
}
|
||||
double f2(double x)
|
||||
{
|
||||
x = 1 / (cbrt(3*x) + 1);
|
||||
return x;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
double x;
|
||||
cout << "X=?\n";
|
||||
cin >> x;
|
||||
if (x == 0) {
|
||||
cout << "err: x == 0" << endl;
|
||||
return 0;
|
||||
}
|
||||
cout << "y=" << f1(x) + f2(x) << endl;
|
||||
}
|
||||
|
||||
// 1 -8299.59
|
||||
// 2 -8298.26
|
||||
20
1sem/programming basics/5/0_60.cpp
Normal file
20
1sem/programming basics/5/0_60.cpp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// pb_5_0_60.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int N;
|
||||
cout << "N >>";
|
||||
cin >> N;
|
||||
cout << "With for(...)>>>" << endl;
|
||||
for (int i = 1; i <= N; i++) cout << i << endl;
|
||||
cout << endl;
|
||||
cout << "With while(...)>>>" << endl;
|
||||
int wi = 1;
|
||||
while (wi <= N) cout << wi++ << endl;
|
||||
cout << endl;
|
||||
cout << "With do{...}while(...)>>>" << endl;
|
||||
int dwi = 1;
|
||||
do { cout << dwi++ << endl; } while (dwi <= N);
|
||||
}
|
||||
20
1sem/programming basics/5/1_40.cpp
Normal file
20
1sem/programming basics/5/1_40.cpp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// pb_5_1_40.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int gcd (int a, int b) {
|
||||
while (b) {
|
||||
a %= b;
|
||||
swap(a, b);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
int a, b, c;
|
||||
cout << "a, b, c >>";
|
||||
cin >> a >> b >> c;
|
||||
cout << (gcd(gcd(a,b),c) == 1? "YES" : "NO") << endl;
|
||||
}
|
||||
44
1sem/programming basics/6/2_100.cpp
Normal file
44
1sem/programming basics/6/2_100.cpp
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
// pb_6_2_100.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
|
||||
double arctg(double, double);
|
||||
double mant(double);
|
||||
|
||||
int main() {
|
||||
double x, d;
|
||||
cout << "x, d >>>";
|
||||
cin >> x >> d;
|
||||
if ((x < -1) or (x > 1)) {
|
||||
cout << "err: x not in range";
|
||||
return 0;
|
||||
}
|
||||
|
||||
double oarctg = arctg(x, d);
|
||||
double carctg = atan(x);
|
||||
cout << " own arctg(x) = " << oarctg << endl;
|
||||
cout << "cmath arctg(x) = " << carctg << endl;
|
||||
cout << " diff = " << oarctg - carctg << endl;
|
||||
}
|
||||
|
||||
double arctg(double x, double delta) {
|
||||
double out = x;
|
||||
double px = x;
|
||||
int den = 1;
|
||||
bool neg = false;
|
||||
double pout = out + 0.1f;
|
||||
while (abs(mant(out)-mant(pout)) > delta) {
|
||||
pout = out;
|
||||
neg = !neg;
|
||||
x *= px*px;
|
||||
den += 2;
|
||||
out += x/den * (neg? -1 : 1);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
double mant(double num) {
|
||||
return num - (int)num;
|
||||
}
|
||||
43
1sem/programming basics/7/2_100.cpp
Normal file
43
1sem/programming basics/7/2_100.cpp
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
// pb_7_2_100.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
|
||||
double f(double);
|
||||
void pp (double, double);
|
||||
|
||||
int main() {
|
||||
double a, b, h;
|
||||
bool mono_decreasing = true;
|
||||
cout << "a, b, h >>>";
|
||||
cin >> a >> b >> h;
|
||||
double last_y = f(a);
|
||||
for (; a < b; a += h) {
|
||||
double y = f(a);
|
||||
pp(a, y);
|
||||
if (last_y < y) {
|
||||
mono_decreasing = false;
|
||||
}
|
||||
else {
|
||||
last_y = y;
|
||||
}
|
||||
}
|
||||
cout << "Monotonously decreasing? " << (mono_decreasing? "YES" : "NO") << endl;
|
||||
}
|
||||
|
||||
double f(double x) {
|
||||
return x * cos(3 * x ) - 2;
|
||||
}
|
||||
|
||||
void pp (double x, double y) {
|
||||
cout.setf(ios_base::right);
|
||||
cout.fill(' ');
|
||||
cout.width(10);
|
||||
cout << x;
|
||||
cout.width(15);
|
||||
cout << y << endl;
|
||||
}
|
||||
|
||||
// -99 -98.5 0.02 for yes
|
||||
// 0 0.5 0.02 for no
|
||||
60
1sem/programming basics/8/2_100.cpp
Normal file
60
1sem/programming basics/8/2_100.cpp
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
// pb_8_2_100.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int *read_array(int len)
|
||||
{
|
||||
int *out = new int[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
cout << "arr[" << i << "] >>>";
|
||||
cin >> out[i];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void print_array(int arr[], int len) {
|
||||
for (int i = 0; i < len; i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int my_task(const int A[], int lenA, int B[], int lenB) {
|
||||
int actbs = 0;
|
||||
int indx = -1;
|
||||
int *p = new int[lenB];
|
||||
for (int i = lenA; i >= 0;) {
|
||||
i -= 1;
|
||||
if (A[i] >= 0) {
|
||||
indx = i;
|
||||
break;
|
||||
}
|
||||
p[actbs++] = A[i];
|
||||
}
|
||||
delete[] B;
|
||||
B = new int[actbs];
|
||||
for (int i = 0; i < actbs; i++) {
|
||||
B[i] = p[actbs-i-1];
|
||||
}
|
||||
return indx;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int len;
|
||||
cout << "len >>>";
|
||||
cin >> len;
|
||||
int *a1 = read_array(len);
|
||||
int *b = new int[len];
|
||||
int indx = my_task(a1, len, b, len);
|
||||
if (indx > -1) {
|
||||
cout << "Last positive: " << indx;
|
||||
}
|
||||
else {
|
||||
cout << "No positive elements";
|
||||
}
|
||||
cout << endl << "B = ";
|
||||
print_array(b, len-1-indx);
|
||||
}
|
||||
60
1sem/programming basics/9/100.cpp
Normal file
60
1sem/programming basics/9/100.cpp
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
// pb_9_100.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int *read_array(int len)
|
||||
{
|
||||
int *out = new int[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
cout << "arr[" << i << "] >>>";
|
||||
cin >> *(out + i);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void print_array(const int *arr, int len) {
|
||||
for (int i = 0; i < len; i++) {
|
||||
cout << *(arr + i) << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int my_task(const int *A, int lenA, int *B, int lenB) {
|
||||
int actbs = 0;
|
||||
int indx = -1;
|
||||
int *p = new int[lenB];
|
||||
for (int i = lenA; i >= 0;) {
|
||||
i -= 1;
|
||||
if (*(A + i) >= 0) {
|
||||
indx = i;
|
||||
break;
|
||||
}
|
||||
*(p + actbs++) = *(A + i);
|
||||
}
|
||||
delete[] B;
|
||||
B = new int[actbs];
|
||||
for (int i = 0; i < actbs; i++) {
|
||||
*(B + i) = *(p + (actbs-i-1));
|
||||
}
|
||||
return indx;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int len;
|
||||
cout << "len >>>";
|
||||
cin >> len;
|
||||
int *a1 = read_array(len);
|
||||
int *b = new int[len];
|
||||
int indx = my_task(a1, len, b, len);
|
||||
if (indx > -1) {
|
||||
cout << "Last positive: " << indx;
|
||||
}
|
||||
else {
|
||||
cout << "No positive elements";
|
||||
}
|
||||
cout << endl << "B = ";
|
||||
print_array(b, len-1-indx);
|
||||
}
|
||||
13
1sem/programming basics/z1/1.cpp
Normal file
13
1sem/programming basics/z1/1.cpp
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
// pb_z1_1.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <stdio.h>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int a, b;
|
||||
printf("a b >>>");
|
||||
scanf("%d %d", &a, &b);
|
||||
swap(a, b);
|
||||
printf("a=%d \nb=%d", a, b);
|
||||
}
|
||||
15
1sem/programming basics/z1/2.cpp
Normal file
15
1sem/programming basics/z1/2.cpp
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
// pb_z1_2.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <stdio.h>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int num_people, num_seats, num_buses, num_free_seats;
|
||||
printf("People count seats count >>>");
|
||||
scanf("%d %d", &num_people, &num_seats);
|
||||
num_buses = num_people / num_seats;
|
||||
bool additional_seats = num_people % num_seats > 0;
|
||||
num_buses += additional_seats;
|
||||
num_free_seats = num_buses * num_seats - num_people;
|
||||
printf("Busses count: %d\nFree seats: %d\n", num_buses, num_free_seats);
|
||||
}
|
||||
14
1sem/programming basics/z1/3.cpp
Normal file
14
1sem/programming basics/z1/3.cpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// pb_z1_3.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <stdio.h>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
double d;
|
||||
printf("d >>>");
|
||||
scanf("%lf", &d);
|
||||
d = ((int)(abs(d) * 10)) % 10;
|
||||
printf("%d", (int)d);
|
||||
}
|
||||
61
1sem/programming basics/z10/28.cpp
Normal file
61
1sem/programming basics/z10/28.cpp
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
// pb_z10_28.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
|
||||
#define MAX_STR 5000
|
||||
using namespace std;
|
||||
|
||||
void rstrip(char*, const char*);
|
||||
char* get_string();
|
||||
void println_string(const char*);
|
||||
|
||||
int main() {
|
||||
char *s, *chars;
|
||||
cout << "Input string:" << endl;
|
||||
s = get_string();
|
||||
cout << "Input chars:" << endl;
|
||||
chars = get_string();
|
||||
cout << "String before:" << endl;
|
||||
println_string(s);
|
||||
rstrip(s, chars);
|
||||
cout << "String after:" << endl;
|
||||
println_string(s);
|
||||
}
|
||||
|
||||
void rstrip(char *s, const char *chars) {
|
||||
int rpad = 0;
|
||||
int i = (int)strlen(s)-1;
|
||||
bool found;
|
||||
do {
|
||||
found = false;
|
||||
char cc = *(s + i--);
|
||||
for (size_t j = 0; j < strlen(chars); j++) {
|
||||
if (cc == *(chars + j)) {
|
||||
rpad += 1;
|
||||
found = true;
|
||||
*(s + i+1) = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while (found && i >= 0);
|
||||
}
|
||||
|
||||
char* get_string() {
|
||||
int c = 0;
|
||||
char *s = new char[MAX_STR];
|
||||
char ch;
|
||||
while ((ch = cin.get()) != '\n') {
|
||||
*(s + c++) = ch;
|
||||
}
|
||||
*(s + c) = '\0';
|
||||
return s;
|
||||
}
|
||||
|
||||
void println_string(const char* s) {
|
||||
for (size_t i = 0; i < strlen(s); i++) {
|
||||
cout << *(s + i);
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
85
1sem/programming basics/z10/29.cpp
Normal file
85
1sem/programming basics/z10/29.cpp
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
// pb_z10_29.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
|
||||
#define MAX_STR 5000
|
||||
using namespace std;
|
||||
|
||||
void strip(char*, const char*);
|
||||
char* get_string();
|
||||
void println_string(const char*);
|
||||
|
||||
int main() {
|
||||
char *s, *chars;
|
||||
cout << "Input string:" << endl;
|
||||
s = get_string();
|
||||
cout << "Input chars:" << endl;
|
||||
chars = get_string();
|
||||
cout << "String before:" << endl;
|
||||
println_string(s);
|
||||
strip(s, chars);
|
||||
cout << "String after:" << endl;
|
||||
println_string(s);
|
||||
}
|
||||
|
||||
void strip(char *s, const char *chars) {
|
||||
int lpad = 0;
|
||||
int fi = 0;
|
||||
bool found;
|
||||
do {
|
||||
found = false;
|
||||
char cc = *(s + fi);
|
||||
for (size_t j = 0; j < strlen(chars); j++) {
|
||||
if (cc == *(chars + j)) {
|
||||
lpad += 1;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
fi++;
|
||||
} while(found && fi >= 0);
|
||||
if(lpad > 0)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < strlen(s) - lpad; i++)
|
||||
{
|
||||
*(s + i) = *(s + i + lpad);
|
||||
}
|
||||
*(s + i) = '\0';
|
||||
}
|
||||
|
||||
int rpad = 0;
|
||||
int ei = (int)strlen(s)-1;
|
||||
do {
|
||||
found = false;
|
||||
char cc = *(s + ei--);
|
||||
for (size_t j = 0; j < strlen(chars); j++) {
|
||||
if (cc == *(chars + j)) {
|
||||
rpad += 1;
|
||||
found = true;
|
||||
*(s + ei + 1) = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while (found && ei >= 0);
|
||||
}
|
||||
|
||||
char* get_string() {
|
||||
int c = 0;
|
||||
char *s = new char[MAX_STR];
|
||||
char ch;
|
||||
while ((ch = cin.get()) != '\n') {
|
||||
*(s + c++) = ch;
|
||||
}
|
||||
*(s + c) = '\0';
|
||||
return s;
|
||||
}
|
||||
|
||||
void println_string(const char* s) {
|
||||
for (size_t i = 0; i < strlen(s); i++) {
|
||||
cout << *(s + i);
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
70
1sem/programming basics/z10/30.cpp
Normal file
70
1sem/programming basics/z10/30.cpp
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
// pb_z10_30.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
|
||||
#define MAX_STR 5000
|
||||
using namespace std;
|
||||
|
||||
size_t join(char*, size_t , const char*const*, size_t, const char*);
|
||||
char* get_string();
|
||||
char** get_string_list(size_t);
|
||||
void println_string(const char*);
|
||||
|
||||
int main() {
|
||||
size_t n_in;
|
||||
cout << "Count of strings >>>";
|
||||
cin >> n_in;
|
||||
cout << "Strings:" << endl;
|
||||
char **s_in = get_string_list(n_in+1);
|
||||
cout << "Joiner separator >>>";
|
||||
char *sep = get_string();
|
||||
cout << "Max length >>>";
|
||||
size_t len;
|
||||
cin >> len;
|
||||
char *s_out = new char[len];
|
||||
size_t f_len = join(s_out, len, s_in, n_in, sep);
|
||||
cout << "Joined string:" << endl;
|
||||
println_string(s_out);
|
||||
}
|
||||
|
||||
size_t join(char *s_out, size_t len, const char *const *s_in, size_t n_in, const char *sep) {
|
||||
string ps_out;
|
||||
ps_out += string(*(s_in + 1));
|
||||
for (size_t i = 2; i <= n_in; i++) {
|
||||
ps_out += string(sep) + string(*(s_in + i));
|
||||
}
|
||||
size_t mnl = min(len, ps_out.length());
|
||||
for (size_t i = 0; i < mnl; i++) {
|
||||
*(s_out + i) = ps_out.c_str()[i];
|
||||
}
|
||||
*(s_out + mnl) = '\0';
|
||||
return strlen(s_out);
|
||||
}
|
||||
|
||||
char** get_string_list(size_t count) {
|
||||
char **arr = new char*[count];
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
*(arr + i) = get_string();
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
char* get_string() {
|
||||
int c = 0;
|
||||
char *s = new char[MAX_STR];
|
||||
char ch;
|
||||
while ((ch = cin.get()) != '\n') {
|
||||
*(s + c++) = ch;
|
||||
}
|
||||
*(s + c) = '\0';
|
||||
return s;
|
||||
}
|
||||
|
||||
void println_string(const char* s) {
|
||||
for (size_t i = 0; i < strlen(s); i++) {
|
||||
cout << *(s + i);
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
24
1sem/programming basics/z2/4.cpp
Normal file
24
1sem/programming basics/z2/4.cpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// pb_z2_4.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 x, a, b, c, d;
|
||||
printf("x [a b] [c d] >>");
|
||||
scanf("%lf %lf %lf %lf %lf", &x, &a, &b, &c, &d);
|
||||
if ((fmin(a, b) <= x && x <= fmax(a, b)) || (fmin(c, d) <= x && x <= fmax(c, d))) {
|
||||
printf("Принадлежит");
|
||||
}
|
||||
else {
|
||||
printf("Не принадлежит");
|
||||
};
|
||||
}
|
||||
33
1sem/programming basics/z2/5.cpp
Normal file
33
1sem/programming basics/z2/5.cpp
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
// pb_z2_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");
|
||||
|
||||
double a1, a2, b1, b2;
|
||||
printf("[a1 a2] [b1, b2] >>");
|
||||
scanf("%lf %lf %lf %lf", &a1, &a2, &b1, &b2);
|
||||
if (fmin(a1, a2) == fmin(b1, b2) && fmax(a1, a2) == fmax(b1, b2)) {
|
||||
printf("А равно B");
|
||||
}
|
||||
else if (fmin(a1, a2) >= fmin(b1, b2) && fmax(a1, a2) <= fmax(b1, b2)) {
|
||||
printf("А внутри B");
|
||||
}
|
||||
else if (fmin(b1, b2) >= fmin(a1, a2) && fmax(b1, b2) <= fmax(a1, a2)) {
|
||||
printf("B внутри A");
|
||||
}
|
||||
else if ((fmin(b1, b2) <= fmin(a1, a2) && fmin(a1, a2) <= fmax(b1, b2)) || (fmin(b1, b2) <= fmax(a1, a2) && fmax(a1, a2) <= fmax(b1, b2))) {
|
||||
printf("Другое пересечение");
|
||||
}
|
||||
else {
|
||||
printf("Нет пересечения");
|
||||
}
|
||||
}
|
||||
32
1sem/programming basics/z2/6.cpp
Normal file
32
1sem/programming basics/z2/6.cpp
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// pb_z2_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");
|
||||
|
||||
double a, b, c, d;
|
||||
printf("a b c >>");
|
||||
scanf("%lf %lf %lf", &a, &b, &c);
|
||||
if (a == 0) {
|
||||
printf("Недопустимое значение `a`");
|
||||
return 0;
|
||||
}
|
||||
d = b*b-4*a*c;
|
||||
if (d < 0) {
|
||||
printf("Действительных корней нет");
|
||||
}
|
||||
else if (d == 0) {
|
||||
printf("x= %lf", -b/(2*a));
|
||||
}
|
||||
else {
|
||||
printf("x1= %lf\nx2= %lf", (-b+sqrt(d))/(2*a), (-b-sqrt(d))/(2*a));
|
||||
}
|
||||
}
|
||||
19
1sem/programming basics/z3/7.cpp
Normal file
19
1sem/programming basics/z3/7.cpp
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// pb_z3_7.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int find_most_frequent(int a, int b, int c, int d)
|
||||
{
|
||||
int s;
|
||||
s = a + b + c + d;
|
||||
return (s == 2 ? -1 : (s > 2 ? 1 : 0));
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int a, d, c, b;
|
||||
cout << ("a,b,c,d=?");
|
||||
cin >> a >> b >> c >> d;
|
||||
cout << find_most_frequent(a, b, c, d);
|
||||
}
|
||||
24
1sem/programming basics/z3/8.cpp
Normal file
24
1sem/programming basics/z3/8.cpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// pb_z3_8.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
|
||||
float inline sqr(float i)
|
||||
{
|
||||
return i * i;
|
||||
}
|
||||
|
||||
float dist(float x1, float y1, float x2, float y2)
|
||||
{
|
||||
float d = sqrt(sqr(x2 - x1) + sqr(y2 - y1));
|
||||
return d;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
float x1, y1, x2, y2;
|
||||
cout << "x1,y1,x2,y2\n";
|
||||
cin >> x1>> y1 >> x2 >> y2;
|
||||
cout << dist(x1, y1, x2, y2);
|
||||
}
|
||||
22
1sem/programming basics/z3/9.cpp
Normal file
22
1sem/programming basics/z3/9.cpp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// pb_z3_9.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
|
||||
float min_or_max(float a, float b, float c)
|
||||
{
|
||||
if (a > 0.5)
|
||||
return max(b, c);
|
||||
else
|
||||
return min(b, c);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
float a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
float d = min_or_max(a, b, c);
|
||||
cout << d << endl;
|
||||
}
|
||||
17
1sem/programming basics/z4/10.cpp
Normal file
17
1sem/programming basics/z4/10.cpp
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// pb_z4_10.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int a;
|
||||
cin >> a;
|
||||
cout << a << endl;
|
||||
do {
|
||||
int b;
|
||||
cin >> b;
|
||||
if (a > b) break;
|
||||
cout << b << endl;
|
||||
a = b;
|
||||
} while (true);
|
||||
}
|
||||
20
1sem/programming basics/z4/11.cpp
Normal file
20
1sem/programming basics/z4/11.cpp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// pb_z4_11.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void print_diag(int s) {
|
||||
for (int i = 0; i < s; i++) {
|
||||
for (int j = 0; j < i; j++) {
|
||||
cout << " ";
|
||||
}
|
||||
cout << "*" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int a;
|
||||
cout << "a >>";
|
||||
cin >> a;
|
||||
print_diag(a);
|
||||
}
|
||||
22
1sem/programming basics/z4/12.cpp
Normal file
22
1sem/programming basics/z4/12.cpp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// pb_z4_12.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void print_rect(int x, int y, int sx, int sy) {
|
||||
cout << "<<< printing rect >>>" << endl;
|
||||
for (int i = 0; i < y; i++) cout << endl;
|
||||
for (int i = 0; i < sy; i++) {
|
||||
for (int j = 0; j < x; j++) cout << " ";
|
||||
for (int j = 0; j < sx; j++) cout << "@";
|
||||
cout << endl;
|
||||
}
|
||||
cout << "<<<done printing rect>>>" << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int x, y, sx, sy;
|
||||
cout << "x, y, sx, sy >>";
|
||||
cin >> x >> y >> sx >> sy;
|
||||
print_rect(x, y, sx, sy);
|
||||
}
|
||||
20
1sem/programming basics/z5/13.cpp
Normal file
20
1sem/programming basics/z5/13.cpp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// pb_z5_13.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
double ai(int);
|
||||
|
||||
int main() {
|
||||
int N;
|
||||
cout << "N >>>";
|
||||
cin >> N;
|
||||
cout << "S= " << ai(N);
|
||||
}
|
||||
|
||||
double ai(int N) {
|
||||
double out = 0;
|
||||
for (int i = 1; i <= N; i++)
|
||||
out += (float)i/((float)i+1.f);
|
||||
return out;
|
||||
}
|
||||
28
1sem/programming basics/z5/14.cpp
Normal file
28
1sem/programming basics/z5/14.cpp
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// pb_z5_14.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
double ai(int, double);
|
||||
double spow(double, double);
|
||||
|
||||
int main() {
|
||||
int N;
|
||||
double x;
|
||||
cout << "N, x >>>";
|
||||
cin >> N >> x;
|
||||
cout << "P= " << ai(N, x);
|
||||
}
|
||||
|
||||
double ai(int N, double x) {
|
||||
double out = 1;
|
||||
for (int i = 1; i <= N; i++)
|
||||
out *= 2 * spow(x, i);
|
||||
return out;
|
||||
}
|
||||
|
||||
double spow(double f, double p) {
|
||||
double out = 1;
|
||||
for (int i = 0; i < p; i++) out *= f;
|
||||
return out;
|
||||
}
|
||||
25
1sem/programming basics/z5/15.cpp
Normal file
25
1sem/programming basics/z5/15.cpp
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// pb_z5_15.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
double ai(int);
|
||||
double s(int);
|
||||
|
||||
int main() {
|
||||
int N;
|
||||
cout << "N >>>";
|
||||
cin >> N;
|
||||
cout << "S= " << ai(N);
|
||||
}
|
||||
|
||||
double ai(int N) {
|
||||
double out = 0;
|
||||
for (int i = 1; i <= N; i++)
|
||||
out += i * s(i);
|
||||
return out;
|
||||
}
|
||||
|
||||
double s(int i) {
|
||||
return (i % 2? -1 : 1);
|
||||
}
|
||||
36
1sem/programming basics/z6/16.cpp
Normal file
36
1sem/programming basics/z6/16.cpp
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
// pb_z6_16.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
double f(double x)
|
||||
{
|
||||
return 2 * x*x - 3 * x + 1;
|
||||
}
|
||||
|
||||
void pp(double x)
|
||||
{
|
||||
cout.width(10);
|
||||
cout.fill(' ');
|
||||
cout.setf(ios_base::fixed);
|
||||
cout.setf(ios_base::right);
|
||||
cout << x;
|
||||
cout.width(15);
|
||||
cout.fill(' ');
|
||||
cout.setf(ios_base::right);
|
||||
cout << scientific << f(x);
|
||||
cout << endl;
|
||||
cout.unsetf(ios_base::scientific);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
double a, b, s;
|
||||
cout << "a, b, s >>>\n";
|
||||
cin >> a >> b >> s;
|
||||
while (a <= b)
|
||||
{
|
||||
pp(a);
|
||||
a += s;
|
||||
}
|
||||
}
|
||||
44
1sem/programming basics/z6/17.cpp
Normal file
44
1sem/programming basics/z6/17.cpp
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
// pb_z6_17.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
using namespace std;
|
||||
|
||||
double inline f(double x)
|
||||
{
|
||||
return 2 * x*x - 3 * x + 1;
|
||||
}
|
||||
|
||||
void pp1(double x)
|
||||
{
|
||||
cout.width(15);
|
||||
cout.fill(' ');
|
||||
cout.setf(ios_base::right);
|
||||
cout.setf(ios_base::fixed);
|
||||
cout << setprecision(3)<<x;
|
||||
}
|
||||
|
||||
void pp2(double x)
|
||||
{
|
||||
cout.width(15);
|
||||
cout.fill(' ');
|
||||
cout.setf(ios_base::right);
|
||||
cout << scientific << setprecision(5) << f(x);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
double a, b, s;
|
||||
cout << "a, b, s >>>\n";
|
||||
cin >> a >> b >> s;
|
||||
for (double _a = a; _a <= b; _a += s)
|
||||
{
|
||||
pp1(_a);
|
||||
}
|
||||
cout << endl;
|
||||
for (double _a = a; _a <= b; _a += s)
|
||||
{
|
||||
pp2(_a);
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
33
1sem/programming basics/z6/18.cpp
Normal file
33
1sem/programming basics/z6/18.cpp
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
// pb_z6_18.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
|
||||
double f(double);
|
||||
void plot(double, double, int);
|
||||
|
||||
int main() {
|
||||
double a, b, s;
|
||||
int scale;
|
||||
cout << "a, b, s, scale >>>";
|
||||
cin >> a >> b >> s >> scale;
|
||||
while (a < b) {
|
||||
double y = f(a);
|
||||
int v = (int)(y * scale);
|
||||
plot(a, y, v);
|
||||
a += s;
|
||||
}
|
||||
}
|
||||
|
||||
static const double PI = acos(-1);
|
||||
double f(double x) {
|
||||
return pow(sin(PI * x), 2);
|
||||
}
|
||||
|
||||
void plot(double x, double y, int v) {
|
||||
for (int i = 0; i < v; ++i) {
|
||||
cout << " ";
|
||||
}
|
||||
cout << "$(" << x << ", " << y << ")" << endl;
|
||||
}
|
||||
31
1sem/programming basics/z7/19.cpp
Normal file
31
1sem/programming basics/z7/19.cpp
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
// pb_z7_19.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
const int al = 100;
|
||||
|
||||
int min(int a, int b) {
|
||||
return (a < b? a : b);
|
||||
}
|
||||
|
||||
void print_array(int arr[], int len)
|
||||
{
|
||||
for (int i = 0; i < min(al, len); i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int count;
|
||||
cout << "count >>>";
|
||||
cin >> count;
|
||||
int a[al];
|
||||
for (int i = 0; i < al; i++)
|
||||
{
|
||||
a[i] = rand() % 100 + 1;
|
||||
}
|
||||
print_array(a, count);
|
||||
}
|
||||
32
1sem/programming basics/z7/20.cpp
Normal file
32
1sem/programming basics/z7/20.cpp
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// pb_z7_20.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int count_equal(const int arr[], int len, int x)
|
||||
{
|
||||
int count = 0;
|
||||
for (int i=0; i<len; i++)
|
||||
{
|
||||
count += arr[i] == x;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int n, x;
|
||||
cout << "n >>>";
|
||||
cin >>n;
|
||||
int *a = new int[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cout << "a[" << i << "] >>>";
|
||||
cin >> a[i];
|
||||
}
|
||||
cout << "x >>>";
|
||||
cin >> x;
|
||||
cout << "count of `" << x << "`: " << count_equal(a, n, x);
|
||||
delete[] a;
|
||||
}
|
||||
45
1sem/programming basics/z7/21.cpp
Normal file
45
1sem/programming basics/z7/21.cpp
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// pb_z7_21.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int splice_array(const int arr1[], const int arr2[], int len, int arr_out[])
|
||||
{
|
||||
int out = 0;
|
||||
for (int i=0; i<len; i++)
|
||||
{
|
||||
arr_out[i*2] = arr1[i];
|
||||
arr_out[i*2+1] = arr2[i];
|
||||
out += 2;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int n;
|
||||
cout << "n >>>";
|
||||
cin >> n;
|
||||
int *a1 = new int[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cout << "a1[" << i << "] >>>";
|
||||
cin >> a1[i];
|
||||
}
|
||||
|
||||
int *a2 = new int[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cout << "a2[" << i << "] >>>";
|
||||
cin >> a2[i];
|
||||
}
|
||||
int *a = new int[n * 2];
|
||||
int l = splice_array(a1, a2, n, a);
|
||||
cout << "length " << l << endl;
|
||||
for (int i = 0; i < l; i++) {
|
||||
cout << a[i] << " ";
|
||||
}
|
||||
delete[] a, a1, a2;
|
||||
}
|
||||
31
1sem/programming basics/z8/22.cpp
Normal file
31
1sem/programming basics/z8/22.cpp
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
// pb_z8_22.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
const int al = 100;
|
||||
|
||||
int min(int a, int b) {
|
||||
return (a < b? a : b);
|
||||
}
|
||||
|
||||
void print_array(int *arr, int len)
|
||||
{
|
||||
for (int i = 0; i < min(al, len); i++) {
|
||||
cout << *(arr + i) << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int count;
|
||||
cout << "count >>>";
|
||||
cin >> count;
|
||||
int *a = new int[al];
|
||||
for (int i = 0; i < al; i++)
|
||||
{
|
||||
*(a + i) = rand() % 100 + 1;
|
||||
}
|
||||
print_array(a, count);
|
||||
}
|
||||
31
1sem/programming basics/z8/23.cpp
Normal file
31
1sem/programming basics/z8/23.cpp
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
// pb_z8_23.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int count_equal(const int *arr, int len, int x)
|
||||
{
|
||||
int count = 0;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
count += *(arr + i) == x;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n, x;
|
||||
cout << "n >>>";
|
||||
cin >>n;
|
||||
int *a = new int[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cout << "a[" << i << "] >>>";
|
||||
cin >> *(a + i);
|
||||
}
|
||||
cout << "x >>>";
|
||||
cin >> x;
|
||||
cout << "count of `" << x << "`: " << count_equal(a, n, x);
|
||||
delete[] a;
|
||||
}
|
||||
43
1sem/programming basics/z8/24.cpp
Normal file
43
1sem/programming basics/z8/24.cpp
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
// pb_z8_24.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int splice_array(const int *arr1, const int *arr2, int len, int *arr_out)
|
||||
{
|
||||
int out = 0;
|
||||
for (int i=0; i<len; i++)
|
||||
{
|
||||
*(arr_out + i*2) = *(arr1 + i);
|
||||
*(arr_out + i*2+1) = *(arr2 + i);
|
||||
out += 2;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
cout << "n >>>";
|
||||
cin >> n;
|
||||
int *a1 = new int[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cout << "a1[" << i << "] >>>";
|
||||
cin >> *(a1 + i);
|
||||
}
|
||||
|
||||
int *a2 = new int[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cout << "a2[" << i << "] >>>";
|
||||
cin >> *(a2 + i);
|
||||
}
|
||||
int *a = new int[n * 2];
|
||||
int l = splice_array(a1, a2, n, a);
|
||||
cout << "length " << l << endl;
|
||||
for (int i = 0; i < l; i++) {
|
||||
cout << *(a + i) << " ";
|
||||
}
|
||||
delete[] a, a1, a2;
|
||||
}
|
||||
66
1sem/programming basics/z9/25.cpp
Normal file
66
1sem/programming basics/z9/25.cpp
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
// pb_z9_25.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void sort(double arr[], int len, bool asc);
|
||||
void print_array(const double *arr, size_t len);
|
||||
double *get_array(size_t len);
|
||||
int comp_asc(const void* a, const void* b);
|
||||
int comp_desc(const void* a, const void* b);
|
||||
|
||||
// 10 1 2 3 5 4 3 2 2 9 1 0
|
||||
int main() {
|
||||
size_t len;
|
||||
cout << "Array length >>>";
|
||||
cin >> len;
|
||||
cout << "Enter array elements >>>";
|
||||
double *a = get_array(len);
|
||||
bool asc;
|
||||
cout << "Ascending sort? (1/0) >>>";
|
||||
cin >> asc;
|
||||
cout << "Array before: ";
|
||||
print_array(a, len);
|
||||
sort(a, len, asc);
|
||||
cout << "Array after: ";
|
||||
print_array(a, len);
|
||||
}
|
||||
|
||||
void sort(double arr[], int len, bool asc) {
|
||||
qsort(arr, len, sizeof(double), asc? comp_asc : comp_desc);
|
||||
}
|
||||
|
||||
void print_array(const double *arr, size_t len) {
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
cout << *(arr + i) << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
double *get_array(size_t len) {
|
||||
auto *a = new double[len];
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
cin >> *(a + i);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
int comp_asc(const void* a, const void* b) {
|
||||
double arg1 = *(const double*)(a);
|
||||
double arg2 = *(const double*)(b);
|
||||
|
||||
if(arg1 < arg2) return -1;
|
||||
if(arg1 > arg2) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int comp_desc(const void* a, const void* b) {
|
||||
double arg1 = *(const double*)(a);
|
||||
double arg2 = *(const double*)(b);
|
||||
|
||||
if(arg1 < arg2) return 1;
|
||||
if(arg1 > arg2) return -1;
|
||||
return 0;
|
||||
}
|
||||
70
1sem/programming basics/z9/26.cpp
Normal file
70
1sem/programming basics/z9/26.cpp
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
// pb_z9_26.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void sort(int arr[], int len, bool even_greater);
|
||||
void print_array(const int *arr, size_t len);
|
||||
int *get_array(size_t len);
|
||||
|
||||
// 8 1 2 3 4 3 2 6 5 1
|
||||
int main() {
|
||||
size_t len;
|
||||
cout << "Array length >>>";
|
||||
cin >> len;
|
||||
cout << "Enter array elements >>>";
|
||||
int *a = get_array(len);
|
||||
bool even_greater;
|
||||
cout << "Is even greater? (1/0) >>>";
|
||||
cin >> even_greater;
|
||||
cout << "Array before: ";
|
||||
print_array(a, len);
|
||||
sort(a, len, even_greater);
|
||||
cout << "Array after: ";
|
||||
print_array(a, len);
|
||||
}
|
||||
|
||||
void sort(int arr[], int len, bool even_greater) {
|
||||
bool swapped;
|
||||
do {
|
||||
swapped = false;
|
||||
for (size_t i = 0; i < len - 1; i++) {
|
||||
int cv = *(arr + i),
|
||||
nv = *(arr + i + 1);
|
||||
if (cv % 2 == nv % 2) {
|
||||
if (cv > nv) {
|
||||
swap(*(arr + i), *(arr + i + 1));
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
if (even_greater) {
|
||||
if (cv % 2 == 0 && nv % 2 != 0) {
|
||||
swap(*(arr + i), *(arr + i + 1));
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (cv % 2 != 0 && nv % 2 == 0) {
|
||||
swap(*(arr + i), *(arr + i + 1));
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (swapped);
|
||||
}
|
||||
|
||||
void print_array(const int *arr, size_t len) {
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
cout << *(arr + i) << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int *get_array(size_t len) {
|
||||
int *a = new int[len];
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
cin >> *(a + i);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
65
1sem/programming basics/z9/27.cpp
Normal file
65
1sem/programming basics/z9/27.cpp
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
// pb_z9_27.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void sort(double arr[], int len, double x);
|
||||
void print_array(const double *arr, size_t len);
|
||||
double *get_array(size_t len);
|
||||
|
||||
// 8 1 2 3.5 4 3 2 2.9 1 2.5
|
||||
int main() {
|
||||
size_t len;
|
||||
cout << "Array length >>>";
|
||||
cin >> len;
|
||||
cout << "Enter array elements >>>";
|
||||
double *a = get_array(len);
|
||||
double x;
|
||||
cout << "x >>>";
|
||||
cin >> x;
|
||||
cout << "Array before: ";
|
||||
print_array(a, len);
|
||||
sort(a, len, x);
|
||||
cout << "Array after: ";
|
||||
print_array(a, len);
|
||||
auto *dist = new double[len];
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
*(dist + i) = abs(*(a + i) - x);
|
||||
}
|
||||
cout << "Array dist: ";
|
||||
print_array(dist, len);
|
||||
}
|
||||
|
||||
void sort(double arr[], int len, double x) {
|
||||
bool swapped;
|
||||
do {
|
||||
swapped = false;
|
||||
for (size_t i = 0; i < len-1; i++) {
|
||||
double cv = *(arr + i),
|
||||
nv = *(arr + i + 1),
|
||||
cvd = abs(cv - x),
|
||||
nvd = abs(nv - x);
|
||||
if (cvd > nvd) {
|
||||
swap(*(arr + i), *(arr + i + 1));
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
} while (swapped);
|
||||
}
|
||||
|
||||
void print_array(const double *arr, size_t len) {
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
cout << *(arr + i) << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
double *get_array(size_t len) {
|
||||
auto *a = new double[len];
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
cin >> *(a + i);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue