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 prev = 0;
- int c = 1;
- int f = 0;
- prev = arr[0];
- for (int i = 1; i < n; i++)
- {
- if (arr[i] - prev == 1)
- {
- c++;
- }
- else if (c > 2)
- {
- c = 1;
- }
- else
- {
- f = 1;
- break;
- }
- prev = arr[i];
- if ( i == (n - 1) & c != 0 & c < 3)
- {
- f = 1;
- }
- }
- return f;
- }
- 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);
- }
- if (sol(arr, N) == 0)
- {
- printf("can be divided\n");
- }
- else
- {
- printf("can NOT be divided\n");
- }
- free(arr);
- fclose(file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement