本文介绍了按最大的整数和从而按降序对结构数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 想象一下,我需要跟踪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: 4992School 3: 3029School 2: 1633I'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: 3884School #1: 488Any 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! 这篇关于按最大的整数和从而按降序对结构数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 12:02