結構_[CPE] DNA Sorting

 avatar
user_3763047219
c_cpp
2 years ago
1.1 kB
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

struct DNA {
    char string[50];
        int order;
};

int main() {
    int case1 = 0;
    scanf("%d", &case1);
    int n = 0,m = 0;
    scanf("%d %d", &n, &m);
    struct DNA* list;
    list = (struct DNA*)malloc(sizeof(struct DNA) * m);
    for (int i = 0; i < m; i++) {
        scanf("%s", &list[i].string);
    }

    for (int i = 0; i<m; i++) {
        int count = 0;
        for (int j = 0; j < n-1; j++) {
            for (int k = j + 1; k < n; k++) {
                if (list[i].string[j] > list[i].string[k]) {
                    count++;
                }
            }
        }
        list[i].order = count;
    }

    for (int i = m - 1; i >= 0; i--) {
        for (int j = 0; j < i; j++) {
            if (list[j].order > list[j + 1].order) {
                struct DNA temp = list[j];
                list[j] = list[j + 1];
                list[j + 1] = temp;
            }
        }
    }

    for (int i = 0; i < m-1; i++) {
        printf("%s\n", list[i].string);
    }
    printf("%s", list[m-1].string);
}