LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: emilio

求算法思想。。。。谢谢各位了!

[复制链接]
发表于 2003-6-27 21:50:10 | 显示全部楼层
ralphlee的做法是确实用数组的.
ralphlee是我的同学,刚学编程,数据结构应该还没学过吧.
但不管怎样,编程求的是创新,是思考,如果只会套算法,那就没有趣味了
所以是值得鼓励的
发表于 2003-6-28 10:10:58 | 显示全部楼层
用数组不便于动态扩展数组空间大小。不过ralphlee编程的技巧比较熟练。鼓励一下。:)
发表于 2003-6-28 14:57:57 | 显示全部楼层

来个perl版的,支持数组动态扩充。

  1. #!/usr/bin/perl -w
  2. #
  3. # Who is the Monkey King?
  4. #

  5. print "Enter the count of the monkey:";
  6. chomp($m = <STDIN>);
  7. print "Enter a number to do the loop:";
  8. chomp($n = <STDIN>);

  9. @monkey = (1..$m);
  10. $j=1;

  11. # do the loop until only one monkey remain.
  12. until ($m == 1) {
  13.     $i=0;   
  14.    
  15.     foreach (@monkey) {
  16.         
  17.         # if the monkey's value is "out" skip the step.
  18.         if ( $monkey[$i] eq "out" ) {
  19.             $i++;   
  20.             next;   
  21.         }      
  22.         
  23.         # if the monkey is the correct one, let it "out" :)
  24.         if ( $j == $n ) {
  25.             $monkey[$i]="out";
  26.             $j=0;   
  27.             $m--;   
  28.         }      
  29.         
  30.         $j++;   
  31.         $i++;   
  32.     }
  33. }

  34. foreach (@monkey) {
  35.     print "No.$_ monkey is the King!\n" unless $_ eq "out";
  36. }
复制代码

lyoo@lsd:~/shellsample/perlscript$ ./monkey.pl
Enter the count of the monkey:10
Enter a number to do the loop:5
No.3 monkey is the King!
发表于 2003-6-28 19:25:12 | 显示全部楼层
楼上的是perl高手,以后请多多指教。
发表于 2003-6-30 20:55:26 | 显示全部楼层
11、图的建立及输出
任务:建立图的存储结构(图的类型可以是有向图、无向图、有向网、无向网,学生可以任选两种类型),能够输入图的顶点和边的信息,并存储到相应存储结构中,而后输出图的邻接矩阵。
要求:
  1. #include <stdio.h>
  2. #define NUM 4
  3. struct head {
  4. int No;
  5. int visited;
  6. struct node *next;
  7. };
  8. struct node {
  9. int No;
  10. int link;
  11. struct node *next;
  12. };
  13. typedef struct head ehead;
  14. typedef struct node enode;
  15. void traversal(ehead *graph,int i);
  16. int matrix[NUM][NUM];
  17. main()
  18. {
  19. int i,j,linkdata;
  20. enode *p,*last;
  21. ehead graph[NUM];
  22. for(i=0;i<NUM;i++)
  23.         for(j=0;j<NUM;j++)
  24.         matrix[NUM][NUM]=0;
  25. printf("Please input the linkdata\n");
  26. for(i=0;i<NUM;i++)
  27. {
  28.         graph[i].No=i;graph[i].next=NULL;graph[i].visited=0;
  29.         for(j=0;j<NUM;j++)
  30.         {
  31.         if(i==j)  continue;
  32.         printf("link(%d,%d):",i+1,j+1);scanf("%d",&linkdata);
  33.         if(linkdata==0)  continue;
  34.         p=(enode *)malloc(sizeof(enode));
  35.         p->link=linkdata;p->next=NULL;p->No=j;
  36.         if(graph[i].next==NULL)  {graph[i].next=p;last=p;}
  37.         else {  last->next=p;   last=p;}
  38.         }
  39. }
  40. traversal(graph,0);
  41. for(i=0;i<NUM;i++)
  42.         {
  43.         for(j=0;j<NUM;j++)
  44.         printf("%5d",matrix[i][j]);
  45.         printf("\n");
  46.         }
  47. }
  48. void traversal(ehead *graph,int i)
  49. {
  50. enode *last;
  51. last=graph[i].next;
  52. printf("%-5d\n",graph[i].No+1);
  53. graph[i].visited=1;
  54. while(last!=NULL)
  55.         {
  56. //      printf("link(%d,%d) :%d",i+1,last->No+1,last->link);
  57.         matrix[i][last->No]=last->link;
  58.         if(graph[last->No].visited==0)
  59.         traversal(graph,last->No);
  60.         else
  61.         last=last->next;
  62.         }
  63. }
  64. [root@localhost tmp]# ./a.out
  65. Please input the linkdata
  66. link(1,2):1
  67. link(1,3):2
  68. link(1,4):3
  69. link(2,1):0
  70. link(2,3):4
  71. link(2,4):0
  72. link(3,1):0
  73. link(3,2):0
  74. link(3,4):5
  75. link(4,1):0
  76. link(4,2):0
  77. link(4,3):0
  78. 1
  79. 2
  80. 3
  81. 4
  82.     0    1    2    3
  83.     0    0    4    0
  84.     0    0    0    5
  85.     0    0    0    0
复制代码
图为:
  1. V1--V2
  2. | \/
  3. | /\
  4. V3--V4
复制代码
其实有向图和无向图是一样的啦!只是在输入边权值的时候自己注意不要双向赋值就行了。代码写得很差。但有些问题我无法解决。不知道要怎么才能把matrix[][]放到main函数里。而且构造图的函数应该独立。有向网的以后再想想。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表