Делаю 15 Задачу
This commit is contained in:
parent
3f5c6299fc
commit
4c36e1638a
3 changed files with 62 additions and 18 deletions
|
@ -10,6 +10,6 @@ int main(void) {
|
|||
numbers = getList(file);
|
||||
if (numbers == NULL) return 1;
|
||||
|
||||
printf("Count elements in file: %d", replaceLocalMin(numbers));
|
||||
printf("Count elements in file: %d", findAndReplaceLocalMin(numbers));
|
||||
free(numbers);
|
||||
}
|
||||
|
|
|
@ -1,25 +1,63 @@
|
|||
#include "replace_local_min.h"
|
||||
|
||||
double min(double * numbers) {
|
||||
|
||||
for (int i = 1; i < length; ++i) {
|
||||
int findAndReplaceLocalMin(double * numbers) {
|
||||
double min_element = min(numbers);
|
||||
int length = (int)numbers[0];
|
||||
unsigned start_end_minimum_index[3] = {0};
|
||||
do {
|
||||
unsigned start_index = (start_end_minimum_index[2]) ? start_end_minimum_index[2] : 1;
|
||||
findLocalMinimum(&numbers[start_index], length - start_index, start_end_minimum_index);
|
||||
replaceLocalMinimum(&numbers[start_index], start_end_minimum_index, min_element);
|
||||
} while start_end_minimum_index[0];
|
||||
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
printf("%d\n", start_end_minimum_index[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int replaceLocalMin(double * numbers) {
|
||||
int length = numbers[0];
|
||||
unsigned final_num = 1;
|
||||
|
||||
double min(double * numbers) {
|
||||
unsigned index_min_el = 0;
|
||||
for (int i = 1; i < (int)numbers[0]; ++i) {
|
||||
if ((numbers[index_min_el] - numbers[i]) > exp || !index_min_el) index_min_el = i;
|
||||
}
|
||||
return numbers[index_min_el];
|
||||
}
|
||||
|
||||
|
||||
void findLocalMinimum(double * numbers, unsigned length, unsigned *start_end_minimum_index) {
|
||||
unsigned local_length = 1;
|
||||
|
||||
for (int i = 2; i < length; ++i, ++final_num) {
|
||||
if (fabs(numbers[i - 1] - numbers[i]) < exp) { if (local_length) ++local_length;
|
||||
} else if ((numbers[i] - numbers[i - 1]) > exp) {
|
||||
if (local_length) final_num -= (local_length - 1), local_length = 0;
|
||||
} else local_length = 1;
|
||||
for (unsigned i = 1; i < length; ++i) {
|
||||
if (isEqual(numbers[i - 1], numbers[i])) {
|
||||
if (local_length) ++local_length;
|
||||
} else if ((numbers[i - 1] - numbers[i]) < exp) {
|
||||
if (local_length) {
|
||||
start_end_minimum_index[0] = i - local_length, start_end_minimum_index[1] = i - 1;
|
||||
local_length = 0;
|
||||
}
|
||||
} else {
|
||||
if (start_end_minimum_index[1]) {
|
||||
start_end_minimum_index[2] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((fabs(numbers[length - 1] - numbers[length - 2]) < exp) && (local_length)) final_num -= (local_length - 1);
|
||||
|
||||
return final_num;
|
||||
}
|
||||
|
||||
|
||||
bool isEqual(double first, double second) {
|
||||
return (fabs(first - second) < exp);
|
||||
}
|
||||
|
||||
|
||||
void replaceLocalMinimum(double * numbers, unsigned * start_end_minimum_index, double minimum) {
|
||||
unsigned start_i = start_end_minimum_index[0], end_i = start_end_minimum_index[1]
|
||||
numbers[start_i] = minimum;
|
||||
|
||||
for (int i = 0)
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -2,9 +2,15 @@
|
|||
#define REPLACE_LOCAL_MIN
|
||||
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define exp 1.e-6
|
||||
|
||||
int replaceLocalMin(double * numbers);
|
||||
int findAndReplaceLocalMin(double * numbers);
|
||||
double min(double * numbers);
|
||||
void findLocalMinimum(double * numbers, unsigned length, unsigned *start_end_minimum_index);
|
||||
bool isEqual(double first, double second);
|
||||
|
||||
#endif // REPLACE_LOCAL_MIN
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue