题0902

[quote]现有若干个集合的数据,每个数据集合可以自选一个指标参加排序。这些指标包含如下四种:
Min,取集合中元素的最小值
Max,取集合中元素的最大值
Mean,取集合中元素的平均值,平均值的计算公式为:(V1+V2+…+Vn) / n
Median,取集合中元素的中值,中值的计算公式为:(Vmin+Vmax) / 2
读入数据后,输出时请根据各个集合选择的指标对这些集合进行降序排列,每个集合内的元素请升序排列。
要求:必须使用标准容器和标准泛型算法,否则本题不得分。
输入文件:C:\2_in.txt。每行一个集合。[]内为该集合选取的用于集合间排序的指标。随后为集合内各个元素,元素个数不定,以空格分隔。
输出文件:C:\2_out.txt。首行08XXXX应替换为本人的学号,第二行起每行输出一个集合。{}内为计算出该集合的排序指标值,随后为该集合的各个元素的升序排列。
输入样例:C:\2_in.txt
[Max]8 3 15
[Min]9 10 1 2 7
[Median]2 4
[Mean]30 20 10

输出样例:C:\2_out.txt
Done by 08XXXX
{20}10 20 30
{15}3 8 15
{3}2 4
{1}1 2 7 9 10[/quote]

我写了个类专门处理取各种指标的操作。这样的话这道题其实也很复杂

基础知识:
1)面向对象的类的创建
class MyContainer
{
private:
string indicator;
vector<int> container;
int key;
bool isValid;

void setIndicator(string & str);
void setContainer(string & str);
void setKey();
public:
MyContainer(string & str);
MyContainer();
int getKey() const;
int IsValid() const;
};


2) STL集合
vector<int>  container;
map<string, MyContainer> out;


3)<<操作符重载
ostream & operator <<(ostream & os, MyContainer c);


4)友元
class MyContainer
{
...
friend ostream & operator <<(ostream & os, MyContainer c);
};


5)字符串解析 string, string::npos, find, substr
void MyContainer::setIndicator(string  & str)
{
int first = str.find("[");
int last = str.find("]");
if (first==string::npos || last == string::npos || last-first<=1)
{
isValid = false;
return;
}
indicator = str.substr(first+1, last-first-1);
}


6)STL操作
void MyContainer::setContainer(string  & str)
{
...
sort(container.begin(),container.end());
}
void MyContainer::setKey()
{
if (indicator=="Max")
key= *max_element(container.begin(), container.end());
else if (indicator == "Min")
key = *min_element(container.begin(),container.end());
else if(indicator == "Mean")
key = accumulate(container.begin(),container.end(), 0)/container.size();
else if(indicator == "Median")
key = (*max_element(container.begin(), container.end())+*min_element(container.begin(),container.end()))/2;
else
isValid = false;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值