#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;
}
结构体数组的输入与输出
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 技术专家团-Plio大咸鱼 2021-09-22 23:57关注
大概这种感觉?
不太清楚为什么你结构体里面申请的是几个数组,但是用的时候只赋了一个值,所以直接去掉了#include <stdio.h> #include <stdlib.h> struct str { int id; int deng; int chai; } 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, n, 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; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥50 有数据,怎么建立模型求影响全要素生产率的因素
- ¥50 有数据,怎么用matlab求全要素生产率
- ¥15 TI的insta-spin例程
- ¥15 完成下列问题完成下列问题
- ¥15 C#算法问题, 不知道怎么处理这个数据的转换
- ¥15 YoloV5 第三方库的版本对照问题
- ¥15 请完成下列相关问题!
- ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
- ¥15 求daily translation(DT)偏差订正方法的代码
- ¥15 js调用html页面需要隐藏某个按钮