結構_[CPE] Sort! Sort!! and Sort!!!

 avatar
user_3763047219
c_cpp
a year ago
2.1 kB
1
Indexable
Never
#include<stdio.h>
#include<stdlib.h>

typedef struct List {
    int num;
    int mod;
    int Even_Odd;
}List;

int main() {
    int n = 0, m = 0;
    scanf("%d %d", &n, &m);
    struct List* list;
    list = (struct List*)malloc(sizeof(struct List) * n);
    for (int i = 0; i < n; i++) {
        scanf("%d ", &list[i].num);
    }
    /*
    int n0 = 0, m0 = 0;
    scanf("%d %d", m0, m0);
    */
    for (int i = 0; i < n; i++) {
        list[i].mod = list[i].num % m;
        if (list[i].num % 2 == 0) {
            list[i].Even_Odd = 1;
        }
        else {
            list[i].Even_Odd = 0;
        }
    }

    for (int i = n - 1; i >= 1; i--) {
        for (int j = 0; j < i; j++) {
            if (list[j].mod > list[j + 1].mod) {
                struct List temp = list[j];
                list[j] = list[j + 1];
                list[j + 1] = temp;
            }
            else if (list[j].mod == list[j + 1].mod) {
                if (list[j].Even_Odd > list[j + 1].Even_Odd) {
                    struct List temp = list[j];
                    list[j] = list[j + 1];
                    list[j + 1] = temp;
                }
                else if (list[j].Even_Odd == list[j + 1].Even_Odd) {
                    if (list[j].Even_Odd == 1) {
                        if (list[j].num > list[j + 1].num) {
                            struct List temp = list[j];
                            list[j] = list[j + 1];
                            list[j + 1] = temp;
                        }
                    }
                    else {
                        if (list[j].num < list[j + 1].num) {
                            struct List temp = list[j];
                            list[j] = list[j + 1];
                            list[j + 1] = temp;
                        }
                    }
           
                }
            }
        }
    }
    printf("%d %d\n", n ,m);
    for (int i = 0; i < n; i++) {
        printf("%d\n", list[i].num);
    }
    printf("0 0");
}