日志档案

发表于 2007-12-29 13:08:59

0

标签: c  算法  

约瑟夫环的算法实现

菜菜一个 ,调试通过。呵呵,快考试了,编个程序玩玩。icon

刚才看了以前做的一个送给熊猫的flash,哎,想熊猫了。太大了传不上来哈。

#include "stdio.h"
#include "alloc.h"
typedef struct node {
                     int data;
                     int mm;
                     struct node * next;
                    }NODE
void main(void)
          {
          int i,j,m,a[7];
          NODE *p,*L;
          int b[7]={3,4,7,2,8,5,1};
          for(i=1;i<=7;i++)
             {
              a[i]=i;
             }
          L="malloc"(sizeof(NODE));
          L->next=NULL;
          p="l"
          for(i=1;i<=7;i++)
             {
              q="malloc"(sizeof(NODE));
              q->data=a[i];
              p->mm=b[i-1];
              p->next=q;
              p="p-">next;
              q->next=NULL;
             }
             p->next=L;/*create the cycle*/
          printf("please input the first \n");  

          scanf("%d",&m)
          printf("\n");
          p="L";
          for(i=1;i<=7;i++)
             {
              j="1";
              while(j<m-1)
                   {
                    p="p-">next;
                    if(p!=L){j++;}
                   }
               if(p->next==L) p="L";
               a[i-1]=p->next->data;
               m="p-">next->mm;
               p->next=p->next->next;
             }   
          for(i=0;i<7;i++)
             {printf("%d *\n",a[i]);}        
           }

系统分类: 自由话题   |   用户分类: C language   |   来源: 原创   |   【推荐给朋友】   |   【添加到收藏夹】

    阅读(446)    回复(1)  

投一票您将和博主都有获奖机会!