-
Notifications
You must be signed in to change notification settings - Fork 0
добавила решение контрольной работы Иванова Алина #7
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
Open
ialina07
wants to merge
1
commit into
main
Choose a base branch
from
kr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
| #define MAX_SIZE 100 | ||
|
|
||
| int sumNumbers(int a); | ||
|
|
||
| int main() | ||
| { | ||
| int m[MAX_SIZE]; | ||
| int n; | ||
| printf("Введите длину массива: "); | ||
| scanf("%d", &n); | ||
|
|
||
| if (n <= 0 || n > MAX_SIZE) { | ||
| printf("Неверный размер массива!\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| printf("Введите %d элементов\n", n); | ||
| for (int i = 0; i < n; i++) { | ||
| printf("Элемент %d: ", i + 1); | ||
| scanf("%d", &m[i]); | ||
| } | ||
|
|
||
| int mx = -1; // Максимальная сумма цифр | ||
| int ch = m[0]; // Число с максимальной суммой цифр | ||
|
|
||
| for (int i = 0; i < n; i++) { | ||
| int currentSum = sumNumbers(m[i]); | ||
| if (currentSum > mx) { | ||
| mx = currentSum; | ||
| ch = m[i]; | ||
| } | ||
| } | ||
|
|
||
| printf("Число с максимальной суммой цифр: %d (сумма цифр: %d)\n", ch, mx); | ||
| return 0; | ||
| } | ||
|
|
||
| int sumNumbers(int a) | ||
| { | ||
| int res = 0; | ||
| char s[20]; | ||
|
|
||
| // Работаем с абсолютным значением числа | ||
| int n = a < 0 ? -a : a; | ||
|
Comment on lines
+45
to
+46
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. Для этого в стандартной библиотеке есть функция |
||
|
|
||
| // Преобразуем число в строку | ||
|
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. Не самый эффективный подход, хоть и довольно естественный. |
||
| sprintf(s, "%d", n); | ||
| int len = strlen(s); | ||
|
|
||
| // Суммируем цифры | ||
| for (int i = 0; i < len; i++) { | ||
| res = res + (s[i] - '0'); | ||
| } | ||
|
|
||
| return res; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
|
|
||
| bool isBinaryPalindrome(int n) | ||
| { | ||
| int highBit = 0; | ||
| int temp = n; | ||
|
|
||
| while (temp > 0) { | ||
| highBit++; | ||
| temp >>= 1; | ||
| } | ||
|
|
||
| for (int i = 0; i < highBit / 2; i++) { | ||
| int leftBit = (n >> (highBit - 1 - i)) & 1; | ||
| int rightBit = (n >> i) & 1; | ||
| if (leftBit != rightBit) | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
| int n; | ||
|
|
||
| printf("Введите n: "); | ||
| scanf("%d", &n); | ||
|
|
||
| printf("Палиндромы [1; %d]: ", n); | ||
|
|
||
| for (int i = 1; i <= n; i++) { | ||
| if (isBinaryPalindrome(i)) { | ||
| printf("%d ", i); | ||
| } | ||
| } | ||
|
|
||
| printf("\n"); | ||
| return 0; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Ваша программа выдаёт неправильный ответ, например, на массиве
47 14 56 20. Прочитайте внимательно задание.