1007 DNA Sorting

本文介绍了一个使用C语言实现的程序,该程序通过输入一系列字符串并根据特定规则进行排序。程序首先读取字符串的数量和每个字符串的长度,然后逐个读取字符串,并计算每个字符串中较小字符位于较大字符之前的次数作为排序依据。最后使用qsort函数根据计算得到的数值对字符串进行排序并输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>
struct record
{
    char s[52];
    int ans;
}cord[102];
int comp(const void *a,const void *b);
int main()
{
    int lenth,n,i;
    scanf("%d %d",&lenth,&n);
    for(i=0;i<n;i++)
    {
        scanf("%s",cord[i].s);
        int j,k;
        for(j=0;j<lenth;j++)
        {
            for(k=j+1;k<lenth;k++)
            {
                if(cord[i].s[k]<cord[i].s[j])
                    cord[i].ans++;
            }
        }
    }
    qsort(cord,n,sizeof(struct record),comp);
    for(i=0;i<n;i++)
        printf("%s\n",cord[i].s);
    return 0;
}
int comp(const void *a,const void *b)
{
    return ((struct record*)a)->ans-((struct record*)b)->ans;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值