#include <IOSTREAM>
#include<string>
#include <cstdlib>
using namespace std;
class DNA{
public:
char seqDNA[51];
int num;
};
void setDnaNum(char strDna[],DNA*d);
int compare(const void*p1,const void*p2){
return ((DNA*)p1)->num-((DNA*)p2)->num;
}
int main(){
DNA*dnas;
int n,m;
char strDNa[200];
cin>>m>>n;
dnas=new DNA[n];
for(int i=0;i<n;i++){
cin>>strDNa;
setDnaNum(strDNa,dnas+i);
strcpy(dnas[i].seqDNA,strDNa);
}
qsort(dnas,n,sizeof(DNA),compare);
for(i=0;i<n;i++)
cout<<dnas[i].seqDNA<<endl;
}
void setDnaNum(char strDna[],DNA*d){
int num=0,len=strlen(strDna);
for(int i=0;i<len-1;i++){
for(int j=i+1;j<len;j++){
if(strDna[i]>strDna[j])
num++;
}
}
d->num=num;
}
其中的setDnaNum函数,其只可以传递结构体指针,而不能传递结构体本身,因为,结构体在函数调用时,其实是复制了一个结构体来进行函数调用的,因此,要使用指针才能改变结构体本山的值。
qsort()的用法之1007 DNA Sorting
最新推荐文章于 2018-12-12 09:05:47 发布