-
Notifications
You must be signed in to change notification settings - Fork 0
15. Переписывание контрольных № 1 (143) #4
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,98 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| void merge(int* arr, int left, int middle, int right) | ||
| { | ||
| int i, j, k; | ||
| int n = middle - left + 1; | ||
| int m = right - middle; | ||
| int* leftArr = malloc(n * sizeof(int)); | ||
| int* rightArr = malloc(m * sizeof(int)); | ||
|
|
||
| for (i = 0; i < n; i++) | ||
| leftArr[i] = arr[left + i]; | ||
|
|
||
| for (j = 0; j < m; j++) | ||
| rightArr[j] = arr[middle + 1 + j]; | ||
|
|
||
| i = 0; | ||
| j = 0; | ||
| k = left; | ||
|
|
||
| while (i < n && j < m) { | ||
| if (leftArr[i] <= rightArr[j]) { | ||
| arr[k] = leftArr[i]; | ||
| i++; | ||
| } | ||
| else { | ||
| arr[k] = rightArr[j]; | ||
| j++; | ||
| } | ||
| k++; | ||
| } | ||
|
|
||
| while (i < n) { | ||
| arr[k] = leftArr[i]; | ||
| i++; | ||
| k++; | ||
| } | ||
|
|
||
| while (j < m) { | ||
| arr[k] = rightArr[j]; | ||
| j++; | ||
| k++; | ||
| } | ||
|
|
||
| free(leftArr); | ||
| free(rightArr); | ||
| } | ||
|
|
||
| void mergeSort(int* arr, int left, int right) | ||
| { | ||
| if (left < right) { | ||
| int middle = left + (right - left) / 2; | ||
| mergeSort(arr, left, middle); | ||
| mergeSort(arr, middle + 1, right); | ||
| merge(arr, left, middle, right); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| int* removeDuplicates(int* arr, int* arraySize) | ||
| { | ||
| if (*arraySize <= 0) | ||
| return arr; | ||
|
|
||
| int j = 0; | ||
| for (int i = 0; i < *arraySize - 1; i++) { | ||
| if (arr[i] != arr[i + 1]) | ||
| arr[j++] = arr[i]; | ||
| } | ||
| arr[j++] = arr[*arraySize - 1]; | ||
|
|
||
| *arraySize = j; | ||
| return realloc(arr, *arraySize * sizeof(int)); | ||
| } | ||
|
|
||
| int main(void) | ||
| { | ||
| int* array = malloc(1 * sizeof(int)); | ||
| int number = 999; | ||
| int count = 0; | ||
|
|
||
| while (number != 0) { | ||
| scanf("%d", &number); | ||
| count++; | ||
| array = realloc(array, count * sizeof(int)); | ||
|
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. А почистить память? |
||
| array[count - 1] = number; | ||
| } | ||
|
|
||
| mergeSort(array, 0, count - 1); | ||
| array = removeDuplicates(array, &count); | ||
|
Comment on lines
+90
to
+91
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. А от Вас просили другого? |
||
|
|
||
| for (int i = 1; i < count; i++) | ||
| printf("%d ", array[i]); | ||
|
|
||
| printf("\n"); | ||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| #include <stdio.h> | ||
| #include <stdbool.h> | ||
|
|
||
| long int minimizeNumber(long int n) | ||
| { | ||
| if (n <= 0) { | ||
| return 0; | ||
| } | ||
|
Comment on lines
+6
to
+8
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 cnt[10] = {0}; | ||
|
|
||
| while (n > 0) { | ||
| int d = n % 10; | ||
| cnt[d]++; | ||
| n /= 10; | ||
| } | ||
|
|
||
| long int result = 0; | ||
|
|
||
| for (int d = 1; d <= 9; d++) { | ||
| if (cnt[d] > 0) { | ||
| result = d; | ||
| cnt[d]--; | ||
| break; | ||
| } | ||
| } | ||
|
Comment on lines
+20
to
+26
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. Ну, нет, всё-таки нули стоило откинуть |
||
|
|
||
| for (int d = 0; d <= 9; d++) { | ||
| while (cnt[d] > 0) { | ||
| result = result * 10 + d; | ||
| cnt[d]--; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| bool testZero() | ||
| { | ||
| return minimizeNumber(0) == 0; | ||
| } | ||
|
|
||
| bool testWithZero() | ||
| { | ||
| return minimizeNumber(1001) == 1001; | ||
| } | ||
|
|
||
| bool testAllDigits() | ||
| { | ||
| return minimizeNumber(8967450123L) == 1023456789L; | ||
| } | ||
|
|
||
| int main(void) | ||
| { | ||
| if (!testZero() || !testWithZero() || !testAllDigits()) | ||
| return 1; | ||
| return 0; | ||
| } | ||
|
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. Тестов нет |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,49 @@ | ||||||
| #include <stdlib.h> | ||||||
|
|
||||||
| typedef struct listItem { | ||||||
|
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. По стайлгайду
Suggested change
|
||||||
| struct listItem* before; | ||||||
| int value; | ||||||
| int index; | ||||||
| } | ||||||
|
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. И по-моему без окончания строки оно не скомпилируется |
||||||
|
|
||||||
| listItem create() | ||||||
| { | ||||||
| listItem* item = (*listItem)malloc(1 * sizeif(listItem)); | ||||||
| item->value = 0; | ||||||
| item->before = NULL; | ||||||
| item->index = 0; | ||||||
| return item; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| listItem* insert(listItem* list, newValue* list, int where) | ||||||
| { | ||||||
| if where > list->index { | ||||||
| listItem* correctItem; | ||||||
| correctItem->index = list->index + 1 | ||||||
| correctItem->before = list | ||||||
| } else { | ||||||
| listItem cursor = *list; | ||||||
| while (cursor->index != where) | ||||||
| cursor = cursor->before; | ||||||
| listItem* correctItem = *cursor; | ||||||
|
|
||||||
| correctItem->value = *newValue; | ||||||
| } | ||||||
| } | ||||||
|
Comment on lines
+32
to
+33
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. Здесь скобочек многовато |
||||||
|
|
||||||
| listItem* revert(listItem* list) | ||||||
| { | ||||||
| int lenght = list->index + 1; | ||||||
| listItem after = NULL; | ||||||
|
|
||||||
| while (list->index != 0) { | ||||||
| list->after = before; | ||||||
| list->index = lenght - list->index; | ||||||
| after = list; | ||||||
| list = list->before; | ||||||
| } | ||||||
|
|
||||||
| return list; | ||||||
| } | ||||||
|
|
||||||
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.
Довольно странный инициализатор, но ладно