-
Notifications
You must be signed in to change notification settings - Fork 0
Контрольная 2. Разгуляева Ада #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| #include "kr_2_1_palindrom.h" | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| // структура узла списка | ||
| typedef struct Node { | ||
| int data; | ||
| struct Node* next; | ||
| } Node; | ||
|
|
||
| // функция для создания нового узла | ||
| Node* createNode(int value) | ||
| { | ||
| Node* newNode = (Node*)malloc(sizeof(Node)); | ||
| newNode->data = value; | ||
| newNode->next = NULL; | ||
| return newNode; | ||
| } | ||
|
|
||
| // функция для добавления элемента | ||
| Node* append(Node* head, int value) | ||
| { | ||
| Node* newNode = createNode(value); | ||
|
|
||
| if (head == NULL) | ||
| return newNode; | ||
|
|
||
| Node* current = head; | ||
| while (current->next != NULL) { | ||
| current = current->next; | ||
| } | ||
| current->next = newNode; | ||
|
|
||
| return head; | ||
| } | ||
|
|
||
| // функция разворота списка | ||
| Node* reverseList(Node* head) | ||
| { | ||
| Node* newHead = NULL; | ||
| Node* current = head; | ||
| Node* next = NULL; | ||
|
|
||
| while (current != NULL) { | ||
| next = current->next; | ||
| current->next = newHead; | ||
| newHead = current; | ||
| current = next; | ||
| } | ||
|
|
||
| return newHead; | ||
| } | ||
|
|
||
| // функция для сравнения двух списков | ||
| bool compareLists(Node* list1, Node* list2) | ||
| { | ||
| Node* curr1 = list1; | ||
| Node* curr2 = list2; | ||
| while (curr1 != NULL && curr2 != NULL) { | ||
| if (curr1->data != curr2->data) { | ||
| return false; | ||
| } | ||
| curr1 = curr1->next; | ||
| curr2 = curr2->next; | ||
| } | ||
| return (curr1 == NULL && curr2 == NULL); | ||
| } | ||
|
|
||
| // функция для создания копии списка | ||
| Node* copyList(Node* head) | ||
| { | ||
| if (head == NULL) | ||
| return NULL; | ||
| Node* newHead = createNode(head->data); | ||
| Node* currentOrig = head->next; | ||
| Node* currentCopy = newHead; | ||
|
|
||
| while (currentOrig != NULL) { | ||
| currentCopy->next = createNode(currentOrig->data); | ||
| currentCopy = currentCopy->next; | ||
| currentOrig = currentOrig->next; | ||
| } | ||
| return newHead; | ||
| } | ||
|
|
||
| // функция для проверки симметричности списка | ||
| bool isSymmetric(Node* head) | ||
| { | ||
| if (head == NULL || head->next == NULL) | ||
| return true; | ||
|
|
||
| Node* copy = copyList(head); | ||
| copy = reverseList(copy); | ||
| bool result = compareLists(head, copy); | ||
| Node* temp = copy; | ||
| while (temp != NULL) { | ||
| Node* next = temp->next; | ||
| free(temp); | ||
| temp = next; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| // функция для освобождения памяти | ||
| void freeList(Node* head) | ||
| { | ||
| Node* current = head; | ||
| while (current != NULL) { | ||
| Node* temp = current; | ||
| current = current->next; | ||
| free(temp); | ||
| } | ||
| } | ||
|
|
||
| // функция создания списка из массива для тестов | ||
| Node* createListFromArray(int arr[], int n) | ||
| { | ||
| Node* head = NULL; | ||
| for (int i = 0; i < n; i++) { | ||
| head = append(head, arr[i]); | ||
| } | ||
| return head; | ||
| } | ||
|
|
||
| // тест с несимметричным списком | ||
| void test1() | ||
| { | ||
| int arr[] = { 10, 20, 30, 40, 20, 10 }; | ||
| Node* list = createListFromArray(arr, 6); | ||
| printf("Ожидается: не симметричный\n"); | ||
| printf("Результат: %s\n\n", isSymmetric(list) ? "симметричный ✗" : "не симметричный ✓"); | ||
|
|
||
| freeList(list); | ||
| } | ||
|
|
||
| // тест со списком из одного элемента | ||
| void test2() | ||
| { | ||
| int arr[] = { 42 }; | ||
| Node* list = createListFromArray(arr, 1); | ||
| printf("Ожидается: симметричный\n"); | ||
| printf("Результат: %s\n\n", isSymmetric(list) ? "симметричный ✓" : "не симметричный ✗"); | ||
|
|
||
| freeList(list); | ||
| } | ||
|
|
||
| // тест с пустым списком | ||
| void test3() | ||
| { | ||
| Node* list = NULL; | ||
| printf("Ожидается: симметричный\n"); | ||
| printf("Результат: %s\n\n", isSymmetric(list) ? "симметричный ✓" : "не симметричный ✗"); | ||
|
|
||
| freeList(list); | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
| Node* head = NULL; | ||
| int number; | ||
|
|
||
| printf("Введите натуральные числа (0 для окончания ввода): \n"); | ||
|
|
||
| while (scanf("%d", &number) == 1 && number > 0) { | ||
| head = append(head, number); | ||
| } | ||
|
|
||
| // проверяем симметричность | ||
| if (isSymmetric(head)) { | ||
| printf("Список симметричен.\n"); | ||
| } else { | ||
| printf("Список не симметричен.\n"); | ||
| } | ||
|
|
||
| // освобождаем память | ||
| freeList(head); | ||
|
|
||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #ifndef PALINDROM_H | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. По стайлгайду мы не используем include guards. |
||
| #define PALINDROM_H | ||
|
|
||
| // структура узла списка | ||
| typedef struct Node { | ||
| int data; | ||
| struct Node* next; | ||
| } Node; | ||
|
|
||
| // функция для создания нового узла | ||
| Node* createNode(int value); | ||
|
|
||
| // функция для добавления элемента | ||
| Node* append(Node* head, int value); | ||
|
|
||
| // функция разворота списка | ||
| Node* reverseList(Node* head); | ||
|
|
||
| // функция для сравнения двух списков | ||
| bool compareLists(Node* list1, Node* list2); | ||
|
|
||
| // функция для проверки симметричности списка | ||
| bool isSymmetric(Node* head); | ||
|
|
||
| // функция для освобождения памяти | ||
| void freeList(Node* head); | ||
|
|
||
| // функция создания списка из массива для тестов | ||
| Node* createListFromArray(int arr[], int n); | ||
|
|
||
| // тест с несимметричным списком | ||
| void test1(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тесты лучше выделить в отдельный файл. |
||
|
|
||
| // тест со списком из одного элемента | ||
| void test2(); | ||
|
|
||
| // тест с пустым списком | ||
| void test3(); | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #include <stdio.h> | ||
|
|
||
| int main() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше не писать всё в |
||
| { | ||
| int N, bits[32]; | ||
| scanf("%d", &N); | ||
| for (int i = 0; i < N; i++) | ||
| scanf("%d", &bits[i]); | ||
|
|
||
| int max_val = 0; | ||
|
|
||
| // перебираем все начальные позиции для сдвига | ||
| for (int start = 0; start < N; start++) { | ||
| int current = 0; | ||
|
|
||
| // собираем число с позиции start | ||
| for (int i = 0; i < N; i++) { | ||
| int idx = (start + i) % N; | ||
| current = (current << 1) | bits[idx]; // добавляем бит | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +2 балла за использование битовых операций (+5 я ставил только если циклический сдвиг тоже сделать на битовых операциях) |
||
| } | ||
|
|
||
| if (current > max_val) { | ||
| max_val = current; | ||
| } | ||
| } | ||
|
|
||
| printf("%d\n", max_val); | ||
| return 0; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Этот
whileимеет квадратичную сложность, хотя сам алгоритм линейный. А можно сделать это заO(n), если где-то сохранить указатель на последний элемент списка.