Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int readarr(FILE* fin, int* data, int cnt); //считываем данные из файла в массив
- int readarr(FILE* fin, int* data, int cnt)
- {
- for (int k = 0; k < cnt; k++)
- {
- if (fscanf(fin, "%d", data + k) != 1){
- return -1;
- }
- }
- return 0;
- }
- int sol(int* arr, int n);
- int sol(int* arr, int n)
- {
- int c = 0;
- int ans = 0;
- for (int i = 0; i < n; i++)
- {
- if (arr[i] == 1)
- {
- c++;
- }
- else
- {
- c = c - 1;
- }
- if (c == 0)
- {
- ans++;
- }
- }
- return ans;
- }
- int main(void)
- {
- FILE* file;
- int* arr;
- int N;
- int ans = 0;
- if ((file = fopen("in.txt", "rt")) == NULL) {
- printf("Can't open input file!\n");
- return -1;
- }
- if (fscanf(file, "%d", &N) != 1) {
- printf("Can't read N!\n");
- fclose(file);
- return -1;
- }
- if (N < 1) {
- printf("Invalid N!\n");
- fclose(file);
- return -1;
- }
- if ((arr = (int*) malloc(N * sizeof(int))) == NULL)
- {
- printf("Can't allocate memory!\n");
- fclose(file);
- return -1;
- }
- if (readarr(file, arr, N) < 0) {
- printf("Can't read array 1!\n");
- fclose(file);
- free(arr);
- }
- printf("%d\n", sol(arr, N));
- free(arr);
- fclose(file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement