zoukankan      html  css  js  c++  java
  • C语言实现排名算法和排位算法

      1 #include "stdio.h"
      2 int search_second_max(int array[], int n,int m)
      3  {
      4      int max1;
      5      int i,num;
      6      num=1;//默认第一名 
      7      if(m>n) return 0; 
      8      max1 = array[m];
      9      for (i = 0; i < n; i++)
     10      {
     11          if (array[i]>max1)
     12          {
     13              num++;      
     14          }
     15 
     16      }
     17      return num;
     18 }
     19 
     20 
     21 void sort(int *a, int l)//a为数组地址,l为数组长度。
     22 {
     23     int i, j;
     24     int v;
     25     //排序主体
     26     for(i = 0; i < l - 1; i ++)
     27         for(j = i+1; j < l; j ++)
     28         {
     29             if(a[i] > a[j])//如前面的比后面的大,则交换。
     30             {
     31                 v = a[i];
     32                 a[i] = a[j];
     33                 a[j] = v;
     34             }
     35         }
     36 }
     37 
     38 //排名 
     39 void  PaiMing(int *a, int *b,int *c,int N)
     40 {
     41     int i, j;
     42     int v;
     43     int arrayindex[8][2];
     44     
     45     for(i=0;i<8;i++)
     46     {
     47        arrayindex[i][0]=a[i];
     48        arrayindex[i][1]=i;
     49     }
     50     //排序主体从小到大 
     51     for(i = 0; i < N - 1; i ++)
     52         for(j = i+1; j < N; j ++)
     53         {
     54             if(arrayindex[i][0] > arrayindex[j][0])//如前面的比后面的大,则交换。
     55             {
     56                 v = arrayindex[i][0];
     57                 arrayindex[i][0] = arrayindex[j][0];
     58                 arrayindex[j][0] = v;
     59                 
     60                 v = arrayindex[i][1];
     61                 arrayindex[i][1] = arrayindex[j][1];
     62                 arrayindex[j][1] = v;
     63                 
     64             }
     65         }    
     66         
     67        //排名次 
     68        for(i=0,j=1;i<N-1;i++)
     69        {
     70              
     71                if(arrayindex[i][0]!=arrayindex[i+1][0])
     72           {
     73                b[i]=j;
     74                j++;
     75                b[i+1]=j;
     76           }else
     77           {
     78                b[i]=j;
     79                b[i+1]=j;
     80           }
     81        } 
     82        
     83        for(i=0;i<N;i++)
     84        {
     85              a[i]=arrayindex[i][0];
     86              c[i]=arrayindex[i][1];
     87        }
     88     
     89 }
     90 
     91  
     92 int main(void)
     93 {
     94    int a[8]={7,2,6,3,3,7,1,8};    
     95    int num;
     96    int i;
     97    int b[8];
     98    int c[8]; 
     99 
    100    printf("---------------------------------------
    ");
    101    
    102    PaiMing(a,b,c,8); 
    103    for(i=0;i<8;i++)
    104    {
    105         printf("a[%d]=%d b[%d]=%d c[%d]=%d 
    ",i,a[i],i,b[i],i,c[i]);
    106    } 
    107    printf("
    ");
    108    printf("----------------------------------------
    ");
    109         
    110 
    111 
    112    
    113 }

    结果输出

    ---------------------------------------
    a[0]=1 b[0]=1 c[0]=6
    a[1]=2 b[1]=2 c[1]=1
    a[2]=3 b[2]=3 c[2]=4
    a[3]=3 b[3]=3 c[3]=3
    a[4]=6 b[4]=4 c[4]=2
    a[5]=7 b[5]=5 c[5]=5
    a[6]=7 b[6]=5 c[6]=0
    a[7]=8 b[7]=6 c[7]=7

    ----------------------------------------

    --------------------------------
    Process exited after 0.8098 seconds with return value 0
    请按任意键继续. . .

  • 相关阅读:
    Hammer.js手势库 安卓4.0.4上的问题
    大前端晋级系列之-单一职责原则
    大前端晋级系列之-策略模式
    为什么MVC不是一种设计模式
    解读sencha touch移动框架的核心架构(二)
    解读sencha touch移动框架的核心架构(一)
    大型 JavaScript 应用架构中的模式
    jQuery2.0.3源码分析系列之(29) 窗口尺寸
    jQuery2.0.3源码分析系列(28) 元素大小
    开放封闭原则(Open Closed Principle)
  • 原文地址:https://www.cnblogs.com/huangyangquan/p/8391097.html
Copyright © 2011-2022 走看看