Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19446
  • 博文数量: 5
  • 博客积分: 110
  • 博客等级: 民兵
  • 技术积分: 80
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-18 20:24
文章分类

全部博文(5)

文章存档

2013年(1)

2012年(4)

我的朋友

分类: C/C++

2012-09-13 16:17:15


点击(此处)折叠或打开

  1. /*
  2.  * =====================================================================================
  3.  *
  4.  * Filename: GreedSelector.c
  5.  *
  6.  * Description: intend to resolve conference schedual problem with Greedy algorithm
  7.  *
  8.  * Version: 1.0
  9.  * Created: 09/13/2012 10:01:13 AM
  10.  * Revision: none
  11.  * Compiler: gcc
  12.  *
  13.  * Author: scar (gao), fevergao@gmail.com
  14.  * Company: zecloudd.Inc
  15.  *
  16.  * =====================================================================================
  17.  */

  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <time.h>

  22. void
  23. swap (int *a, int *b)
  24. {
  25.     int tmp;
  26.     tmp = *a;
  27.     *a = *b;
  28.     *b = tmp;
  29. }

  30. int
  31. partition (int m[], int n[], int low, int high)
  32. {
  33.     int i = low;
  34.     int j = high;
  35.     int pivot = m[low];
  36.     
  37.     while (i < j)
  38.     {
  39.         while (i < j && m[j] >= pivot)
  40.             j--;
  41.         if (i < j)
  42.         {
  43.             swap (&m[i], &m[j]);
  44.             swap (&n[i++], &n[j]);
  45.         }
  46.         while (i < j && m[i] <= pivot)
  47.             i++;
  48.         if (i < j)
  49.         {
  50.             swap (&m[i], &m[j]);
  51.             swap (&n[i], &n[j--]);
  52.         }
  53.     }
  54.     return j;
  55. }

  56. void
  57. Quick_sort (int m[], int n[], int low, int high)
  58. {
  59.     int pivot;
  60.     if (low < high)
  61.     {
  62.         pivot = partition (m, n, low, high);
  63.         Quick_sort (m, n, low, pivot - 1);
  64.         Quick_sort (m, n, pivot + 1, high);
  65.     }
  66. }

  67. void
  68. GreedySelector (int n, int B[], int E[], int A[])
  69. {
  70.     int i, j;
  71.     i = 0;
  72.     j = 1;
  73.     A[0] = 1;
  74.     Quick_sort (E, B, 0, n-1);
  75.     printf ("after sort:\n");
  76.     for (i = 0; i < 11; i++)
  77.     {
  78.         printf ("---B=%d,E=%d,A=%d\n", B[i], E[i], A[i]);
  79.     }
  80.     i = 0;
  81.     j = 1;
  82.     while (j < n)
  83.     {
  84.         if (B[j] >= E[i])
  85.         {
  86.             A[j] = 1;
  87.             i = j;
  88.         } else
  89.             A[j] = 0;
  90.         j++;
  91.     }
  92. }

  93. int
  94. main (int argc, char **argv)
  95. {
  96.     int n = 11;
  97.     int i;
  98.     int B[11] = {0};
  99.     int E[11] = {0};
  100.     int A[11] = {0};
  101.     
  102.     for(i = 0; i < 11; i++)
  103.     {
  104.         printf ("B[%d]:",i);
  105.         scanf ("%d ",&B[i]);
  106.         printf ("E[%d]:",i);
  107.         scanf ("%d ",&E[i]);
  108.     }
  109.     printf ("before select:\n");
  110.     for (i = 0; i < 11; i++)
  111.     {
  112.         printf ("---B=%d,E=%d,A=%d\n", B[i], E[i], A[i]);
  113.     }
  114.     GreedySelector (n, B, E, A);
  115.     printf ("after select:\n");
  116.     for (i = 0; i < 11; i++)
  117.     {
  118.         printf ("---B=%d,E=%d,A=%d\n", B[i], E[i], A[i]);
  119.     }
  120.     return 0;
  121. }

阅读(1108) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:工作相关

给主人留下些什么吧!~~