• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
指尖上的快乐
博客园    首页    新随笔    联系   管理    订阅  订阅

实现查找数组中第N大的数,数组内容可重复

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 void swap(int *a, int *b)
 5 {
 6     int temp = *a;
 7     *a = *b;
 8     *b = temp;
 9 }
10 
11 
12 /*查找数组中第N大的数,num为数组元素,n为第N大*/
13 int getmax_n(int *p, int num, int n)     
14 {
15     int res = 0;
18     int k = 0;
19     for (int i = 0; (i < n); (p[res] == p[res -1]) ? 1 : i++)
20     {
21         int max = p[k];
22         int maxi = k;
23         for (int j = k + 1; j < num; j++)
24         {
25             if (max < p[j])
26             {
27                 max = p[j];
28                 maxi = j;
29             }
30         }
31         swap(&p[k], &p[maxi]);
32         res = k++;
33     }
34     return p[res];
35 }
36 
37 
38 /*调用方式*/
39 int main()
40 {
41     int a[11] = { 12, 11, 4, 69, 68, 68, 469, 67, 68, 69, 69 };
42     printf("%d\n", getmax_n(a,11,11));
43 
44     system("pause");
45     return 0;
46 }

 

posted @ 2015-07-01 21:08  指尖上的快乐  阅读(366)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3