六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 44|回复: 0

算法:c语言实现第三章 约瑟夫函数

[复制链接]

升级  0.65%

321

主题

321

主题

321

主题

探花

Rank: 6Rank: 6

积分
1013
 楼主| 发表于 2013-1-26 12:26:53 | 显示全部楼层 |阅读模式
root@ubuntu:~/algorithm# cat nodetest.c #include <stdlib.h>#include <stdio.h>typedef struct node* link;struct node { int item; link next;};int main(int argc,char *argv[]){    int i,N=atoi(argv[1]),M=atoi(argv[2]);    link t=malloc(sizeof *t),x=t;    t->item=1;    t->next=t;    for(i=2;i<=N;i++){        x=(x->next=malloc(sizeof *t));        x->item=i;x->next=t;    }    while(x!=x->next){        for(i=1;i<M;i++){            x=x->next;        }        x->next=x->next->next;N--;    }    printf("%d\n",x->item);    }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

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