Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | None | 0 0
  1. #include <fcntl.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. int CalculateThisShit(int* array, size_t count, int N) {
  8.     // Условие уебанское и мне не понятно: нумерация элементов последовательности с единицы или с нуля?
  9.     // Вообще у нормальных ребят всегда нумерация с нуля
  10.     // А у курильщиков -- встречается и с единицы
  11.     // Еще ничего не сказано про то, могут ли быть l и k равны, поэтому будем считать, что могут
  12.  
  13.  
  14.     // Будем решать так: сначала рассмотрим некоторую индикатор-функцию, равную 1, если элемент
  15.     // последовательности представим нужным образом или нулю, если не представим.
  16.  
  17.     // Перемножи индикатор-функцию на массив, и затем посчитаем сумму элементов в массиве.
  18.  
  19.    
  20.     // P.S -- я не курильщик, но буду считать с единицы, потому что чуйка подсказывает, что они того и хотели
  21.     // Хотя в неравенстве на l и k, они сами себе противоречат
  22.     // пох крч
  23.  
  24.  
  25.     for (size_t i = 1; i < count; ++i) {
  26.         int indicator = 0;
  27.         for (size_t k = i - 1; k <= i; ++k) {
  28.             for (size_t l = i - 1; l <= i; ++i) {
  29.                 if (array[i] == array[k] * array[l] || array[i] * array[l] == array[k]) {
  30.                     indicator = 1;
  31.                 }
  32.             }
  33.         }
  34.         array[i] *= indicator;
  35.     }
  36.  
  37.     int summ = 0;
  38.     for (int i = 1; i < count; ++i) {
  39.         summ += array[i];
  40.     }
  41.  
  42.     return summ;
  43. }
  44.  
  45.  
  46. int main(int argc, int* argv[]) {
  47.     /* Open file */
  48.     int fd = open("data.dat", O_RDONLY);
  49.  
  50.     if (fd == -1) {
  51.         exit(-1);
  52.     }
  53.  
  54.  
  55.     /*  Calculate size  */
  56.     size_t count = 0;
  57.     int buff;
  58.     while(read(fd, &buff, sizeof(buff))) {
  59.         count++;
  60.     }
  61.  
  62.     if (count <= 1) {
  63.         exit(-1);
  64.     }
  65.  
  66.  
  67.  
  68.  
  69.     /*  Go to the beginning of file  */
  70.     lseek(fd, 0, SEEK_SET);
  71.  
  72.     int* array = malloc((count - 1) * sizeof(int));
  73.  
  74.     size_t idx = 0;
  75.     read(fd, &buff, sizeof(buff));
  76.     int N = buff;
  77.  
  78.     if (N < 1) {
  79.         perror("on ");
  80.         exit(-1);
  81.     }
  82.  
  83.     while(read(fd, &buff, sizeof(buff))) {
  84.         array[idx] = buff;
  85.     }
  86.  
  87.     int summ = CalculateThisShit(array, count, N);
  88.  
  89.     int output = open("data.res", O_RDWR | O_CREAT);
  90.  
  91.     if (output == -1) {
  92.         perror("on output");
  93.         exit(-1);
  94.     }
  95.  
  96.     dprintf(output, "%d", summ);
  97.  
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement