Alx09

Untitled

May 12th, 2020
1,427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. int N, V[5000], max = 0, R, sum = 0, nr;
  4. void Citire()
  5. {
  6.     int i, element;
  7.     FILE* f = fopen("in.txt", "r");
  8.     if (f == NULL)
  9.     {
  10.         printf("Eroare la deschiderea fisierului f");
  11.         exit(1);
  12.     }
  13.     fscanf(f, "%d ", &N);
  14.     for (i = 0; i < N; i++)
  15.     {
  16.         fscanf(f, "%d", &element);
  17.         V[i] = element;
  18.         if (V[i] > max)
  19.             max = V[i];
  20.         sum = sum + V[i];
  21.     }
  22.  
  23.     fclose(f);
  24. }
  25. void Afisare()
  26. {
  27.     int i;
  28.     FILE* f2 = fopen("out.txt", "w");
  29.     if (f2 == NULL)
  30.     {
  31.         fprintf(f2, "Eroare la deschiderea fisierului f2");
  32.         exit(1);
  33.     }
  34.     R = max;
  35.     while (R <= sum / 2) {
  36.         if (sum % R == 0) {
  37.             max = 0;
  38.             for (i = 0; i < N && max < R; i++) {
  39.                 max = max + V[i];
  40.                 if (max == R)
  41.                     max = 0;
  42.             }
  43.             if (i == N && max == 0) {
  44.                 fprintf(f2, "%d", R);
  45.                 return ;
  46.             }
  47.  
  48.         }
  49.         R++;
  50.  
  51.     }
  52.     fprintf(f2, "%d", max);
  53.     fclose(f2);
  54. }
  55.  
  56. int main()
  57. {
  58.     Citire();
  59.     Afisare();
  60.     return 0;
  61.     system("pause");
  62. }
Advertisement
Add Comment
Please, Sign In to add comment