Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/kr1/n1.c
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ваша программа выдаёт неправильный ответ, например, на массиве 47 14 56 20. Прочитайте внимательно задание.

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Для этого в стандартной библиотеке есть функция abs.


// Преобразуем число в строку
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
}
41 changes: 41 additions & 0 deletions src/kr1/n3.c
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;
}