Добавил программы по тестированию
This commit is contained in:
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
*.docx
|
||||||
|
*.pdf
|
||||||
|
*.exe
|
||||||
|
stuff
|
||||||
65
programs/1_bubble_sort.cpp
Normal file
65
programs/1_bubble_sort.cpp
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void swap(int &a, int &b)
|
||||||
|
{
|
||||||
|
int tmp = a;
|
||||||
|
a = b;
|
||||||
|
b = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void bubbleSort(int arr[], int n)
|
||||||
|
{
|
||||||
|
int nPairs = n;
|
||||||
|
A:
|
||||||
|
nPairs = nPairs - 1;
|
||||||
|
bool hasSwapped = false;
|
||||||
|
int i = 0;
|
||||||
|
while (i < nPairs)
|
||||||
|
{
|
||||||
|
if (arr[i] > arr[i + 1])
|
||||||
|
{
|
||||||
|
swap(arr[i], arr[i + 1]);
|
||||||
|
hasSwapped = true;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if (hasSwapped)
|
||||||
|
{
|
||||||
|
goto A;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
|
||||||
|
if (n < 1 || n > 10000) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int *arr = new int[n];
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
int tmp;
|
||||||
|
cin >> tmp;
|
||||||
|
if (tmp > 10000 || tmp < -10000) return 1;
|
||||||
|
arr[i] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
bubbleSort(arr, n);
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
cout << arr[i];
|
||||||
|
if (i < n - 1)
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
delete[] arr;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
31
programs/2_exponentiation.cpp
Normal file
31
programs/2_exponentiation.cpp
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
unsigned long long n, k;
|
||||||
|
|
||||||
|
cin >> n >> k;
|
||||||
|
|
||||||
|
if (n < 1 || n > 15 || k < 1 || k > 15)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long long r = 1;
|
||||||
|
|
||||||
|
while (k > 0)
|
||||||
|
{
|
||||||
|
if (k % 2 == 1)
|
||||||
|
{
|
||||||
|
r *= n;
|
||||||
|
}
|
||||||
|
n *= n;
|
||||||
|
k /= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << r << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
165
programs/3_bst.cpp
Normal file
165
programs/3_bst.cpp
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <locale>
|
||||||
|
#include <vector>
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct BSTNode {
|
||||||
|
string word;
|
||||||
|
BSTNode* left;
|
||||||
|
BSTNode* right;
|
||||||
|
BSTNode(const string& w) : word(w), left(nullptr), right(nullptr) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class BST {
|
||||||
|
BSTNode* root;
|
||||||
|
|
||||||
|
bool insertNode(BSTNode*& node, const string& w) {
|
||||||
|
if (!node) {
|
||||||
|
node = new BSTNode(w);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (w == node->word) return false;
|
||||||
|
if (w < node->word) return insertNode(node->left, w);
|
||||||
|
return insertNode(node->right, w);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool removeNode(BSTNode*& node, const string& w) {
|
||||||
|
if (!node) return false;
|
||||||
|
if (w < node->word) return removeNode(node->left, w);
|
||||||
|
if (w > node->word) return removeNode(node->right, w);
|
||||||
|
if (!node->left) {
|
||||||
|
BSTNode* temp = node->right;
|
||||||
|
delete node;
|
||||||
|
node = temp;
|
||||||
|
} else if (!node->right) {
|
||||||
|
BSTNode* temp = node->left;
|
||||||
|
delete node;
|
||||||
|
node = temp;
|
||||||
|
} else {
|
||||||
|
BSTNode* minNode = findMin(node->right);
|
||||||
|
node->word = minNode->word;
|
||||||
|
removeNode(node->right, minNode->word);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
BSTNode* findMin(BSTNode* node) {
|
||||||
|
if (!node) return nullptr;
|
||||||
|
while (node->left) node = node->left;
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clearTree(BSTNode*& node) {
|
||||||
|
if (!node) return;
|
||||||
|
clearTree(node->left);
|
||||||
|
clearTree(node->right);
|
||||||
|
delete node;
|
||||||
|
node = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void inorder(BSTNode* node, vector<string>& result) const {
|
||||||
|
if (!node) return;
|
||||||
|
inorder(node->left, result);
|
||||||
|
result.push_back(node->word);
|
||||||
|
inorder(node->right, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
BST() : root(nullptr) {}
|
||||||
|
bool insert(const string& w) { return insertNode(root, w); }
|
||||||
|
bool remove(const string& w) { return removeNode(root, w); }
|
||||||
|
void clear() { clearTree(root); }
|
||||||
|
vector<string> getAllWords() const {
|
||||||
|
vector<string> result;
|
||||||
|
inorder(root, result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
SetConsoleCP(1251);
|
||||||
|
SetConsoleOutputCP(1251);
|
||||||
|
setlocale(LC_ALL, "Russian");
|
||||||
|
BST dictionary;
|
||||||
|
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:\n"
|
||||||
|
<< "0 - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;\n"
|
||||||
|
<< "1,<<3C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>> - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;\n"
|
||||||
|
<< "2,<<3C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>> - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;\n"
|
||||||
|
<< "3 - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>;\n"
|
||||||
|
<< "4 - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.\n\n";
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
string input;
|
||||||
|
if (!getline(cin, input)) break;
|
||||||
|
if (input.empty()) {
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>!\n";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int commaPos = input.find(',');
|
||||||
|
int command;
|
||||||
|
string cmdStr, word;
|
||||||
|
if (commaPos == (int)string::npos) {
|
||||||
|
cmdStr = input;
|
||||||
|
} else {
|
||||||
|
cmdStr = input.substr(0, commaPos);
|
||||||
|
if (commaPos + 1 < (int)input.size()) {
|
||||||
|
word = input.substr(commaPos + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try { command = stoi(cmdStr); }
|
||||||
|
catch (...) {
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>!\n";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (command) {
|
||||||
|
case 0:
|
||||||
|
dictionary.clear();
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
if (word.empty()) {
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>!\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (dictionary.insert(word))
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> \"" << word << "\" <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
|
||||||
|
else
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> \"" << word << "\" <20><><EFBFBD> <20><><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
if (word.empty()) {
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>!\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (dictionary.remove(word))
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> \"" << word << "\" <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
|
||||||
|
else
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> \"" << word << "\" <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
|
||||||
|
break;
|
||||||
|
case 3: {
|
||||||
|
vector<string> allWords = dictionary.getAllWords();
|
||||||
|
if (allWords.empty()) {
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < (int)allWords.size(); i++) {
|
||||||
|
cout << allWords[i];
|
||||||
|
if (i + 1 < (int)allWords.size()) cout << ", ";
|
||||||
|
}
|
||||||
|
cout << "\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 4:
|
||||||
|
return 0;
|
||||||
|
default:
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>!\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user