一共3个版本
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct data_d_t {
double *v;
size_t s;
};
struct data_vd_t {
struct data_d_t *v;
size_t s;
};
struct data_ds_t {
double d;
size_t s;
};
struct data_pds_t {
struct data_ds_t *v;
size_t s;
};
int compar_d(const void *a, const void *b) {
return *(const double *)a > *(const double *)b;
}
int compar_pds(const void *a, const void *b) {
return ((const struct data_ds_t *)a)->s > ((const struct data_ds_t *)b)->s;
}
void free_d(struct data_d_t d) {
free(d.v);
}
void free_vd(struct data_vd_t d) {
for(size_t i = 0; i < d.s; ++i) {
free_d(d.v[i]);
}
free(d.v);
}
void free_pds(struct data_pds_t d) {
free(d.v);
}
void data_pds_insert(size_t i, struct data_pds_t *d, struct data_ds_t v) {
memmove(&d->v[i + 1], &d->v[i], (d->s - i) * sizeof(v));
d->v[i] = v;
++d->s;
}
void data_pds_append(struct data_pds_t *d, struct data_ds_t v) {
d->v[d->s++] = v;
}
struct data_d_t create_data_d(const double *a, const double *b) {
struct data_d_t result;
result.v = malloc(sizeof(*result.v) * 100);
result.s = 0;
for(size_t i = 0; &a[i] != b; ++i) {
result.v[i] = a[i];
++result.s;
}
return result;
}
struct data_d_t get_input(FILE *f, size_t s) {
struct data_d_t d;
d.s = 0;
d.v = malloc(sizeof(*d.v) * 100);
for(size_t i = 0; i < s; ++i) {
double tmp;
fscanf(f, "%lf", &tmp);
d.v[i] = tmp;
++d.s;
}
return d;
}
struct data_pds_t get_max_distance(struct data_d_t d, size_t s) {
struct data_pds_t result;
result.v = malloc(sizeof(*result.v) * 100);
result.s = 0;
struct data_d_t distance;
distance.s = d.s - 1;
distance.v = malloc(sizeof(*distance.v) * distance.s);
for(size_t i = 0; i < distance.s; ++i) {
distance.v[i] = fabs(d.v[i] - d.v[i + 1]);
}
for(size_t i = 0; i < s; ++i) {
struct data_ds_t tmp;
tmp.d = distance.v[0];
tmp.s = 0;
result.v[i] = tmp;
++result.s;
distance.v[0] = 0;
for(size_t j = 1; j < distance.s; ++j) {
struct data_ds_t *p = &result.v[i];
if(distance.v[j] > p->d) {
distance.v[p->s] = p->d;
p->d = distance.v[j];
p->s = j;
distance.v[j] = j;
}
}
}
free_d(distance);
return result;
}
struct data_vd_t get_groups(struct data_d_t d, size_t s) {
struct data_vd_t result;
result.v = malloc(sizeof(*result.v) * 100);
result.s = 0;
struct data_pds_t distance = get_max_distance(d, s - 1);
qsort(distance.v, distance.s, sizeof(*distance.v), compar_pds);
struct data_ds_t tmp;
tmp.d = 0;
tmp.s = -1;
data_pds_insert(0, &distance, tmp);
tmp.d = 0;
tmp.s = d.s - 1;
data_pds_append(&distance, tmp);
for(size_t i = 0; i < distance.s - 1; ++i) {
result.v[i] = create_data_d(&d.v[distance.v[i].s + 1], &d.v[distance.v[i + 1].s + 1]);
++result.s;
}
free_pds(distance);
return result;
}
struct data_d_t get_average(struct data_vd_t d) {
struct data_d_t result;
result.v = malloc(sizeof(*result.v) * 100);
result.s = 0;
for(size_t i = 0; i < d.s; ++i) {
double sum = 0;
for(size_t j = 0; j < d.v[i].s; ++j) {
sum += d.v[i].v[j];
}
result.v[i] = sum / d.v[i].s;
++result.s;
}
return result;
}
void print(struct data_d_t d) {
for(size_t i = 0; i < d.s; ++i) {
printf("%f ", d.v[i]);
}
printf("\n");
}
int main(void) {
struct data_d_t raw_data = get_input(stdin, 10);
qsort(raw_data.v, raw_data.s, sizeof(*raw_data.v), compar_d);
struct data_vd_t group_data = get_groups(raw_data, 3);
struct data_d_t average = get_average(group_data);
print(average);
free_d(average);
free_vd(group_data);
free_d(raw_data);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int compar_d(const void *a, const void *b) {
return *(const double *)a > *(const double *)b;
}
void print(double data[], size_t size) {
for(size_t i = 0; i < size; ++i) {
printf("%f ", data[i]);
}
printf("\n");
}
int main(void) {
double raw_buff[10];
for(size_t i = 0; i < 10; ++i) {
scanf("%lf", &raw_buff[i]);
}
qsort(raw_buff, 10, sizeof(*raw_buff), compar_d);
size_t group_buff[2];
//size_t group_size = get_groups(group_buff, raw_buff, 10);
{
double distance_buff[9];
for(size_t i = 0; i < 9; ++i) {
distance_buff[i] = fabs(raw_buff[i] - raw_buff[i + 1]);
}
for(size_t i = 0; i < 2; ++i) {
size_t max_index = 0;
for(size_t j = 1; j < 9; ++j) {
if(distance_buff[max_index] < distance_buff[j]) {
max_index = j;
}
}
distance_buff[max_index] = 0;
group_buff[i] = max_index;
}
if(group_buff[0] > group_buff[1]) {
size_t tmp = group_buff[0];
group_buff[0] = group_buff[1];
group_buff[1] = tmp;
}
}
double average_buff[3];
//size_t average_size = get_average(average_buff, raw_buff, group_buff, group_size);
{
{
double sum = 0;
for(size_t i = 0; i <= group_buff[0]; ++i) {
sum += raw_buff[i];
}
average_buff[0] = sum / (group_buff[0] + 1);
}
{
double sum = 0;
for(size_t i = group_buff[0] + 1; i <= group_buff[1]; ++i) {
sum += raw_buff[i];
}
average_buff[1] = sum / (group_buff[1] - group_buff[0]);
}
{
double sum = 0;
for(size_t i = group_buff[1] + 1; i <= 9; ++i) {
sum += raw_buff[i];
}
average_buff[2] = sum / (9 - group_buff[1]);
}
}
print(average_buff, 3);
return 0;
}
|