Catherrine
2021-09-22 23:44
采纳率: 50%
浏览 82

结构体数组的输入与输出


#include<stdio.h>
#include<stdlib.h>

struct str {
    int id[10];
    int deng[10];
    int chai[10];
    
}st[100];

int compare(const void*a,const void*b){
    struct str *pa = (struct str*)a;
    struct str *pb = (struct str*)b;

    int num1 = *pa->deng + *pa->chai;
    int num2 = *pb->deng + *pb->chai;

    return (int)num2 - num1;

}
int cmp(const void*a, const void*b) {
    struct str *pa = (struct str*)a;
    struct str *pb = (struct str*)b;

    return (int)(*pb->chai - *pa->chai);
}
int main(void)
{
    int n, k, h;
    int sum[100];
    for (int t = 0; t < 100; t++) {
        sum[t] = 0;
    }

    scanf_s("%d %d %d", &n, &k, &h);

    for (int i = 0; i < n; i++) {
        scanf_s("%d %d %d", &st[i].id, &st[i].deng, &st[i].chai);
    }

    for (int i = 0; i < n; i++) {
        printf("%d %d %d\n", st[i].id, st[i].deng, st[i].chai);
    }

    qsort(st, sizeof(st[n]) / sizeof(st[0]), sizeof(int), cmp);


    for (int i = 0; i < n; i++) {
        printf("%d %d %d\n", st[i].id, st[i].deng, st[i].chai);
    }

    return 0;

    
}

img

2条回答 默认 最新

相关推荐 更多相似问题