文章

38

粉丝

74

获赞

124

访问

26.5k

头像
出现次数最多的数 题解:qsort ,将下标当做结构体属性一起排序
P2001 重庆大学机试题
发布于2024年3月17日 17:27
阅读数 573

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct node{
  4. int num;
  5. int count;
  6. }node;
  7. int n;
  8. struct node p[10009];
  9. int cmp(const void*a,const void*b)
  10. {
  11. struct node a1=*(struct node*)a;
  12. struct node b1=*(struct node*)b;
  13. if(a1.count!=b1.count)return b1.count-a1.count;
  14. else return a1.num-b1.num;
  15. }
  16. int main()
  17. {
  18. scanf("%d",&n);
  19. for(int i=0;i<10009;i++){
  20. p[i].num=i;
  21. p[i].count=0;
  22. }
  23. for(int i=0;i<n;i++){
  24. int a;
  25. scanf("%d",&a);
  26. p[a].count++;
  27. }
  28. qsort(p,10009,sizeof(struct node),cmp);
  29. printf("%d",p[0].num);
  30. return 0;
  31. }

先对次数排序,在对下标排序

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发