題0902


参照
いくつかのセットのデータがあり、各データセットは1つの指標を選択してソートに参加することができます.これらの指標には、次の4つが含まれています.
Min,コレクション内の要素の最小値をとる
Max,コレクション内の要素の最大値をとる
Mean、集合の中の元素の平均値を取って、平均値の計算式は:(V 1+V 2+...+Vn)/n
Median、集合の中の要素の中値を取って、中値の計算式は:(Vmin+Vmax)/2
データを読み込み、出力するときは各集合で選択した指標に従ってこれらの集合を降順に並べ、各集合内の要素を昇順に並べてください.
要件:標準コンテナと標準汎用アルゴリズムを使用する必要があります.そうしないと、本題は得点しません.
入力ファイル:C:2_in.txt.行ごとに集合する.[]内で選択したコレクション間のソートに使用する指標.次に、集合内の各要素について、要素の数が不定で、スペースで区切られます.
出力ファイル:C:2_out.txt.最初の行08 XXXXは私の学号に交换しなければならなくて、第2行から各行は1つの集合を出力します.{}内は、その集合を計算するソート指標値であり、その後、その集合の各要素の昇順配列である.
入力サンプル: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
私はいろいろな指標を取る操作を専門に処理するクラスを書いた.これではこの問題も実に複雑だ
基礎知識:
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;
}