【问题标题】:Write a function that finds the top 5 max values in array of structures?编写一个函数来查找结构数组中的前 5 个最大值?
【发布时间】:2017-04-12 13:49:58
【问题描述】:

找到结构数组中的前 5 个最大值(用于 C 编程)? 我有一个结构数组如下:

struct info {
char name[100];
int number;
}
struct info people[10]

在 char name[100] 中是人名(最多 10 个),它们在 int balance 中有对应的值:

Jane 10
John 40
Harry 16
Eva -5
...

直到达到 10 人。 如何找到并打印数字最高的 5 个人?
即:

John 40
Harry 16
Jane 10
...

我已经尝试了以下代码:

int i,j, k=5, max, temp;
//move maximum 5 numbers to the front of the array
for (i=0; i<k; i++) {
    max=i;
for (j=i+1; j<10; j++) {
    if (people[i].number>people[max].number) {
        max=j;
    }
}
//swap numbers
temp=people[i].number;
people[i].number=people[max].number;
people[max].number=temp;

//swap names to match swapped numbers so they correspond
temp=people[i].name;
people[i].name=people[max].name;
people[max]=temp;
}
for (i=0; i<k; i++) {
    printf("%s  %d\n", people[i].name, people[i].number);
}

但是,我在第二次交换时收到一条错误消息,因为它是 char 类型。我应该如何解决这个问题,或者还有什么方法可以实现这个目标?

【问题讨论】:

  • 您使用strcpy 系列函数复制字符串。
  • 为什么要交换结构?只需找到对应于数组中具有最大值的五个元素的五个索引。请记住允许重复值的可能性。
  • struct info temp = people[i]; people[i] = people[max]; people[max] = temp;
  • 使用这个例子排序..cplusplus.com/forum/general/97555
  • @4py - 对c 程序员不是很有帮助:(

标签: c arrays structure


【解决方案1】:

只需对数组进行排序,然后取排序后数组的第一个/最后一个(取决于排序顺序)条目。

第一个定义比较函数:

#include <stdlib.h> /* for qsort() */
#include <stdio.h> /* for printf() */


struct info
{
  char name[100];
  int number;
};

int cmp_struct_info_desc(const void * pv1, const void * pv2)
{
  const struct info * pi1 = pv1;
  const struct info * pi2 = pv2;

  return pi2->number - pi1->number;
}

第二次使用Standard C function qsort()

struct info people[] =  {
  ... /* initialise array people here ... */
}

int main(void)
{
  size_t number_of_array_elements = sizeof people/sizeof *people;

  qsort(people, number_of_array_elements, sizeof *people, cmp_struct_info_desc);

  for (size_t s = 0; s < number_of_array_elements; ++s)
  {
    printf("%zu. = {%d, '%s'}\n", s, people[s].number, people[s].name);
  }
}

【讨论】:

  • 这就是我的意思。加分。
【解决方案2】:

从 OP 的代码开始,只需交换结构即可。 @BLUEPIXY。 OP 的代码只需要稍作改动。

数组不能用赋值复制,但对象,比如struct info可以赋值。

int i, j, k=5;

//move maximum `k` numbers to the front of the array
for (i=0; i<k; i++) {
  int max=i;
  for (j=i+1; j<10; j++) {
    if (people[i].number > people[max].number) {
      max=j;
    }
  }
  //swap info
  struct info temp = people[i];
  people[i] = people[max];
  people[max] = temp;
}

for (i=0; i<k; i++) {
  printf("%s  %d\n", people[i].name, people[i].number);
}

【讨论】:

    【解决方案3】:

    最简单和最通用的方法可能是首先对people 数组进行排序。完成后,只需选择前五个元素。

    【讨论】:

    • 如果您使用现有函数进行排序,这只是最简单的方法...否则,如果从头开始编程,扫描数组并记住前 5 个索引更容易编码。跨度>
    • 不应该使用已有的功能吗?
    【解决方案4】:

    最好的办法是根据个人互换的数字属性对数组people进行排序。

    如果您想继续使用当前方法,请使用 strcpy 函数 iso "=" operator for name

    【讨论】:

      【解决方案5】:

      比起跟踪总共 10 个索引中的 5 个,排序对我来说似乎更好。 您可以使用 qsort 对 10 个元素进行排序,然后选择前 5 个。

       int cmp(void *a,void *b)
       {
         struct info as=*((struct info*)a);
         struct info bs=*((struct info*)b)
         return bs.number-as.number;  //sorting in descending order
       }
      

      然后

          qsort(people,10,sizeof people[0],cmp);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-09
        • 2021-01-25
        • 2022-12-07
        • 2014-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多