From 9922fad48192e102e388d5dd4c4dc29d4ec0f0c9 Mon Sep 17 00:00:00 2001 From: nihonium Date: Mon, 7 Mar 2022 15:57:12 +0300 Subject: [PATCH 1/5] second --- data/base64.c | 2 +- data/float.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++ data/meteorit.c | 10 ++++++ data/patch.c | 16 +++++++++ data/showdouble.c | 42 ++++++++++++++++++++++++ data/sum.c | 37 +++++++++++++++++++++ data/virus.c | 70 +++++++++++++++++++++++++++++++++++++++ numbers/tolongn.c | 71 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 330 insertions(+), 1 deletion(-) create mode 100644 data/float.c create mode 100644 data/meteorit.c create mode 100644 data/patch.c create mode 100644 data/showdouble.c create mode 100644 data/sum.c create mode 100644 data/virus.c create mode 100644 numbers/tolongn.c diff --git a/data/base64.c b/data/base64.c index f8a53c4..cfb1988 100644 --- a/data/base64.c +++ b/data/base64.c @@ -1,5 +1,5 @@ #include -#define SIZE 10000000 +#define SIZE 100000000 //#define _DEBUG const char base64[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; diff --git a/data/float.c b/data/float.c new file mode 100644 index 0000000..447cc9b --- /dev/null +++ b/data/float.c @@ -0,0 +1,83 @@ +#include +#include +#include +#define SIZE 1000000 + +int euclid(int a, int b); + +int main() { + float f; + char p = 0; + static char s[SIZE] = ""; + static char x[SIZE] = "", y[SIZE] = "", z[SIZE] = ""; + int i, j; + int xi = 0, yi = 0, zi = 0, gcd = 1; + scanf("%f", &f); + sprintf(s, "%.6f", f); + + for (i = 0; (i < (int)strlen(s)) && (s[i] != '.'); ++i); + + for (j = 0; j < i; ++j) + x[j] = s[j]; + + for (j = 0; j < (int)strlen(s); ++j) { + y[j] = s[i+1+j]; + if ((y[j] != '0') && (y[j] != '\0')) + p = 1; + } + + if (p) + for (j = 0; j < (int)strlen(s) - i; ++j) + z[j] = '0'; + else { + z[0] = '1'; + z[1] = '\0'; + } + + if (j) { + z[0] = '1'; + z[j] = '\0'; + } + else { + y[0] = '0'; + y[1] = '\0'; + z[0] = '1'; + z[1] = '\0'; + } + + xi = atoi(x); + yi = atoi(y); + zi = atoi(z); + if (zi != 0) + gcd = euclid(zi, yi); + + if (gcd) { + zi = (int) zi / gcd; + yi = (int) yi / gcd; + } + if (zi == 0) + zi = 1; + printf("%d (%d/%d)", xi, yi ,zi); +} + +int euclid(int a, int b) { + if ((a == 0) || (b == 0)) + return 0; + int x = 1; + if (a < b) { + int c = a; + a = b; + b = c; + } + + while (x != 0) { + x = a % b; + a = x; + if (a < b) { + int c = a; + a = b; + b = c; + } + } + return a; +} diff --git a/data/meteorit.c b/data/meteorit.c new file mode 100644 index 0000000..51c8bc2 --- /dev/null +++ b/data/meteorit.c @@ -0,0 +1,10 @@ +union data convert(union data x) { + int i; + for (i = 0; i < 4; ++i) { + uint8_t z = x.arr[i]; + x.arr[i] = x.arr[7-i]; + x.arr[7-i] = z; + } + return x; +} + diff --git a/data/patch.c b/data/patch.c new file mode 100644 index 0000000..795ee5c --- /dev/null +++ b/data/patch.c @@ -0,0 +1,16 @@ +#ifdef _DEBUG +#include +unsigned int patch(unsigned int x); + +int main() { + unsigned int x = 1000 * 100; + unsigned int y = 2310 * 100 + 72; + unsigned int z = patch(y); + return 0; +} + +#endif + +unsigned int patch(unsigned int x) { + return x & ~(1 << 17); +} diff --git a/data/showdouble.c b/data/showdouble.c new file mode 100644 index 0000000..6eb1e09 --- /dev/null +++ b/data/showdouble.c @@ -0,0 +1,42 @@ +#include + +union u { + double d; + float f; + unsigned u; + unsigned long long l; +} uu; + +int main() { + char s[10] = ""; + scanf("%s", s); + if (s[0] == 'f') { + float a; + scanf("%f", &a); + uu.f = a; + int t = 0; + for (int i = sizeof(float) * 8 - 1; i >= 0; i--) { + printf("%d", (uu.u >> i) & 1); + t += 1; + if (t==4) { + t=0; + putchar(' '); + } + } + } else if (s[0] == 'd') { + double a; + scanf("%lf", &a); + uu.d = a; + int t = 0; + for (int i = sizeof(double) * 8 - 1; i >= 0; i--) { + printf("%lld", (uu.l >> i) & 1); + t += 1; + if (t==4) { + t=0; + putchar(' '); + } + } + } else { + printf("0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1011 1111 1111 1111 1000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000"); + } +} diff --git a/data/sum.c b/data/sum.c new file mode 100644 index 0000000..145cf3e --- /dev/null +++ b/data/sum.c @@ -0,0 +1,37 @@ +#include +#include +#define SIZE 10000000 + +int f(const void *a, const void *b); + +int main() { + static long double nums[SIZE]; + int len, i; + long double x; + long double sum = 0; + + for (len = 0; scanf("%Lf", &x) != EOF; ++len) { + nums[len] = x; + } + qsort(nums, len, sizeof(long double), f); + +#ifdef _DEBUG + for (int z = 0; z < len; ++z) + printf("%.20Lf ", nums[z]); + putchar('\n'); + printf("len:%d\n", len); +#endif + for (i = 0; i < len; ++i) + sum += nums[i]; + printf("%.20Lf", sum); + +} + +int f(const void *a, const void *b) { +#ifdef _DEBUG + printf("a = %f, b = %f\n", *(float *)a, *(float *)b); +#endif + if (*(const long double *)a > *(const long double *)b) + return 1; + return -1; +} diff --git a/data/virus.c b/data/virus.c new file mode 100644 index 0000000..8354761 --- /dev/null +++ b/data/virus.c @@ -0,0 +1,70 @@ +#include +#include +#define SIZE 12500000 + +int check_bit(char x, int pos); +int count_bits(char x, int max_offset); + +int main() { + char *robots = (char*)calloc(SIZE, sizeof(char)); + int v, N, i, j, x, s, byte_n, byte, offset; + int res = 0; + + scanf("%d %d", &v, &N); + if ((N + 1) % 8) { + byte_n = (int)((N + 1) / 8) + 1; + } + else { + byte_n = (int)((N + 1) / 8); + } + +#ifdef _DEBUG + printf("byte_n:%d, N;%d\n", byte_n, N); +#endif + for (i = 0; i < v; ++i) { + scanf("%d", &x); + byte = (int)(x / 8); + offset = x % 8; +#ifdef _DEBUG + printf("Setting first robot at byte %d[%d]\n!=!=!\n", byte, offset); +#endif + robots[byte] = robots[byte] | (128 >> offset); + for (j = 1, s = 0; x * (j + 1) + s <= N; s += j, ++j) { +#ifdef _DEBUG + printf("x*%d+%d = %d\n", j+1, s, x * (j+1) + s); +#endif + byte = (int)((x * (j + 1) + s) / 8); + offset = (x * (j + 1) + s) % 8; +#ifdef _DEBUG + printf("Setting byte %d[%d]\n", byte, offset); +#endif + robots[byte] = robots[byte] | (128 >> offset); + } + } + for (i = 0; i < byte_n - 1; ++i) { +#ifdef _DEBUG + printf("Counting bits at byte %d, found %d bits\n", i, count_bits(robots[i], 7)); +#endif + res += count_bits(robots[i], 7); + } + + offset = (N - 1) % 8; + +#ifdef _DEBUG + printf("Counting bits at byte %d, found %d bits[final offset: %d]\n", i, count_bits(robots[i], offset), offset); +#endif + res += count_bits(robots[i], offset); + printf("%d", res); +} + +int check_bit(char x, int pos) { + return (x >> (7 - pos)) & 1; +} + +int count_bits(char x, int max_offset) { + int res = 0; + for (int i = 0; i <= max_offset; ++i) { + res += check_bit(x, i); + } + return res; +} diff --git a/numbers/tolongn.c b/numbers/tolongn.c new file mode 100644 index 0000000..2bc1300 --- /dev/null +++ b/numbers/tolongn.c @@ -0,0 +1,71 @@ +#include +#include +#include + +typedef struct{ + char *dig; + int n; + char sign; +} LongN; + +LongN getLongN(char * s); +void printLong(LongN x); +int main() { + LongN a; + a.dig = (char*)calloc(101, sizeof(char)); + a.dig[0] = 12; + a.dig[1] = 34; + a.dig[2] = 5; + a.n = 5; + a.sign = 1; + //printLong(a); + char s[50]; //= "-1234"; + scanf("%s", s); + LongN x = getLongN(s); + printLong(x); + free(a.dig); + free(x.dig); + return 0; +} + +void printLong(LongN x) { + if (x.sign) { + printf("-"); + } + if (x.n % 2) { + x.n++; + } + for (int i = 0; i < (int)(x.n/2); ++i) { + printf("%d", x.dig[i]); + } + printf("\n"); +} + +LongN getLongN(char* s) { + int k = 0; + LongN r; + + r.sign = 0; + if (s[0] == '-') { + k = 1; + r.sign = 1; + } + if (s[0] == '+') + k = 1; + + int l = k; + + for (; s[l] >= '0' && s[l] <= '9'; ++l); + r.n = (l - k - 1) / 2 + 1; + r.dig = (char*)calloc(101, sizeof(char)); + + int t = 0; + for (int i = l - 1; i >= k; i -= 2, t++) { + if (i == k) { + r.dig[t] = s[i] - '0'; + } + else + r.dig[t] = 10 * (s[i - 1] - '0') + s[i] - '0'; + } + return r; +} From 92c4a7411a06e390388f945bd90abd178f7e557a Mon Sep 17 00:00:00 2001 From: nihonium Date: Mon, 7 Mar 2022 17:23:06 +0300 Subject: [PATCH 2/5] updated README.md --- README.md | 5 ++--- cpu/cpu.c | 5 +++++ cpu/notes.md | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 cpu/cpu.c create mode 100644 cpu/notes.md diff --git a/README.md b/README.md index 9ab2d8a..8268df8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ # MIPT C -Код из лекций Бабичева по алгоритмам и структурам данных. -Несколько решенных задач для контестов с Ejudge и горка всякой мелочевки. - +Решения контестов с Ejudge по архитектуре компьютера. +Публикуется, как правило, после окончания контеста. diff --git a/cpu/cpu.c b/cpu/cpu.c new file mode 100644 index 0000000..29cb34b --- /dev/null +++ b/cpu/cpu.c @@ -0,0 +1,5 @@ +#include + +int main() { + char a = 0, b = 0, c = 0, d = 0; +} diff --git a/cpu/notes.md b/cpu/notes.md new file mode 100644 index 0000000..360ae92 --- /dev/null +++ b/cpu/notes.md @@ -0,0 +1,22 @@ +# CPU + +* Беззнаковые +* По модулю 256 (8-битность) +* Switch +* Массив регистров +* #ifdef REGISTERS +Используем REGISTERS + +# Cache + +* Первая лекция Северова +* "Размазывает" справа налево +* Обнулять массив через {0}/{} +* Двумерный массив можно инициализировать последовательным списком +* Адрес массива - не lvalue +* Матрицы симметричны относительно диагонали +* У второй матрицы меняем индексы местами +* + +# Notes +* (int (*)[10])p - "приведение" к массиву длины 10 From b52d5291fd1c3c7776c74bc59e990a8a5452237d Mon Sep 17 00:00:00 2001 From: nihonium Date: Mon, 7 Mar 2022 17:43:52 +0300 Subject: [PATCH 3/5] motes --- cpu/notes.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cpu/notes.md b/cpu/notes.md index 360ae92..6135076 100644 --- a/cpu/notes.md +++ b/cpu/notes.md @@ -16,7 +16,15 @@ * Адрес массива - не lvalue * Матрицы симметричны относительно диагонали * У второй матрицы меняем индексы местами +<<<<<<< Updated upstream * +======= + +# Neo +* number/_of_/anomaly = 6 +* Переписать значение в другой части структуры +* Учитывать "дырки" в структуре, тут её нет +>>>>>>> Stashed changes # Notes * (int (*)[10])p - "приведение" к массиву длины 10 From ab668d12de96f12ea74aae682d7054f552f8fd84 Mon Sep 17 00:00:00 2001 From: nihonium Date: Mon, 7 Mar 2022 17:49:25 +0300 Subject: [PATCH 4/5] notes --- cpu/notes.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 cpu/notes.md diff --git a/cpu/notes.md b/cpu/notes.md new file mode 100644 index 0000000..ab40c2d --- /dev/null +++ b/cpu/notes.md @@ -0,0 +1,26 @@ +# CPU + +* Беззнаковые +* По модулю 256 (8-битность) +* Switch +* Массив регистров +* #ifdef REGISTERS +Используем REGISTERS + +# Cache + +* Первая лекция Северова +* "Размазывает" справа налево +* Обнулять массив через {0}/{} +* Двумерный массив можно инициализировать последовательным списком +* Адрес массива - не lvalue +* Матрицы симметричны относительно диагонали +* У второй матрицы меняем индексы местами + +# Neo +* number/_of_/anomaly = 6 +* Переписать значение в другой части структуры +* Учитывать "дырки" в структуре, тут её нет + +# Notes +* (int (*)[10])p - "приведение" к массиву длины 10 From 40cf51271fefb98a2da0b9087f24ed07f6f0fcb2 Mon Sep 17 00:00:00 2001 From: nihonium Date: Mon, 7 Mar 2022 18:47:34 +0300 Subject: [PATCH 5/5] notes --- cpu/notes.md | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/cpu/notes.md b/cpu/notes.md index ab40c2d..b190b8c 100644 --- a/cpu/notes.md +++ b/cpu/notes.md @@ -1,13 +1,14 @@ -# CPU +# cpu * Беззнаковые * По модулю 256 (8-битность) * Switch * Массив регистров * #ifdef REGISTERS + Используем REGISTERS -# Cache +# cache * Первая лекция Северова * "Размазывает" справа налево @@ -17,10 +18,40 @@ * Матрицы симметричны относительно диагонали * У второй матрицы меняем индексы местами -# Neo +# neo + * number/_of_/anomaly = 6 * Переписать значение в другой части структуры * Учитывать "дырки" в структуре, тут её нет -# Notes +# flag + +* Анализ операций +* MOV меняет флаг + +# cuda + +* Прочитать =3 + +# simple_page + +* Можно посчитать размер страницы +* Одноуровневая система адресов (в 32х-битном, на самом деле, двухуровневая) +* Остаток бит - смещение внутри страницы + +# pdp_rw + +* typedef +* Функции - однострочные +* char mem[64K] +* typedef short/int/_16t word (+stdint) +* Приводим через указатель к word: *(word *)(mem + i) + +# pdp_load + +* Текстовый файл (r/rt) + +# Заметки * (int (*)[10])p - "приведение" к массиву длины 10 +* PAE (трехстраничная память) +* Массив указателей на функции - для будущего эмулятора