美文网首页
1007 DNA Sorting

1007 DNA Sorting

作者: 狂奔的皮皮花 | 来源:发表于2018-09-23 14:16 被阅读0次
#include <stdio.h>

#define MAX_CHARS 50
#define MAX_STRINGS 100

struct DNA {
    char str[MAX_CHARS];
    int unsortedness;
};

int main() {
    struct DNA dnas[MAX_STRINGS];

    int n, m;//n=length m=number
    scanf("%d %d", &n, &m);

    int count = 0;
    while (count < m) {

        dnas[count].unsortedness = 0;
        scanf("%s", dnas[count].str);

        // compute the unsortedness
        for (int i = 0; i < n-1; i ++) {
            for (int j = i+1; j < n; j ++) {
                if (dnas[count].str[i]>dnas[count].str[j]) {
                    dnas[count].unsortedness ++;
                }
            }
        }

        count ++;

    }

    // create pointer array
    struct DNA *dnasp[MAX_STRINGS];
    for (int i = 0; i < m; i ++) {
        dnasp[i] = &dnas[i];
    }

    // sort
    for (int i = 1; i < m; i ++) {

        int j = i - 1;
        struct DNA *temp = dnasp[i];

        while (j >= 0 && dnasp[j]->unsortedness > temp->unsortedness) {
            dnasp[j+1] = dnasp[j];
            j --;
        }
        dnasp[j+1] = temp;
    }

    for (int i = 0; i < m; i ++) {
        printf("%s\n", dnasp[i]->str);
    }

    return 0;
}

相关文章

网友评论

      本文标题:1007 DNA Sorting

      本文链接:https://www.haomeiwen.com/subject/qwzfoftx.html