Chinaunix首页 | 论坛 | 博客
  • 博客访问: 151386
  • 博文数量: 40
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 908
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-03 11:03
个人简介

学习linux

文章分类
文章存档

2014年(7)

2013年(33)

我的朋友

分类: C/C++

2014-04-08 17:34:53

问题:n个人要过桥,但是只有一个手电筒,并且同时只有两个人过去。每个人的过桥时间不同,取决于时间长的那个人。
            现在要求出最短的时间让所有人都过去?

例子:
4个人时间为 1  2   5   10
最短时间为17,方法为:1  2  过  1  回,5  10  过 2  回,1  2  过

我写的程序如下:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>


  3. #define MIN    0
  4. #define MAX    100

  5. static int *time;
  6. static int timenum;

  7. static int *inpeople;
  8. static int inpeoples;
  9. static int *outpeople;
  10. static int outpeoples;

  11. int main(void)
  12. {
  13.     int i;
  14.     int sumtime = 0;

  15.     if(scanf("%d",&timenum) == EOF) {
  16.         printf("Wrong\n");
  17.         goto out;
  18.     }
  19.     if(timenum == 0) {
  20.         printf("Wrong\n");
  21.         goto out;
  22.     }
  23.     else if(timenum == 1) {
  24.         if(scanf("%d",&sumtime) == EOF) {
  25.             printf("Wrong\n");
  26.         }
  27.         goto out;
  28.     }

  29.     // timenum >= 2
  30.     if(input() != 0) {
  31.         printf("Wrong input\n");
  32.         goto out;
  33.     }

  34.     sumtime = bridge();

  35. out:
  36.     printf("timenum is %d sumtime is %d\n",timenum,sumtime);
  37.     return 0;
  38. }

  39. int input(void)
  40. {
  41.     int i = 0;
  42.     
  43.     time = malloc(timenum*sizeof(int));
  44.     inpeople = malloc(timenum*sizeof(int));
  45.     outpeople = malloc(timenum*sizeof(int));
  46.     if(time == NULL || inpeople == NULL || outpeople == NULL) {
  47.         printf("Wrong no mem\n");
  48.         return -1;
  49.     }

  50.     while(i < timenum && scanf("%d",&time[i]) != EOF) {
  51.         inpeople[i] = 1;
  52.         outpeople[i] = 0;
  53.         i++;
  54.     }
  55.     inpeoples = timenum;
  56.     outpeoples = 0;
  57.     if(i != timenum) {
  58.         printf("Wrong i= %d\n",i);
  59.         return -1;
  60.     }

  61.     return 0;
  62. }

  63. void min(int A[],int *x)
  64. {
  65.     int i;
  66.     int ret = 0;
  67.     *x = MAX;
  68.     for(i=0;i<timenum;i++) {
  69.         if(A[i] == 1 && time[i] < *x) {
  70.             *x = time[i];
  71.             ret = i;
  72.         }
  73.     }
  74.     A[ret] = 0;
  75. }

  76. void max(int A[],int *x)
  77. {
  78.     int i;
  79.     int ret = 0;
  80.     *x = MIN;
  81.     for(i=0;i<timenum;i++) {
  82.         if(A[i] == 1 && time[i] > *x) {
  83.             *x = time[i];
  84.             ret = i;
  85.         }
  86.     }
  87.     A[ret] = 0;
  88. }

  89. void insert(int A[],int x)
  90. {
  91.     int i;
  92.     for(i=0;i<timenum;i++) {
  93.         if(time[i] == x)
  94.             break;
  95.     }
  96.     A[i] = 1;
  97. }

  98. int bridge(void)
  99. {
  100.     int sum = 0;
  101.     int times = 0;
  102.     int x,y;

  103.     while(inpeoples > 0) {
  104.         if(times % 2 == 0) {
  105.             min(inpeople,&x);
  106.             min(inpeople,&y);
  107.         }
  108.         else {
  109.             max(inpeople,&y);
  110.             max(inpeople,&x);
  111.         }
  112.         inpeoples -= 2;
  113.         printf("%d and %d go times is %d\n",x,y,times);

  114.         sum += y;

  115.         insert(outpeople,x);
  116.         insert(outpeople,y);
  117.         outpeoples += 2;

  118.         if(outpeoples == timenum)
  119.             break;

  120.         min(outpeople,&x);
  121.         outpeoples--;
  122.         printf("%d back times is %d\n",x,times);

  123.         sum +=x;
  124.         
  125.         insert(inpeople,x);
  126.         inpeoples++;

  127.         times++;
  128.     }
  129.     return sum;
  130. }

输入测试一:

点击(此处)折叠或打开

  1. 4
  2. 1
  3. 6
  4. 8
  5. 12
打印结果一:

点击(此处)折叠或打开

  1. root@archer:/work/user-lib/bridge# ./main < input
  2. 1 and 6 go times is 0
  3. 1 back times is 0
  4. 8 and 12 go times is 1
  5. 6 back times is 1
  6. 1 and 6 go times is 2
  7. timenum is 4 sumtime is 31

输入测试二:

点击(此处)折叠或打开

  1. 12
  2. 1
  3. 6
  4. 8
  5. 12
  6. 15
  7. 20
  8. 33
  9. 42
  10. 65
  11. 78
  12. 83
  13. 90
打印结果二:

点击(此处)折叠或打开

  1. root@archer:/work/user-lib/bridge# ./main < input
  2. 1 and 6 go times is 0
  3. 1 back times is 0
  4. 83 and 90 go times is 1
  5. 6 back times is 1
  6. 1 and 6 go times is 2
  7. 1 back times is 2
  8. 65 and 78 go times is 3
  9. 6 back times is 3
  10. 1 and 6 go times is 4
  11. 1 back times is 4
  12. 33 and 42 go times is 5
  13. 6 back times is 5
  14. 1 and 6 go times is 6
  15. 1 back times is 6
  16. 15 and 20 go times is 7
  17. 6 back times is 7
  18. 1 and 6 go times is 8
  19. 1 back times is 8
  20. 8 and 12 go times is 9
  21. 6 back times is 9
  22. 1 and 6 go times is 10
  23. timenum is 12 sumtime is 313

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

上一篇:四种基本的排序算法练习

下一篇:没有了

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