此题求思路~~~~~
现有若干个集合的数据,每个数据集合可以自选一个指标参加排序。这些指标包含如下四种:
?Min,取集合中元素的最小值
?Max,取集合中元素的最大值
?Mean,取集合中元素的平均值,平均值的计算公式为:(V1+V2+…+Vn) / n
?Median,取集合中元素的中值,中值的计算公式为:(Vmin+Vmax) / 2
读入数据后,输出时请根据各个集合选择的指标对这些集合进行降序排列,每个集合内的元素请升序排列。
要求:必须使用标准容器和标准泛型算法,否则本题不得分。
输入文件:C:\3_in.txt。每行一个集合。[]内为该集合选取的用于集合间排序的指标。随后为集合内各个元素,元素个数不定,以空格分隔。
输出文件:C:\3_out.txt。每行输出一个集合。{}内为计算出该集合的排序指标值,随后为该集合的各个元素的升序排列。
输入样例:C:\3_in.txt
[Max]8 3 15
[Min]9 10 1 2 7
[Median]2 4
[Mean]30 20 10
输出样例:C:\3_out.txt
{20}10 20 30
{15}3 8 15
{3}2 4
{1}1 2 7 9 10
以上是题目,我有几个问题。
首先这个行的读取量是未知的,其次每一行读取的数据是未知的。那么我最好的读取方式是什么?
如果一行一行读的话,每行的每个数据又如何读取,每行需要调用的函数又如何判别?
主要是读取的问题,我不知道如何可以更高效,快速的读取。
[解决办法]
帮顶
出去会,待会回来看帖,呵呵
[解决办法]
还是手写了两个循环。
- C/C++ code
#pragma warning( disable : 4786 )#include <iostream>#include <fstream>#include <sstream>#include <vector>#include <string>#include <map>#include <utility>#include <algorithm>#include <numeric>using namespace std;int main(int argc,char *argv[]){ multimap< int, vector< int > > mapData; ifstream infile( "C:\\3_in.txt" ); while( infile ) { string strLine; getline( infile, strLine ); if( infile.eof() && strLine.empty() ) break; string::size_type pos1 = strLine.find( '[' ) + 1; string::size_type pos2 = strLine.find( ']' ); string strType = strLine.substr( pos1, pos2-pos1 ); strLine = strLine.substr( pos2 + 1 ); stringstream ss( strLine.c_str() ); istream_iterator< int > in( ss ); istream_iterator< int > end; vector< int > vec( in, end ); //vector< int > vec; // for VC6 //copy( in, end, back_inserter( vec ) ); sort( vec.begin(), vec.end() ); int value = 0; if( strType == "Max" ) value = vec.back(); else if( strType == "Min" ) value = vec[0]; else if( strType == "Median" ) value = ( vec[0] + vec.back() ) / 2; else if( strType == "Mean" ) value = accumulate( vec.begin(), vec.end(), 0 ) / vec.size(); mapData.insert( make_pair( value, vec ) ); } infile.close(); ofstream outfile( "C:\\3_out.txt" ); multimap< int, vector< int > >::reverse_iterator iter1; for( iter1 = mapData.rbegin() ; iter1 != mapData.rend(); ++iter1 ) { outfile << '{' << iter1->first << '}'; ostream_iterator< int > out( outfile, " " ); copy( iter1->second.begin(), iter1->second.end(), out ); outfile << endl; } outfile.close(); return 0;}
[解决办法]
不错嘛,都挺牛的