按最大的整数和从而按降序对结构数组进行排序

编程入门 行业动态 更新时间:2024-10-11 17:24:05
本文介绍了按最大的整数和从而按降序对结构数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

想象一下,我需要跟踪3个学校24个月的学生记录。我创建了这样的结构:

const int constMonths [ 24 ]; struct 记录{ int 学生[ 24 ]; }; struct 记录学校[ 3 ];

假设我想排序哪个学校的前12个月学生人数最多,并将其显示为以下内容: 学校1:4992 学校3:3029 学校2:1633 我已经搜索了几种排序方法,比如冒泡排序,QuickSort和交换排序,但看起来我在排序学校的事情上无法得到正确的逻辑。 如何做到这一点?谢谢。 更新: 这是我到目前为止所尝试过的:

#include < stdio.h > int main() { int newArray [ 4 ]; int testArray [ 4 ]; int q2 [ 2 ] = { - 1 , - 1 }; int i; newArray [ 0 ] = 488 ; newArray [ 1 ] = 61 ; newArray [ 2 ] = 3884 ; newArray [ 3 ] = 141 ; newArray [ 4 ] = 20 ; testArray [ 0 ] = newArray [ 0 ]; for (i = 0 ; i< 4 ; i ++) { if (q2 [ 0 ] = - 1 ) { q2 [ 0 ] = i; } else if (newArray [i]> = newArray [q2 [ 0 ]]) { q2 [ 1 ] = q2 [ 0 ]; q2 [ 0 ] = i; } else if (newArray [i]> = newArray [q2 [ 1 ]]) { q2 [ 1 ] = i; } } 返回 0 ; }

结果如果我们访问newArray [q2 [i]]:

for (i = 0 ,i< ; 2; i ++) { printf( School%d:%d,q2 [i],newArray [q2 [i]]); }

学校#3:3884 学校#1:488 任何方式显示超过2个?谢谢。 拜托,我想不出这背后的逻辑,也许有人可以帮助我。

解决方案

所有你需要做的(因为这是你的作业你将得不到代码)是使用你提到的排序算法之一 - 所有这些的解释/代码很容易被任何有权访问谷歌的人使用 - 并提供根据您的标准比较两个结构的功能(您没有解释)。无论标准如何,这都是微不足道的。 那么你的问题是什么?我们不打算为你做这件事:毕竟这是你的功课和成绩。但是,如果你只是坐下来思考它几分钟,这是一个非常微不足道的问题。 自己动手:你可能会发现它比你容易得多想!

Imagine that I need to track record of students for 24 months time, for 3 schools. I created a structure like this:

const int constMonths[24]; struct record{ int students[24]; }; struct record school[3];

Say that I want to sort which school has the highest students only for the first 12 months, and display it to something like this: School 1: 4992 School 3: 3029 School 2: 1633 I've searched for several sorting methods like bubble sort, QuickSort and exchange sort, but seems like I couldn't get the logic right when it comes to the sorting schools thing. How can this be done? Thank you. UPDATE: This is what I've tried so far:

#include <stdio.h> int main() { int newArray[4]; int testArray[4]; int q2[2] = {-1, -1}; int i; newArray[0] = 488; newArray[1] = 61; newArray[2] = 3884; newArray[3] = 141; newArray[4] = 20; testArray[0] = newArray[0]; for (i = 0; i < 4; i++) { if (q2[0] = -1) { q2[0] = i; } else if (newArray[i] >= newArray[q2[0]]) { q2[1] = q2[0]; q2[0] = i; } else if (newArray[i] >= newArray[q2[1]]) { q2[1] = i; } } return 0; }

The result if we access newArray[q2[i]]:

for (i =0, i<2; i++) { printf("School %d: %d", q2[i], newArray[q2[i]]); }

School #3: 3884 School #1: 488 Any way to display more than 2 of them? thank you. Please, I couldn't think of the logic behind this, perhaps someone can help me.

解决方案

All you need to do (and since this is your homework you will get no code) is use one of the sorting algorithms you mention - and explanations / code for all of those is easily available to anyone with access to Google - and provide a function to compare two structs according to your criteria (which you don't explain). Which is trivial, pretty much, regardless of the criteria. So what is your problem? We aren't going to do it for you: it's your homework and your grade after all. But it's a pretty trivial problem if you just sit down and think about it for a couple of minutes. Try it yourself: you may find it's a lot easier than you think!

更多推荐

按最大的整数和从而按降序对结构数组进行排序

本文发布于:2023-11-11 08:56:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577953.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   数组   结构   降序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!