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
53 changes: 53 additions & 0 deletions kr1/1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <stdio.h>

void to2(char string[], int num);
int palindrom(char string[]);
int sizeOfString(char string[]);

void main() {
int n = 1;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
char string[200] = {' '};
to2(string, i);

int res = palindrom(string);
if (res == 1) {
Comment on lines +14 to +15

Choose a reason for hiding this comment

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

Suggested change
int res = palindrom(string);
if (res == 1) {
if (palindrom(string)) {

printf("%d\n", i);
}
}
}

void to2(char string[], int num) {

Choose a reason for hiding this comment

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

Тут есть интересная попытка обмануть пользователя. Имя string намекает на то, что на выходе будет строка символов, терминированная нулём. А по факту это массив целых чисел, терминированный табуляцией.

Если Ваша задача -- повалить программу, которая будет использовать Вашу библиотеку, то у Вас точно получится.

int index = 0;
while (num > 0) {
int ost = num % 2;
num = num / 2;
string[index] = (char) (ost);

Choose a reason for hiding this comment

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

nit: обычно приведение форматируют так: (char)ost.

А здесь даже гитхаб не понял, что происходит, и отключил подсветку.

Choose a reason for hiding this comment

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

Просто на всякий случай: если скастить, например, int a = 7 к char, то он не превратится волшебным образом в '7'. Это будет '\a' (bell).

index++;
}
string[index] = '\t';

Choose a reason for hiding this comment

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

Вы что ли рандомными символами терминируете строку? Конвенционально используется \0.

}


int palindrom(char string[]) {
int res = 1;
int size = sizeOfString(string);

Choose a reason for hiding this comment

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

Если бы string действительно была строкой, можно было бы воспользоваться strlen. Вот Вам ещё одна причина правильно использовать char.

for (int i = 0; i < size; i++) {
if (string[i] != string[size-1-i]) {
res = 0;
break;
}
}

return res;
}

int sizeOfString(char string[]) {
int size = 0;
while (string[size] != '\t') {
size++;
}

return size;
}
39 changes: 39 additions & 0 deletions kr1/2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdio.h>

int getSum(int n);

void main() {
int arr[] = {2, 4, 6, 8, 10, 35};
int size = sizeof(arr) / sizeof(int);

int mx = 0;

for (int i = 0; i < size; i++) {
int sm = getSum(arr[i]);
if (sm > mx) {
mx = sm;
}
}


for (int i = 0; i < size; i++) {
int sm = getSum(arr[i]);
if (sm == mx) {
printf("%d\n", arr[i]);
}
}





Comment on lines +25 to +29

Choose a reason for hiding this comment

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

Если верить HWProj, Вы сдали задачу за три минуты до дедлайна. За это время можно было хотя бы минимально привести код в порядок.

}

int getSum(int n) {
int sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n / 10;
}
return sum;
}
30 changes: 30 additions & 0 deletions kr1/monkey.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdio.h>

int isSorted(int arr[], int size);
void swap(int arr[]);

void main() {
int arr[] = {2, 4, 6, 8, 10};
int size = sizeof(arr) / sizeof(int);
printf("%d\n", isSorted(arr, size));
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}



}

int isSorted(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
if (arr[i] > arr[i+1]) {
return 0;
}
}

return 1;
}

void swap(int arr[]) {

}