Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
奉献代码!仅供参考!/* DNA排序 * 根据DNA字符序列的“sortness”由高到低排序,同时需要稳定排序 * 使用C++中的标准输入输出流在速度上要比printf,scanf慢 * 这道题的本质还是域排序的问题,完全可以构造一个比较函数,还后使用推排序的算法 * 启示:对于数组使用stable_sort()等STL算法后,数组本身的内容也会改变的 */ #include<iostream> #include<string> #include<algorithm> //#include<fstream> #include<vector> using namespace std; #define MAX 102 int linenum,num; int counter; typedef struct{ string seq; int unsortness; }DNA; DNA dna[MAX]; void process(string tmp){ int unsortness=0; for(int i=0;i<linenum ; i++) for(int j=i+1;j< linenum ; j++){ if( tmp[i]>tmp[j]) unsortness++; } dna[counter].seq=tmp; dna[counter].unsortness=unsortness; counter++; } bool compare(DNA a,DNA b){ //返回false,表示不符合排序要求需要交换 if( a.unsortness <= b.unsortness ) return true; else return false; } int main(){ //ifstream in("test.txt"); //in>>linenum>>num; cin>>linenum>>num; string tmp; counter=0; for(int i=0; i<num; i++){ //in>>tmp; cin>>tmp; process(tmp); } stable_sort(dna,dna+counter,compare); //注意dna+counter表示end,不是dna+counter-1 for(int j=0 ; j<counter ; j++) cout<<dna[j].seq<<endl; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator