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
33 changes: 33 additions & 0 deletions src/kr/kr_1-1_sum.c
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));
Copy link
Collaborator

Choose a reason for hiding this comment

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

А что если в массиве есть трёхзначные числа?

Copy link
Collaborator

Choose a reason for hiding this comment

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

И ещё: для второй части этого выражения есть отдельный оператор. Подумайте, какая это арифметическая операция.

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

Choose a reason for hiding this comment

The 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;
}
40 changes: 40 additions & 0 deletions src/kr/kr_1-3_palindrom.c
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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Это можно записать в две строчки.


//printf("%d", binNum[count]);
count = count + 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
count = count + 1;
++count;

}

for (int i = 0; i <= count; i++) {
if (binNum[i] == binNum[count - i])
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Я это запустил, и оно не выводит и половины всех палиндромов.

Например, для 15 выводится 1 5, хотя там ещё есть 3, 7, 9 и 15.

}
return 0;
}