Added my works from 2022
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
a.out
|
||||
.ccls
|
||||
.ccls-cache
|
||||
56
main.cpp
Normal file
56
main.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <iostream>
|
||||
#include "task1.h"
|
||||
#include "task2.h"
|
||||
#include "task3.h"
|
||||
#include "task4.h"
|
||||
#include "task5.h"
|
||||
#include "task6.h"
|
||||
#include "task7.h"
|
||||
#include "task8.h"
|
||||
#include "task9.h"
|
||||
#include "task10.h"
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int id;
|
||||
bgn:
|
||||
cout << "Task id : "; cin >> id;
|
||||
|
||||
switch (id) {
|
||||
|
||||
case 1:
|
||||
task1::init();
|
||||
break;
|
||||
case 2:
|
||||
task2::init();
|
||||
break;
|
||||
case 3:
|
||||
task3::init();
|
||||
break;
|
||||
case 4:
|
||||
task4::init();
|
||||
break;
|
||||
case 5:
|
||||
task5::init();
|
||||
break;
|
||||
case 6:
|
||||
task6::init();
|
||||
break;
|
||||
case 7:
|
||||
task7::init();
|
||||
break;
|
||||
case 8:
|
||||
task8::init();
|
||||
break;
|
||||
case 9:
|
||||
task9::init();
|
||||
break;
|
||||
case 10:
|
||||
task10::init();
|
||||
break;
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
goto bgn;
|
||||
}
|
||||
135
task1.h
Normal file
135
task1.h
Normal file
@@ -0,0 +1,135 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
#pragma once
|
||||
|
||||
namespace task1 {
|
||||
|
||||
int F(int num); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>
|
||||
int a(int* m, int size);
|
||||
int b(int* m, int size);
|
||||
int v(int* m, int size);
|
||||
int g(int* m, int size);
|
||||
int d(int* m, int size);
|
||||
int e(int* m, int size);
|
||||
|
||||
int init()
|
||||
{
|
||||
cout << "TASK #1" << endl;
|
||||
setlocale(LC_ALL, "Russian");
|
||||
int n;
|
||||
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: "; cin >> n;
|
||||
|
||||
int* m = new int[n];
|
||||
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>: " << endl;
|
||||
for (int i = 0; i < n; i++) { cin >> m[i]; }
|
||||
|
||||
cout << "<EFBFBD>) " << a(m, n) << endl;
|
||||
cout << "<EFBFBD>) " << b(m, n) << endl;
|
||||
cout << "<EFBFBD>) " << v(m, n) << endl;
|
||||
cout << "<EFBFBD>) " << g(m, n) << endl;
|
||||
cout << "<EFBFBD>) " << d(m, n) << endl;
|
||||
cout << "<EFBFBD>) " << e(m, n) << endl;
|
||||
|
||||
delete[] m;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int F(int num)
|
||||
{
|
||||
int n = 1;
|
||||
for (int i = 1; i <= num; i++)
|
||||
{
|
||||
n *= i;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int a(int* m, int size)
|
||||
{
|
||||
int n = 0;
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
int num = m[i];
|
||||
if (num % 2 != 0)
|
||||
{
|
||||
n++;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int b(int* m, int size)
|
||||
{
|
||||
int n = 0;
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
int num = m[i];
|
||||
int sqrt_num = sqrt(num);
|
||||
|
||||
if ((sqrt_num * sqrt_num) == num && (num % 2 == 0))
|
||||
{
|
||||
n++;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int v(int* m, int size)
|
||||
{
|
||||
int n = 0;
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
int num = m[i];
|
||||
if (num % 3 == 0 && num % 5 != 0)
|
||||
{
|
||||
n++;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int g(int* m, int size)
|
||||
{
|
||||
int n = 0;
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
int k = i + 1;
|
||||
int num = m[i];
|
||||
if ((pow(2, k) < num) && (num < F(k)))
|
||||
{
|
||||
n++;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int d(int* m, int size)
|
||||
{
|
||||
int n = 0;
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
int k = i + 1;
|
||||
int num = m[i];
|
||||
if (i - 1 >= 0 && i + 1 < size && num < ((m[i - 1] + m[i + 1]) / 2))
|
||||
{
|
||||
n++;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int e(int* m, int size)
|
||||
{
|
||||
int n = 0;
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
int num = m[i];
|
||||
if (i % 2 == 0 && num % 2 != 0)
|
||||
{
|
||||
n++;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
}
|
||||
45
task10.h
Normal file
45
task10.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
#pragma once
|
||||
|
||||
namespace task10 {
|
||||
|
||||
int init()
|
||||
{
|
||||
|
||||
|
||||
cout << "TASK #9" << endl;
|
||||
setlocale(LC_ALL, "Russian");
|
||||
|
||||
queue<int> dig1;
|
||||
|
||||
ifstream input("C:\\input2.txt");
|
||||
string data;
|
||||
while (getline(input, data))
|
||||
{
|
||||
if (data.size() == 2)
|
||||
{
|
||||
dig2.push(n);
|
||||
}
|
||||
}
|
||||
|
||||
while (!dig1.empty())
|
||||
{
|
||||
cout << dig1.front() << endl;
|
||||
dig1.pop();
|
||||
}
|
||||
while (!dig2.empty())
|
||||
{
|
||||
cout << dig2.front() << endl;
|
||||
dig2.pop();
|
||||
}
|
||||
|
||||
cout << "<EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>))" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
34
task2.h
Normal file
34
task2.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#pragma once
|
||||
|
||||
namespace task2 {
|
||||
|
||||
int init() {
|
||||
|
||||
cout << "TASK #2" << endl;
|
||||
|
||||
setlocale(LC_ALL, "Russian");
|
||||
int n; cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: "; cin >> n;
|
||||
int amt = 0, sum = 0;
|
||||
int* m = new int[n];
|
||||
|
||||
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>:" << endl;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
|
||||
int num; cin >> num;
|
||||
m[i] = num; //<2F><> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>
|
||||
if (num % 5 == 0 && num % 7 != 0)
|
||||
{
|
||||
amt++;
|
||||
sum += num;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "<EFBFBD><EFBFBD><EFBFBD>-<2D><>: " << amt << endl << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: " << sum;
|
||||
|
||||
delete[] m;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
38
task3.h
Normal file
38
task3.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace task3 {
|
||||
|
||||
int init()
|
||||
{
|
||||
|
||||
cout << "TASK #3" << endl;
|
||||
|
||||
setlocale(LC_ALL, "Russian");
|
||||
int n; cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: "; cin >> n;
|
||||
int amt = 0;
|
||||
int* m = new int[n];
|
||||
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>: " << endl;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cin>>m[i];
|
||||
}
|
||||
|
||||
int max = 0, cur = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cur = (cur + 1) * (m[i] == 0);
|
||||
max = cur > max ? cur : max;
|
||||
}
|
||||
|
||||
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> " << max * (max!=1) << " <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>\n";
|
||||
cout << "<EFBFBD>) " << (max >= 2) << endl;
|
||||
cout << "<EFBFBD>) " << (max >= 3) << endl;
|
||||
|
||||
delete[] m;
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
69
task4.h
Normal file
69
task4.h
Normal file
@@ -0,0 +1,69 @@
|
||||
#include <iostream>
|
||||
#pragma once
|
||||
|
||||
namespace task4 {
|
||||
|
||||
struct Node
|
||||
{
|
||||
public:
|
||||
int key;
|
||||
Node* next;
|
||||
};
|
||||
|
||||
Node* newNode(int key); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
Node* list(int size, bool print); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
int sum(Node* head); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
int init()
|
||||
{
|
||||
setlocale(LC_ALL, "Russian");
|
||||
int n; cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: "; cin >> n;
|
||||
|
||||
Node* array = list(n, true);
|
||||
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: " << sum(array);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Node* newNode(int key)
|
||||
{
|
||||
Node* node = new Node;
|
||||
node->key = key;
|
||||
node->next = nullptr;
|
||||
return node;
|
||||
}
|
||||
|
||||
Node* list(int size, bool print)
|
||||
{
|
||||
Node* head = newNode(1);
|
||||
Node* last = newNode(2);
|
||||
head->next = last;
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
Node* node = newNode(rand());
|
||||
|
||||
if (print) { cout << node->key << " "; }
|
||||
|
||||
last->next = node;
|
||||
last = node;
|
||||
}
|
||||
if (print) { cout << endl; }
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
int sum(Node* head)
|
||||
{
|
||||
int sum = 0;
|
||||
Node* ptr = head;
|
||||
|
||||
while (ptr)
|
||||
{
|
||||
sum = sum + ptr->key;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
37
task5.h
Normal file
37
task5.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#pragma once
|
||||
|
||||
namespace task5 {
|
||||
|
||||
int init()
|
||||
{
|
||||
cout << "TASK #5" << endl;
|
||||
setlocale(LC_ALL, "Russian");
|
||||
|
||||
int n; cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: "; cin >> n;
|
||||
double* m = new double[n];
|
||||
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>:" << endl;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cin >> m[i];
|
||||
}
|
||||
|
||||
double min = m[1];
|
||||
for (int i = 1; i < n; i++)
|
||||
{
|
||||
double num = m[i];
|
||||
if (num > min)
|
||||
{
|
||||
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>. " << endl;
|
||||
delete[] m;
|
||||
return 0;
|
||||
}
|
||||
min = num;
|
||||
}
|
||||
|
||||
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>. " << endl;
|
||||
delete[] m;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
175
task6.h
Normal file
175
task6.h
Normal file
@@ -0,0 +1,175 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#pragma once
|
||||
|
||||
namespace task6 {
|
||||
|
||||
struct Node
|
||||
{
|
||||
public:
|
||||
int key;
|
||||
Node* next;
|
||||
};
|
||||
|
||||
Node* newNode(int key); //создает узел
|
||||
Node* rand_list(int size); //создает список
|
||||
void print_list(Node* head); //пишет элементы списка
|
||||
|
||||
bool is_list_empty(Node* head); //пишет, что список пуст, если подкинуть nullptr
|
||||
void push_back(Node* head, int key); //поместить в конец
|
||||
void push_forward(Node*& head, int key); //поместить в начало
|
||||
void squeeze_in(Node* head, int pos, int key); //"всунуть" после элемента
|
||||
|
||||
int list_size(Node* head); //узнать размер списка
|
||||
void behead(Node*& head); //получить список без первого элемента
|
||||
|
||||
int init()
|
||||
{
|
||||
setlocale(LC_ALL, "Russian");
|
||||
int n; cout << "Введите размер: "; cin >> n;
|
||||
Node* lst = rand_list(n);
|
||||
|
||||
if (is_list_empty(lst))
|
||||
{
|
||||
cout << "Список является пустым." << endl;
|
||||
return 0;
|
||||
}
|
||||
cout << "Список содержит элементы: " << endl;
|
||||
print_list(lst);
|
||||
|
||||
cout << "\n\nДобавим элемент 11111 в начало:" << endl;
|
||||
push_forward(lst, 11111);
|
||||
print_list(lst);
|
||||
|
||||
cout << "\n\nДобавим элемент 22222 в конец:" << endl;
|
||||
push_back(lst, 22222);
|
||||
print_list(lst);
|
||||
|
||||
cout << "\n\nДобавим элемент 33333 где-то в середине:" << endl;
|
||||
squeeze_in(lst, n/2+1, 33333);
|
||||
print_list(lst);
|
||||
|
||||
cout << "\n\nВсего элементов сейчас: " << list_size(lst) << endl;
|
||||
|
||||
cout << "\n\nПолучим список без первого элемента с помощью функции обезглавливания:" << endl;
|
||||
behead(lst);
|
||||
print_list(lst);
|
||||
|
||||
cout << "\n\nДа это жоско";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Node* newNode(int key)
|
||||
{
|
||||
Node* node = new Node;
|
||||
node->key = key;
|
||||
node->next = nullptr;
|
||||
return node;
|
||||
}
|
||||
|
||||
Node* rand_list(int size)
|
||||
{
|
||||
Node* head = newNode(rand());
|
||||
Node* last = newNode(rand());
|
||||
head->next = last;
|
||||
|
||||
for (int i = 0; i < size-2; i++)
|
||||
{
|
||||
Node* node = newNode(rand());
|
||||
|
||||
last->next = node;
|
||||
last = node;
|
||||
}
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
void print_list(Node* head)
|
||||
{
|
||||
Node* ptr = head;
|
||||
while (ptr)
|
||||
{
|
||||
cout << ptr->key << " -> ";
|
||||
ptr = ptr->next;
|
||||
}
|
||||
cout << "nullptr" << endl;
|
||||
}
|
||||
|
||||
bool is_list_empty(Node* head)
|
||||
{
|
||||
bool empty = true;
|
||||
|
||||
while (head)
|
||||
{
|
||||
empty = false;
|
||||
break;
|
||||
}
|
||||
|
||||
return empty;
|
||||
}
|
||||
|
||||
void push_back(Node* head, int key)
|
||||
{
|
||||
Node* last = nullptr;
|
||||
Node* ptr = head;
|
||||
while (ptr)
|
||||
{
|
||||
last = ptr;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
ptr = newNode(key);
|
||||
last->next = ptr;
|
||||
}
|
||||
|
||||
void push_forward(Node*& head, int key)
|
||||
{
|
||||
Node* node = newNode(key);
|
||||
node->next = head;
|
||||
head = node;
|
||||
}
|
||||
|
||||
void squeeze_in(Node* head, int pos, int key)
|
||||
{
|
||||
Node* node_left=head;
|
||||
Node* node_right;
|
||||
Node* node = newNode(key);
|
||||
|
||||
for (int i = 0; i < pos-1; i++)
|
||||
{
|
||||
node_left = node_left->next;
|
||||
}
|
||||
node_right = node_left->next->next;
|
||||
node_left->next = node;
|
||||
node->next = node_right;
|
||||
}
|
||||
|
||||
int list_size(Node* head)
|
||||
{
|
||||
int num=0;
|
||||
Node* ptr = head;
|
||||
while (ptr)
|
||||
{
|
||||
ptr = ptr->next;
|
||||
num++;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
void behead(Node*& head)
|
||||
{
|
||||
/*
|
||||
⢿⣿⣿⣿⣭⠹⠛⠛⠛⢿⣿⣿⣿⣿⡿⣿⠷⠶⠿⢻⣿⣛⣦⣙⠻⣿
|
||||
⣿⣿⢿⣿⠏⠀⠀⡀⠀⠈⣿⢛⣽⣜⠯⣽⠀⠀⠀⠀⠙⢿⣷⣻⡀⢿
|
||||
⠐⠛⢿⣾⣖⣤⡀⠀⢀⡰⠿⢷⣶⣿⡇⠻⣖⣒⣒⣶⣿⣿⡟⢙⣶⣮
|
||||
⣤⠀⠀⠛⠻⠗⠿⠿⣯⡆⣿⣛⣿⡿⠿⠮⡶⠼⠟⠙⠊⠁⠀⠸⢣⣿
|
||||
⣿⣷⡀⠀⠀⠀⠀⠠⠭⣍⡉⢩⣥⡤⠥⣤⡶⣒⠀⠀⠀⠀⠀⢰⣿⣿
|
||||
⣿⣿⡽⡄⠀⠀⠀⢿⣿⣆⣿⣧⢡⣾⣿⡇⣾⣿⡇⠀⠀⠀⠀⣿⡇⠃
|
||||
⣿⣿⣷⣻⣆⢄⠀⠈⠉⠉⠛⠛⠘⠛⠛⠛⠙⠛⠁⠀⠀⠀⠀⣿⡇⢸
|
||||
⢞⣿⣿⣷⣝⣷⣝⠦⡀⠀⠀⠀⠀⠀⠀⠀⡀⢀⠀⠀⠀⠀⠀⠛⣿⠈
|
||||
⣦⡑⠛⣟⢿⡿⣿⣷⣝⢧⡀⠀⠀⣶⣸⡇⣿⢸⣧⠀⠀⠀⠀⢸⡿⡆
|
||||
⣿⣿⣷⣮⣭⣍⡛⠻⢿⣷⠿⣶⣶⣬⣬⣁⣉⣀⣀⣁⡤⢴⣺⣾⣽⡇
|
||||
*/
|
||||
head = head->next;
|
||||
}
|
||||
}
|
||||
396
task7.h
Normal file
396
task7.h
Normal file
@@ -0,0 +1,396 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
#pragma once
|
||||
|
||||
namespace task7 {
|
||||
|
||||
struct Node
|
||||
{
|
||||
public:
|
||||
int key;
|
||||
Node* next;
|
||||
};
|
||||
|
||||
Node* newNode(int key); //создает узел
|
||||
Node* rand_list(int size); //создает список
|
||||
void print_list(Node* head); //пишет элементы списка
|
||||
|
||||
bool is_list_empty(Node* head); //пишет, что список пуст, если подкинуть nullptr
|
||||
void push_back(Node* head, int key); //поместить в конец
|
||||
void push_forward(Node*& head, int key); //поместить в начало
|
||||
void squeeze_in(Node* head, int pos, int key); //"всунуть" после элемента
|
||||
void behead(Node*& head);
|
||||
int list_size(Node* head); //узнать размер списка
|
||||
|
||||
bool equal(Node* head1, Node* head2); //сравнивает списки
|
||||
bool all_in(Node* head1, Node* head2); //проверяет, есть ли все элементы первого списка в другом
|
||||
bool dupes_present(Node* head); //проверка на дубликаты
|
||||
void head_to_end(Node*& head); //перемещает первый элемент в конец
|
||||
void end_to_head(Node*& head); //перемещает последний элемент в начало
|
||||
void merge(Node* head1, Node* head2); //переносит элементы списка 2 в конец списка 1
|
||||
void reversed(Node*& head); //возвращает элементы массива в обрат. порядке
|
||||
bool in_list(Node* head, int key); //ДЛЯ ЗАДАНИЯ ПОД БУКВОЙ З). Определяет, есть ли элемент в списке
|
||||
void remove_dupes(Node*& head); //not implemented
|
||||
|
||||
int init()
|
||||
{
|
||||
cout << "TASK #7" << endl;
|
||||
setlocale(LC_ALL, "Russian");
|
||||
|
||||
cout << "В конце задания 6 ничего не предвещало беды\n";
|
||||
|
||||
int n1; cout << "Введите размер списка 1: "; cin >> n1;
|
||||
Node* lst1 = newNode(0);
|
||||
cout << "Введите числа:" << endl;
|
||||
for (int i = 0; i < n1; i++)
|
||||
{
|
||||
int num; cin >> num;
|
||||
push_back(lst1, num);
|
||||
}
|
||||
|
||||
int n2; cout << "Введите размер списка 2: "; cin >> n2;
|
||||
Node* lst2 = newNode(0);
|
||||
cout << "Введите числа:" << endl;
|
||||
for (int i = 0; i < n2; i++)
|
||||
{
|
||||
int num; cin >> num;
|
||||
push_back(lst2, num);
|
||||
}
|
||||
behead(lst1);
|
||||
behead(lst2);
|
||||
cout << "\n\nИмеем два списка: \n";
|
||||
print_list(lst1);
|
||||
print_list(lst2);
|
||||
cout << "\n\n";
|
||||
|
||||
if (equal(lst1, lst2))
|
||||
{
|
||||
cout << "Списки совпадают";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Списки не равны";
|
||||
}
|
||||
cout << "\n\n";
|
||||
|
||||
if (all_in(lst1, lst2))
|
||||
{
|
||||
cout << "Все элементы первого списка есть во втором";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Первый список имеет элементы, которых нет во втором";
|
||||
}
|
||||
cout << "\n\n";
|
||||
|
||||
if (dupes_present(lst1))
|
||||
{
|
||||
cout << "В списке есть дубликаты";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "В списке нет дубликатов";
|
||||
}
|
||||
cout << "\n\n";
|
||||
print_list(lst1);
|
||||
cout << "Перенесем первый элемент списка 1 в конец : \n";
|
||||
head_to_end(lst1);
|
||||
print_list(lst1);
|
||||
|
||||
cout << "\n\n";
|
||||
print_list(lst2);
|
||||
cout << "Перенесем последний элемент списка 2 в начало : \n";
|
||||
end_to_head(lst2);
|
||||
print_list(lst2);
|
||||
|
||||
cout << "\n\n";
|
||||
print_list(lst1);
|
||||
print_list(lst2);
|
||||
cout << "Соединим два списка: \n";
|
||||
merge(lst1, lst2);
|
||||
print_list(lst1);
|
||||
cout << "Развернем список: \n";
|
||||
reversed(lst1);
|
||||
print_list(lst1);
|
||||
cout << "Удалим дубликаты из списка: \n";
|
||||
remove_dupes(lst1);
|
||||
print_list(lst1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Node* newNode(int key)
|
||||
{
|
||||
Node* node = new Node;
|
||||
node->key = key;
|
||||
node->next = nullptr;
|
||||
return node;
|
||||
}
|
||||
|
||||
Node* rand_list(int size)
|
||||
{
|
||||
Node* head = newNode(rand());
|
||||
Node* last = newNode(rand());
|
||||
head->next = last;
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
Node* node = newNode(i);
|
||||
|
||||
last->next = node;
|
||||
last = node;
|
||||
}
|
||||
|
||||
return head->next->next;
|
||||
}
|
||||
|
||||
void print_list(Node* head)
|
||||
{
|
||||
Node* ptr = head;
|
||||
while (ptr)
|
||||
{
|
||||
cout << ptr->key << " -> ";
|
||||
ptr = ptr->next;
|
||||
}
|
||||
cout << "nullptr" << endl;
|
||||
}
|
||||
|
||||
bool is_list_empty(Node* head)
|
||||
{
|
||||
bool empty = true;
|
||||
|
||||
while (head)
|
||||
{
|
||||
empty = false;
|
||||
break;
|
||||
}
|
||||
|
||||
return empty;
|
||||
}
|
||||
|
||||
void push_back(Node* head, int key)
|
||||
{
|
||||
Node* ptr = head;
|
||||
while (ptr->next)
|
||||
{
|
||||
ptr = ptr->next;
|
||||
}
|
||||
ptr->next = newNode(key);
|
||||
}
|
||||
|
||||
void push_forward(Node*& head, int key)
|
||||
{
|
||||
Node* node = newNode(key);
|
||||
node->next = head;
|
||||
head = node;
|
||||
}
|
||||
|
||||
void squeeze_in(Node* head, int pos, int key)
|
||||
{
|
||||
Node* node_left = head;
|
||||
Node* node_right;
|
||||
Node* node = newNode(key);
|
||||
|
||||
for (int i = 0; i < pos - 1; i++)
|
||||
{
|
||||
node_left = node_left->next;
|
||||
}
|
||||
node_right = node_left->next->next;
|
||||
node_left->next = node;
|
||||
node->next = node_right;
|
||||
}
|
||||
|
||||
void behead(Node*& head)
|
||||
{
|
||||
/*
|
||||
⢿⣿⣿⣿⣭⠹⠛⠛⠛⢿⣿⣿⣿⣿⡿⣿⠷⠶⠿⢻⣿⣛⣦⣙⠻⣿
|
||||
⣿⣿⢿⣿⠏⠀⠀⡀⠀⠈⣿⢛⣽⣜⠯⣽⠀⠀⠀⠀⠙⢿⣷⣻⡀⢿
|
||||
⠐⠛⢿⣾⣖⣤⡀⠀⢀⡰⠿⢷⣶⣿⡇⠻⣖⣒⣒⣶⣿⣿⡟⢙⣶⣮
|
||||
⣤⠀⠀⠛⠻⠗⠿⠿⣯⡆⣿⣛⣿⡿⠿⠮⡶⠼⠟⠙⠊⠁⠀⠸⢣⣿
|
||||
⣿⣷⡀⠀⠀⠀⠀⠠⠭⣍⡉⢩⣥⡤⠥⣤⡶⣒⠀⠀⠀⠀⠀⢰⣿⣿
|
||||
⣿⣿⡽⡄⠀⠀⠀⢿⣿⣆⣿⣧⢡⣾⣿⡇⣾⣿⡇⠀⠀⠀⠀⣿⡇⠃
|
||||
⣿⣿⣷⣻⣆⢄⠀⠈⠉⠉⠛⠛⠘⠛⠛⠛⠙⠛⠁⠀⠀⠀⠀⣿⡇⢸
|
||||
⢞⣿⣿⣷⣝⣷⣝⠦⡀⠀⠀⠀⠀⠀⠀⠀⡀⢀⠀⠀⠀⠀⠀⠛⣿⠈
|
||||
⣦⡑⠛⣟⢿⡿⣿⣷⣝⢧⡀⠀⠀⣶⣸⡇⣿⢸⣧⠀⠀⠀⠀⢸⡿⡆
|
||||
⣿⣿⣷⣮⣭⣍⡛⠻⢿⣷⠿⣶⣶⣬⣬⣁⣉⣀⣀⣁⡤⢴⣺⣾⣽⡇
|
||||
*/
|
||||
Node* _head = head;
|
||||
head=head->next;
|
||||
delete _head;
|
||||
|
||||
}
|
||||
|
||||
int list_size(Node* head)
|
||||
{
|
||||
int num = 0;
|
||||
Node* ptr = head;
|
||||
while (ptr)
|
||||
{
|
||||
ptr = ptr->next;
|
||||
num++;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
bool equal(Node* head1, Node* head2)
|
||||
{
|
||||
if (list_size(head1) != list_size(head2))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Node* ptr1 = head1; Node* ptr2 = head2;
|
||||
while (ptr1)
|
||||
{
|
||||
if (ptr1->key != ptr2->key)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
ptr1 = ptr1->next;
|
||||
ptr2 = ptr2->next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool all_in(Node* head1, Node* head2)
|
||||
{
|
||||
Node* ptr1 = head1;
|
||||
while (ptr1)
|
||||
{
|
||||
bool is_in = false;
|
||||
Node* ptr2 = head2;
|
||||
|
||||
while (ptr2)
|
||||
{
|
||||
if (ptr1->key == ptr2->key)
|
||||
{
|
||||
is_in = true;
|
||||
break;
|
||||
}
|
||||
|
||||
ptr2 = ptr2->next;
|
||||
}
|
||||
|
||||
if (!is_in)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
ptr1 = ptr1->next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dupes_present(Node* head)
|
||||
{
|
||||
Node* ptr1 = head;
|
||||
while (ptr1)
|
||||
{
|
||||
int count = -1;
|
||||
Node* ptr2 = head;
|
||||
while (ptr2)
|
||||
{
|
||||
if (ptr1->key == ptr2->key)
|
||||
{
|
||||
count++;
|
||||
if (count > 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
ptr2 = ptr2->next;
|
||||
}
|
||||
ptr1 = ptr1->next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void head_to_end(Node*& head)
|
||||
{
|
||||
Node* _head = head;
|
||||
Node* ptr = head;
|
||||
while (ptr->next)
|
||||
{
|
||||
ptr = ptr->next;
|
||||
}
|
||||
ptr->next = _head;
|
||||
head = head->next;
|
||||
_head->next = NULL;
|
||||
|
||||
|
||||
}
|
||||
|
||||
void end_to_head(Node*& head)
|
||||
{
|
||||
Node* ptr = head;
|
||||
while (ptr->next->next)
|
||||
{
|
||||
ptr = ptr->next;
|
||||
}
|
||||
ptr->next->next = head;
|
||||
head = ptr->next;
|
||||
ptr->next = NULL;
|
||||
}
|
||||
|
||||
void merge(Node* head1, Node* head2)
|
||||
{
|
||||
Node* ptr = head1;
|
||||
while (ptr->next)
|
||||
{
|
||||
ptr = ptr->next;
|
||||
}
|
||||
|
||||
Node* ptr2 = head2;
|
||||
while (ptr2)
|
||||
{
|
||||
ptr->next = ptr2;
|
||||
ptr = ptr2;
|
||||
ptr2 = ptr2->next;
|
||||
}
|
||||
}
|
||||
|
||||
void reversed(Node*& head)
|
||||
{
|
||||
Node* rev = nullptr;
|
||||
Node* ptr = head;
|
||||
while (ptr)
|
||||
{
|
||||
push_forward(rev, ptr->key);
|
||||
ptr = ptr->next;
|
||||
}
|
||||
|
||||
head = rev;
|
||||
}
|
||||
|
||||
bool in_list(Node* head, int key)
|
||||
{
|
||||
Node* ptr = head;
|
||||
while (ptr)
|
||||
{
|
||||
if (ptr->key == key)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void remove_dupes(Node*& head)
|
||||
{
|
||||
Node* buffer = newNode(head->key);
|
||||
Node* result = newNode(head->key);
|
||||
|
||||
while (head)
|
||||
{
|
||||
if (!in_list(buffer, head->key))
|
||||
{
|
||||
push_back(result, head->key);
|
||||
push_back(buffer, head->key);
|
||||
}
|
||||
head = head->next;
|
||||
}
|
||||
head = result;
|
||||
}
|
||||
}
|
||||
114
task8.h
Normal file
114
task8.h
Normal file
@@ -0,0 +1,114 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
#pragma once
|
||||
|
||||
namespace task8 {
|
||||
|
||||
struct Node
|
||||
{
|
||||
public:
|
||||
int key;
|
||||
Node* next;
|
||||
};
|
||||
|
||||
Node* newNode(int key); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
Node* rand_list(int size); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
void print_list(Node* head); //<2F><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
void cycle(Node*&head, int offset); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
int init()
|
||||
{
|
||||
int n; cout << "Enter size: "; cin >> n;
|
||||
Node* lst = rand_list(n);
|
||||
print_list(lst);
|
||||
int offset; cout << "Enter offset: "; cin >> offset;
|
||||
|
||||
cycle(lst, offset);
|
||||
print_list(lst);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Node* newNode(int key)
|
||||
{
|
||||
Node* node = new Node;
|
||||
node->key = key;
|
||||
node->next = nullptr;
|
||||
return node;
|
||||
}
|
||||
|
||||
Node* rand_list(int size)
|
||||
{
|
||||
Node* head = newNode(rand());
|
||||
Node* last = newNode(rand());
|
||||
head->next = last;
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
Node* node = newNode(i+1);
|
||||
|
||||
last->next = node;
|
||||
last = node;
|
||||
}
|
||||
|
||||
return head->next->next;
|
||||
}
|
||||
|
||||
void push_back(Node* head, int key)
|
||||
{
|
||||
Node* last = nullptr;
|
||||
Node* ptr = head;
|
||||
while (ptr)
|
||||
{
|
||||
last = ptr;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
ptr = newNode(key);
|
||||
last->next = ptr;
|
||||
}
|
||||
|
||||
Node* push_forward(Node* head, int key)
|
||||
{
|
||||
Node* node = newNode(key);
|
||||
node->next = head;
|
||||
return node;
|
||||
}
|
||||
|
||||
void print_list(Node* head)
|
||||
{
|
||||
Node* ptr = head;
|
||||
while (ptr)
|
||||
{
|
||||
cout << ptr->key << " -> ";
|
||||
ptr = ptr->next;
|
||||
}
|
||||
cout << "nullptr" << endl;
|
||||
}
|
||||
|
||||
void cycle(Node*& head, int offset)
|
||||
{
|
||||
int amt = 0;
|
||||
Node* ptr = head;
|
||||
while (ptr->next)
|
||||
{
|
||||
ptr = ptr->next;
|
||||
amt++;
|
||||
}
|
||||
ptr->next = head;
|
||||
Node* prv = ptr;
|
||||
|
||||
if (offset < 0)
|
||||
{
|
||||
offset = abs(amt+1 + offset);
|
||||
}
|
||||
|
||||
for (int i = 0; i < offset; i++)
|
||||
{
|
||||
prv = prv->next;
|
||||
head = head->next;
|
||||
}
|
||||
prv->next = nullptr;
|
||||
|
||||
}
|
||||
}
|
||||
35
task9.h
Normal file
35
task9.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
#pragma once
|
||||
|
||||
namespace task9 {
|
||||
|
||||
int init()
|
||||
{
|
||||
cout << "TASK #9" << endl;
|
||||
setlocale(LC_ALL, "Russian");
|
||||
|
||||
stack<char> chars;
|
||||
ifstream input("C:\\input.txt");
|
||||
string data;
|
||||
while (getline(input, data))
|
||||
{
|
||||
for (char& c : data)
|
||||
{
|
||||
chars.push(c);
|
||||
}
|
||||
while (!chars.empty())
|
||||
{
|
||||
cout << chars.top();
|
||||
chars.pop();
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user