2012年1月14日 星期六

[program skill] FUNCTION dominance_count


/* ------------------------------------------------------ */

/* FUNCTION dominance_count :                             */

/*    Given two sorted (increasing) arrays, this function */

/* computes the number of pairs that satisfies f[i]>g[j]. */

/* This function is similar to coincidence_count().       */

/*                                                        */

/* Copyright Ching-Kuang Shene               June/30/1989 */

/* ------------------------------------------------------ */

 

#include <stdio.h>

 

int  dominance_count(int f[], int g[], int m, int n)

{

     int  index_f, index_g;

     int  count;

 

     count = index_f = index_g = 0;

     while (index_f < m && index_g < n){

    printf("\n index_f = %d  index_g = %d \n",index_f,index_g);

          if (f[index_f] <= g[index_g])

               index_f++;

          else

               index_g++, count += m - index_f , printf("\n count = %d \n",count);

     }

     return count;

}

 

 

/* ------------------------------------------------------ */

 

 

 

int main(void)

{

     int  x[] = { 1,3,5,7,9};

     int  nx  = sizeof(x)/sizeof(int);

     printf("sizeof(x) / sizeof(int) = %d /%d , nx = %d",sizeof(x),sizeof(int),nx);

 

     int  y[] = {2,3,4,7,8};

     int  ny  = sizeof(y)/sizeof(int);

 

     int  i;

 

     printf("\nDominance Count of two Increasing Arrays\n");

     printf("\n  #   Array 1   Array 2");

     printf("\n --   -------   -------");

 

     for (i = 0; i < nx; i++)

          printf("\n%3d%10d%10d", i, x[i], y[i]);

     printf("\n\nThere are %d Dominance Pairs. \n",  

            dominance_count(x, y, nx, ny));

    return 0;

}



Result :



sizeof(x) / sizeof(int) = 20 /4 , nx = 5

Dominance Count of two Increasing Arrays



  #   Array 1   Array 2

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

  0         1         2

  1         3         3

  2         5         4

  3         7         7

  4         9         8

 index_f = 0  index_g = 0



 index_f = 1  index_g = 0



 count = 4



 index_f = 1  index_g = 1



 index_f = 2  index_g = 1



 count = 7



 index_f = 2  index_g = 2



 count = 10



 index_f = 2  index_g = 3



 index_f = 3  index_g = 3



 index_f = 4  index_g = 3



 count = 11



 index_f = 4  index_g = 4



 count = 12





There are 12 Dominance Pairs.



 



Cpmpile code :




  • Save file as xxx.c (note : small c not a big C)


  • Compile gcc -Wall GT_COUNT.c -o GT_COUNT


  • Execute ./GT_COUNT

     

0 意見:

張貼留言