init commit

This commit is contained in:
2025-04-13 21:48:15 +03:00
commit 23255e9121
192 changed files with 12200 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
#include <stdio.h>
#include <stdlib.h>
enum
{
NUM_OF_DIGITS = '9' - '0' + 1,
NUM_OF_LETTERS = 'z' - 'a' + 1,
BIT_MASK_1 = 0b111011,
BIT_MASK_2 = 0b001000,
MAX_NUM = 64,
};
int
main(void)
{
int c;
while ((c = getchar()) != EOF) {
int code = 0;
if (c <= '9' && c >= '0') {
code = c - '0' + 1;
} else if (c <= 'z' && c >= 'a') {
code = c - 'a' + NUM_OF_DIGITS + 1;
} else if (c <= 'Z' && c >= 'A') {
code = c - 'A' + NUM_OF_DIGITS + NUM_OF_LETTERS + 1;
}
if (code == 0) {
continue;
}
code = code & BIT_MASK_1;
code ^= BIT_MASK_2;
int res = 0;
if (code <= NUM_OF_DIGITS && code >= 1) {
res = code - 1 + '0';
} else if (code <= NUM_OF_DIGITS + NUM_OF_LETTERS && code >= NUM_OF_DIGITS + 1) {
res = code + 'a' - NUM_OF_DIGITS - 1;
} else if (code <= NUM_OF_DIGITS + NUM_OF_LETTERS * 2 && code >= NUM_OF_DIGITS + NUM_OF_LETTERS + 1) {
res = code + 'A' - NUM_OF_DIGITS - NUM_OF_LETTERS - 1;
} else if (code == 0) {
res = '@';
} else if (code == MAX_NUM - 1) {
res = '#';
}
putchar(res);
}
return 0;
}
+80
View File
@@ -0,0 +1,80 @@
#include <stdio.h>
#include <stdlib.h>
enum
{
MAX_N = 2000
};
int
is_prime(int num)
{
if (num <= 1) {
return 0;
}
if (num <= 3) {
return 1;
}
if (num % 2 == 0 || num % 3 == 0) {
return 0;
}
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) {
return 0;
}
}
return 1;
}
int
main(void)
{
int n;
if (scanf("%d", &n) != 1) {
fprintf(stderr, "Error reading input\n");
return 1;
}
if (n >= MAX_N || !is_prime(n)) {
return 1;
}
// двумерный под хранение результата. Не может быть одномерным, т.к. заполняются столбцы по порядку
int **mas = (int **) malloc(n * sizeof(mas));
if (mas == NULL) {
return 1;
}
for (int i = 0; i < n; i++) {
mas[i] = (int *) malloc(n * sizeof(*mas));
if (mas[i] == NULL) {
for (int j = 0; j < i; j++) {
free(mas[j]);
}
free(mas);
return 1;
}
}
for (int i = 1; i < n; i++) {
for (int a = 0; a < n; a++) {
mas[i][(a * i) % n] = a;
}
}
for (int j = 0; j < n; j++) {
for (int i = 1; i < n; i++) {
printf("%d ", mas[i][j]);
}
printf("\n");
}
for (int i = 0; i < n; i++) {
free(mas[i]);
}
free(mas);
return 0;
}
+78
View File
@@ -0,0 +1,78 @@
#include <stdio.h>
#include <stdlib.h>
void
swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
return;
}
int
next_permutation(int *arr, int n)
{
int i = n - 2;
while (i >= 0 && arr[i] >= arr[i + 1]) {
i--;
}
if (i < 0) {
return 0;
}
int j = n - 1;
while (arr[j] <= arr[i]) {
j--;
}
swap(&arr[i], &arr[j]);
int pt1 = i + 1, pt2 = n - 1;
while (pt1 < pt2) {
swap(&arr[pt1++], &arr[pt2--]);
}
return 1;
}
int
main()
{
int n;
if (scanf("%d", &n) != 1) {
fprintf(stderr, "Error reading input\n");
return 1;
}
if (n <= 0 || n >= 10) {
return 1;
}
int *arr = (int *) malloc(n * sizeof(*arr));
if (arr == NULL) {
return 1;
}
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
}
do {
for (int i = 0; i < n; i++) {
printf("%d", arr[i]);
}
printf("\n");
} while (next_permutation(arr, n));
free(arr);
return 0;
}
+51
View File
@@ -0,0 +1,51 @@
#include <stdlib.h>
#include <malloc.h>
#include <stdio.h>
int
cmp(const void *a, const void *b)
{
if (*(int *) a % 2 == 0) {
if (*(int *) b % 2 == 1) {
return -1;
}
if (*(int *) a > *(int *) b) {
return 1;
}
if (*(int *) a < *(int *) b) {
return -1;
}
return 0;
}
if (*(int *) b % 2 == 0) {
return 1;
}
if (*(int *) a > *(int *) b) {
return -1;
}
if (*(int *) a < *(int *) b) {
return 1;
}
return 0;
}
void
sort_even_odd(size_t count, int *data)
{
qsort(data, count, sizeof(int), cmp);
}
int
main()
{
int a[] = {4, 2, 5, 3, 1, 0, -4, -2, -68, 23, 3};
sort_even_odd(sizeof(a) / sizeof(a[0]), a);
for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}