Skip to content
Open

Kr #2

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
25 changes: 25 additions & 0 deletions src/hw_Optimal_Sort/main.c

This comment was marked as resolved.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdio.h>

// Объявление функции
extern int sort_array(int array[], int count);

// Основной код - объявление массива на 100 чисел, создание счетчика, чтобы знать со сколькими числами надо будет работать, чтение чисел через for с scanf и вывод
int main() {
int array[100];
int count = 0;

int i = 0;
while (i < 100 && scanf("%d", &array[i]) == 1) {
i++;
count++;
}

int moved = sort_array(array, count);

printf("Отсортированный массив:");
for (int i = 0; i < count; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
16 changes: 16 additions & 0 deletions src/hw_Optimal_Sort/sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
int sort_array(int* array, int count) {
int moved_count = 0;
// сортировка пузырьком
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
moved_count++;
}
}
}

return moved_count;
}
67 changes: 67 additions & 0 deletions src/kr/diffPalindrome.c

Choose a reason for hiding this comment

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

Вы не добавили решение этой задачи на HWProj, так что поставить оценку не получится

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// Created by sasha on 17.10.2025.
//

//Напечатать на экран все из диапазона [1;n],
//которые являются палиндромами в двоичной записи. Выводить в десятичной системе счисления.

#include <stdio.h>
#include <string.h>

int systemChangerTo2(int n) {
int binaryNum[1000]; //Массив для числа во вторичной
char charBinaryNum[1000]; // Массив для числа во вторичной, но в виде строки

Choose a reason for hiding this comment

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

Зачем нужно два представления? Вполне хватит какого-то одного.

int i = 0;
int len = 0;

while (n > 0) {
binaryNum[i] = n % 2;
n = n / 2;
i++;
len = i; // Длина массива
}

for (i = 0; i < len; i++) {
charBinaryNum[i] = binaryNum[i] + '0';
}

int left = 0;
int right = strlen(charBinaryNum) - 1;

while (left <= right) {
if (charBinaryNum[left] == charBinaryNum[right]) {
left++;
right--;
} else {
return 0;
}
if (left > right) {

Choose a reason for hiding this comment

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

Это условие уже проверяется в заголовке цикла. Второй раз его проверять не нужно.

return 1;
}
}
}




int main(void) {
int n = 0;

printf("Введите n \n");
scanf("%d",&n);

int arr[n];
int num = 0;

for (int i = 0; i < n; i++) {
arr[i] = num;
num++;

if (systemChangerTo2(num) == 1) {
printf("%d\n", num);
}
}


return 0;
}
49 changes: 49 additions & 0 deletions src/kr/max.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// В массиве целых чисел найти число, сумма цифр которого была бы наибольшей.
// Если таких чисел несколько, вывести на экран все эти числа.
//

#include <stdio.h>

// Функция нахождения максимальной суммы
int finder(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
int tempSum = 0; // Временная сумма для каждого отдельно взятого числа
int temp = arr[i];

while (temp / 10 != 0) { // "Резатель числа", режем - пока оно не станет нулевым, прибавляем хвостик к сумме
tempSum += (temp % 10);
temp = temp/10;
printf("Sum %d\n", tempSum);
printf("%d \n", temp);
}
tempSum = temp + tempSum;
printf("Sum %d\n", tempSum);
Comment on lines +15 to +22

Choose a reason for hiding this comment

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

  1. Ваша программа выводит ответ в stdin, так что отладочный вывод лучше убрать.
  2. Этот код можно упростить, если использовать более естественное условие в цикле.


if (tempSum > sum) {
sum = tempSum;
}
}
return sum;
}

int main(void) {
int size = 0;

printf("Введите колво чисел в массиве (len) \n");
scanf("%d",&size);

int arr[size];

printf("Введите сами числа в массив \n");
for (int i = 0; i < size; i++) {
scanf("%d",&arr[i]);
}

int maxSum = finder(arr, size);

printf("\n");
printf("Max sum of numbers %d", maxSum);

Choose a reason for hiding this comment

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

Выводится вообще не то, что нужно по условию.

return 0;
}