帮忙看一道题目
问题描述输入任意10个浮点数,根据它们的聚集程度划分为3组,输出每一组的平均值。
提供老师上课讲的一种思路:将10个数字进行在数轴上排序,然后计算每两个点间的距离,在所有的距离中选取两个最大距离处断开,这样就把10个数字分为了3组。
本题难度较大,如果深入讨论会比较复杂,大家可以只考虑如下面样例所示的分组情况非常简单的情况,只要简单情况能够成功计算,本题就能得分。
十个待输入的浮点数,使用空格隔开
输出格式
三组数的平均数,每输出一个需要换行
样例输入
一个满足题目要求的输入范例。
例1:
50.4 51.3 52.3 9.5 10.4 11.6 19.1 20.8 21.9 49.6
例2:
8.6 7.4 3.5 17.9 19.1 18.5 37.6 40.4 38.5 40.0
样例输出
与上面的样例输入对应的输出。
例1:
10.5
20.6
50.9
例2:
6.5
18.5
39.125
数据规模和约定
仅考虑如样例一样简单情况、分组明显的输入 本帖最后由 人造人 于 2021-4-4 20:48 编辑
1. 没说用什么语言写,那就用C++写了
2. 完全按照你们老师的思路写的
3. 没有注释,^_^
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
const std::vector<double> get_input(std::istream &is, size_t size) {
std::vector<double> v;
for(size_t i = 0; i < size; ++i) {
double tmp; is >> tmp;
v.push_back(tmp);
}
return v;
}
const std::vector<std::pair<double, size_t>> get_max_distance(const std::vector<double> &lhs, size_t size) {
std::vector<std::pair<double, size_t>> result;
std::vector<double> distance(lhs.size() - 1);
for(size_t i = 0; i < distance.size(); ++i) {
distance = std::fabs(lhs - lhs);
}
for(size_t i = 0; i < size; ++i) {
result.push_back(std::pair<double, size_t>(distance, 0));
distance = 0;
for(size_t j = 1; j < distance.size(); ++j) {
auto p = result.end() - 1;
if(distance > p->first) {
distance = p->first;
p->first = distance;
p->second = j;
distance = 0;
}
}
}
return result;
}
const std::vector<std::vector<double>> get_groups(const std::vector<double> &v, size_t size) {
std::vector<std::vector<double>> result;
std::vector<std::pair<double, size_t>> distance = get_max_distance(v, size - 1);
std::sort(distance.begin(), distance.end(), [](const std::pair<double, size_t> &a, \
const std::pair<double, size_t> &b) -> bool {return a.second < b.second;});
distance.insert(distance.begin(), std::pair<double, size_t>(0, -1));
distance.push_back(std::pair<double, size_t>(0, v.size() - 1));
for(size_t i = 0; i < distance.size() - 1; ++i) {
result.push_back(std::vector<double>(v.begin() + distance.second + 1, v.begin() + distance.second + 1));
}
return result;
}
std::vector<double> get_average(const std::vector<std::vector<double>> &data) {
std::vector<double> v;
for(auto iter = data.begin(); iter != data.end(); ++iter) {
double sum = 0;
for(const auto &j: *iter) sum += j;
v.push_back(sum / iter->size());
}
return v;
}
std::ostream &operator<<(std::ostream &os, const std::vector<double> &rhs) {
for(const auto &i: rhs) os << i << " ";
return os;
}
int main() {
std::vector<double> raw_data = get_input(std::cin, 10);
std::sort(raw_data.begin(), raw_data.end());
std::vector<std::vector<double>> group_data = get_groups(raw_data, 3);
std::cout << get_average(group_data) << std::endl;
return 0;
}
人造人 发表于 2021-4-4 20:38
1. 没说用什么语言写,那就用C++写了
2. 完全按照你们老师的思路写的
3. 没有注释,^_^
可以换成c吗,这看不懂 一共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);
}
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, &d->v, (d->s - i) * sizeof(v));
d->v = v;
++d->s;
}
void data_pds_append(struct data_pds_t *d, struct data_ds_t v) {
d->v = 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 != b; ++i) {
result.v = a;
++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 = 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 = fabs(d.v - d.v);
}
for(size_t i = 0; i < s; ++i) {
struct data_ds_t tmp;
tmp.d = distance.v;
tmp.s = 0;
result.v = tmp;
++result.s;
distance.v = 0;
for(size_t j = 1; j < distance.s; ++j) {
struct data_ds_t *p = &result.v;
if(distance.v > p->d) {
distance.v = p->d;
p->d = distance.v;
p->s = j;
distance.v = 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 = create_data_d(&d.v.s + 1], &d.v.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.s; ++j) {
sum += d.v.v;
}
result.v = sum / d.v.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);
}
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);
}
printf("\n");
}
int main(void) {
double raw_buff;
for(size_t i = 0; i < 10; ++i) {
scanf("%lf", &raw_buff);
}
qsort(raw_buff, 10, sizeof(*raw_buff), compar_d);
size_t group_buff;
//size_t group_size = get_groups(group_buff, raw_buff, 10);
{
double distance_buff;
for(size_t i = 0; i < 9; ++i) {
distance_buff = fabs(raw_buff - raw_buff);
}
for(size_t i = 0; i < 2; ++i) {
size_t max_index = 0;
for(size_t j = 1; j < 9; ++j) {
if(distance_buff < distance_buff) {
max_index = j;
}
}
distance_buff = 0;
group_buff = max_index;
}
if(group_buff > group_buff) {
size_t tmp = group_buff;
group_buff = group_buff;
group_buff = tmp;
}
}
double average_buff;
//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; ++i) {
sum += raw_buff;
}
average_buff = sum / (group_buff + 1);
}
{
double sum = 0;
for(size_t i = group_buff + 1; i <= group_buff; ++i) {
sum += raw_buff;
}
average_buff = sum / (group_buff - group_buff);
}
{
double sum = 0;
for(size_t i = group_buff + 1; i <= 9; ++i) {
sum += raw_buff;
}
average_buff = sum / (9 - group_buff);
}
}
print(average_buff, 3);
return 0;
}
人造人 发表于 2021-4-7 15:15
一共3个版本
谢谢
页:
[1]