-
Notifications
You must be signed in to change notification settings - Fork 0
Контрольная работа № 1 (142). Разгуляева А.И. #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,33 @@ | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| #define MAX_SIZE 100 | ||
|
|
||
|
|
||
| int main(void) | ||
| { | ||
| int sizeNumbers = 6; | ||
| int numbers[6] = {10,20,30,49,50,60}; | ||
|
|
||
| int maxSum = 0; | ||
| int maxSumInd[6] = {0}; | ||
| for (int i = 0; i < sizeNumbers; i++) { | ||
| int sum = (numbers[i] / 10) + (numbers[i] - ((numbers[i] / 10) * 10)); | ||
| //printf("%d\n", sum); | ||
| if (sum > maxSum) { | ||
| maxSum = sum; | ||
| for (int j = 0; j < sizeNumbers ;j++) | ||
| maxSumInd[j] = 0; | ||
| maxSumInd[i] = 1; | ||
|
Comment on lines
+19
to
+21
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. Вот в этом месте получается квадратичная сложность. А можно минимальными изменениями добиться линейной. |
||
| } else if (sum == maxSum) { | ||
| maxSumInd[i] = 1; | ||
| } | ||
| } | ||
| for (int i = 0; i < sizeNumbers; i++) { | ||
| if (maxSumInd[i] == 1) { | ||
| printf("%d\n", numbers[i]); | ||
| return 0; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,40 @@ | ||||||
| #include <string.h> | ||||||
| #include <stdio.h> | ||||||
| #define MAX_SIZE 100 | ||||||
|
|
||||||
| int isPal(int num) { | ||||||
| char binNum[MAX_SIZE]; | ||||||
| int count = 0; | ||||||
| while (num > 0) { | ||||||
| if (num % 2 == 1) { | ||||||
| num = num / 2; | ||||||
| binNum[count] = 1; | ||||||
| } else { | ||||||
| num = num / 2; | ||||||
| binNum[count] = 0; | ||||||
| } | ||||||
|
Comment on lines
+9
to
+15
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. Это можно записать в две строчки. |
||||||
|
|
||||||
| //printf("%d", binNum[count]); | ||||||
| count = count + 1; | ||||||
|
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
|
||||||
| } | ||||||
|
|
||||||
| for (int i = 0; i <= count; i++) { | ||||||
| if (binNum[i] == binNum[count - i]) | ||||||
|
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. Здесь напутаны индексы, так что программа работает неверно. Более того, здесь гарантированно происходит обращение за границами массива. |
||||||
| return 0; | ||||||
| } | ||||||
|
|
||||||
| return 1; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| int main(void) | ||||||
| { | ||||||
| int n; | ||||||
| scanf("%d", &n); | ||||||
| for (int i = 1; i <= n; i++) { | ||||||
| int result = isPal(i); | ||||||
| if (result == 1) | ||||||
| printf("%d\n", i); | ||||||
|
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. Я это запустил, и оно не выводит и половины всех палиндромов. Например, для |
||||||
| } | ||||||
| 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.
А что если в массиве есть трёхзначные числа?
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.
И ещё: для второй части этого выражения есть отдельный оператор. Подумайте, какая это арифметическая операция.