Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int CalculateThisShit(int* array, size_t count, int N) {
- // Условие уебанское и мне не понятно: нумерация элементов последовательности с единицы или с нуля?
- // Вообще у нормальных ребят всегда нумерация с нуля
- // А у курильщиков -- встречается и с единицы
- // Еще ничего не сказано про то, могут ли быть l и k равны, поэтому будем считать, что могут
- // Будем решать так: сначала рассмотрим некоторую индикатор-функцию, равную 1, если элемент
- // последовательности представим нужным образом или нулю, если не представим.
- // Перемножи индикатор-функцию на массив, и затем посчитаем сумму элементов в массиве.
- // P.S -- я не курильщик, но буду считать с единицы, потому что чуйка подсказывает, что они того и хотели
- // Хотя в неравенстве на l и k, они сами себе противоречат
- // пох крч
- for (size_t i = 1; i < count; ++i) {
- int indicator = 0;
- for (size_t k = i - 1; k <= i; ++k) {
- for (size_t l = i - 1; l <= i; ++i) {
- if (array[i] == array[k] * array[l] || array[i] * array[l] == array[k]) {
- indicator = 1;
- }
- }
- }
- array[i] *= indicator;
- }
- int summ = 0;
- for (int i = 1; i < count; ++i) {
- summ += array[i];
- }
- return summ;
- }
- int main(int argc, int* argv[]) {
- /* Open file */
- int fd = open("data.dat", O_RDONLY);
- if (fd == -1) {
- exit(-1);
- }
- /* Calculate size */
- size_t count = 0;
- int buff;
- while(read(fd, &buff, sizeof(buff))) {
- count++;
- }
- if (count <= 1) {
- exit(-1);
- }
- /* Go to the beginning of file */
- lseek(fd, 0, SEEK_SET);
- int* array = malloc((count - 1) * sizeof(int));
- size_t idx = 0;
- read(fd, &buff, sizeof(buff));
- int N = buff;
- if (N < 1) {
- perror("on ");
- exit(-1);
- }
- while(read(fd, &buff, sizeof(buff))) {
- array[idx] = buff;
- }
- int summ = CalculateThisShit(array, count, N);
- int output = open("data.res", O_RDWR | O_CREAT);
- if (output == -1) {
- perror("on output");
- exit(-1);
- }
- dprintf(output, "%d", summ);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement