| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1013 人关注过本帖
标题:DNA 逆序数 排列问题
只看楼主 加入收藏
z8869113
Rank: 2
等 级:论坛游民
帖 子:44
专家分:14
注 册:2010-12-14
结帖率:75%
收藏
已结贴  问题点数:74 回复次数:4 
DNA 逆序数 排列问题
这是北京大学  1007题原题如下     我简议一下:输入m,n;m为行数(0<=m<100),n为列数(0<=n<50)
再输入  由ACTG组成的n列m行的字符串
由逆序数从小到大排列输出
例: Input

10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT
 Output

CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA

Description

One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence ``AACEDGG'' has only one inversion (E and D)---it is nearly sorted---while the sequence ``ZWQM'' has 6 inversions (it is as unsorted as can be---exactly the reverse of sorted).

You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness'', from ``most sorted'' to ``least sorted''. All the strings are of the same length.

Input

The first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (0 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.
Output

Output the list of input strings, arranged from ``most sorted'' to ``least sorted''. Since two strings can be equally sorted, then output them according to the orginal order.
Sample Input

10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT
Sample Output

CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA
以下是我的程序
#include<stdio.h>
void main()
{
    int i,j,m,n,s[50],k;
    scanf("%d%d",&n,&m);
    char a[100][50],t;                //输入
    for(i=0 ; i<m ; i++)
    for(j=0 ; j<n ; j++)
    {
        scanf("%c",&a[m][n]);
        s[m]=0;
    }
        for(i=0 ; i<m ; i++)                  
        for(k=0 ; k<n ; k++)                //统计
        for(j=k+1 ; j<n ; j++)
         if(a[i][k]<a[i][j])
            s[i]=s[i]+1;
        for(i=0 ; i<m ; i++)
        for(j=i+1 ; j<m ; j++)
            if(s[i]>s[j])        //判断
            {
                for(k=0 ; k<n ; k++)        //交换
                {
                    t=a[i][k];
                    a[i][k]=a[j][k];
                    a[j][k]=t;
                }
            }
        for(i=0 ; i<m ; i++)
        {
            for(j=0 ; j<n ; j++)
            printf("%c",a[i][j]);
            printf("\n");
        }
}结果输出都是烫....求高手   谢谢啦~
搜索更多相关主题的帖子: 北京大学 字符串 
2011-03-25 15:48
『点点滴滴』
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:168
专家分:1035
注 册:2007-7-9
收藏
得分:74 
#include <stdio.h>
char a[101][51];
typedef struct {
    int seq;
    int inv;
}pair;

pair t[101];

int main()
{
    int len, N, i, j, k, p;
    int cnt;
    pair temp;
   
    t[0].seq = 0;
    t[0].inv = 0;
   
    scanf("%d", &len);
    scanf("%d", &N);
   
    for (i=1; i<=N; i++) {
        scanf("%s",a[i]);
        cnt = 0;
        for (j=len-1; j>=1; j--) {
            for (k=0; k<j; k++) {
                if (a[i][k]>a[i][j]) cnt++;
            }
        }
        temp.seq = i;
        temp.inv = cnt;
        
        for (k=i-1;k>=0&&t[k].inv>temp.inv; k--) {
            t[k+1] = t[k];
        }
        t[k+1] = temp;
    }
    for (p=1; p<=N; p++) {
        printf("%s\n", a[t[p].seq]);
    }
    return 0;
}

你的程序需要注意的地方:
1.输入数据时候是a[i][j]不是a[m][n],用%c要注意用getchar()吃掉多余的回车;
2.s数组没有初始化.
3.排序有问题,范围有错,如果俩个字符串交换过位置,那么他们的s[]也应该做相应的变换

[ 本帖最后由 『点点滴滴』 于 2011-3-25 17:12 编辑 ]
2011-03-25 16:30
ansic
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:恍惚窈冥
等 级:城市猎人
帖 子:1543
专家分:5367
注 册:2011-2-15
收藏
得分:0 
看不懂。。。

善人者,不善人之师;不善人者,善人之资。不贵其师,不爱其资,虽智大迷。
2011-03-25 16:32
njkido
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:224
专家分:1184
注 册:2011-3-8
收藏
得分:0 
scanf("%d,%d",&n,&m);
fflush(stdin);

scanf("%c",&a[i][j]);
fflush(stdin);
2011-03-25 16:47
z8869113
Rank: 2
等 级:论坛游民
帖 子:44
专家分:14
注 册:2010-12-14
收藏
得分:0 
回复 2楼 『点点滴滴』
真强!非常感谢
2011-03-26 15:41
快速回复:DNA 逆序数 排列问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.056900 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved