分享
 
 
 

我的程序:(1)约瑟夫环

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

#include<stdio.h>

#include<stdlib.h>

struct node{

int num;

struct node *next;

}a;

struct node * creat(int n); //返回头指针

int excute(int m,struct node *head);//返回最后剩下的人的编号

main()

{

int n;

int m;

struct node *h = NULL;

printf("input how many people\n");

scanf("%d",&n);

printf("input which people been delete\n");

scanf("%d",&m);

if(n < 1 || m < 1){ /*对特殊情况的讨论*/

printf("wrong input!\n");

return -1;

}else if( n == 1 ){

printf("nobady left!\n");

return 0;

}else if( m == 1 ){

printf("the last number is %d\n",n);

return 0;

}else{

h = creat(n);

printf("\nthe last number is %d\n",excute(m,h));

return 0;

}

}

struct node * creat(int n) //构造循环链表

{

struct node * p = NULL;

struct node * h = NULL;

struct node * q = NULL;

int num = 0;

p = (struct node *)malloc( sizeof(a) );

p->num = ++num;

p->next = NULL;

h = p;

while( --n !=0 ){

q= (struct node *)malloc( sizeof(a) );

q->num = ++num;

p->next = q;

p = q;

}

if(p != NULL)

p->next = h;

return h;

}

int excute(int m,struct node *h)

{

struct node *current=NULL;

struct node * p=NULL;

int i;

current = h;

printf("the out order is:");

while (current->next != current){

for (i = 1; i < m; i++){ /* 查找报数为m的小孩结点 */

p = current;

current = current->next;

}

p->next = current->next; /* 删除报数为m的小孩结点 */

printf("%5d",current->num); //打印出被淘汰的顺序

free(current); /* 释放报数为m的小孩结点空间 */

current = p->next;

}

return current->num;

}

1,先确定数据结构为循环链表,每一个元素为一个结构,包括两个变量。再画一个图,找到解题的思路,写出大致的算法。

2,根据画的图,先不考虑特殊的情况,写出代码。

3,编译,链接,确保无错。

4,加入安全方面的考虑,如非法输入等。

5,加入易用性方面的代码。

6,编译,链接,确保无错。

之后可以参考一下别人的代码,可能会发现很多新的方法和思路。

比如,这个题目也可以这样做:

#include <stdio.h>

#include <stdlib.h>

#include <string.h> //没有使用链表

int main(int argc, char *argv[])

{

int m, n, step=0, index=0, cir=0;

char *out=NULL;

scanf("%d%d", &m, &n);

out = (char*)malloc(m);//可以看作是数组的动态分配,好!!。与memset函数联合使用

if(out == NULL)

return -1;

memset(out, 0, m); //因为malloc不对分配的内存进行初始化,所以这里用memset进行初始化

while(1){

if(step==m)

break;

while(cir<n) {

if(!out[index])

++cir;

index=(index+1)%m;

}

cir=0;

++step;

out[(index+m-1)%m]=1;

printf("\nNo.%d out.", !index?m:index);

}

free(out);

system("PAUSE"); //当有非法输入时,如0,5或4,0时,在屏幕上显示“请按任意键继续”

return 0;

}

也可以这样做:

/**********************************************

********* Josephus problem ************

*********************************************/ //和我的方法差不多

#include <malloc.h>

#include <stdio.h>

struct boy /* struct of boy*/

{

int num;

struct boy *next;

};

int main()

{

int i;

int number; /* 小孩的数目 */

int interval; /* 即报数从1- interval */

struct boy *head, *p, *current;

printf("please input the number of boys:");

scanf("%d", &number);

printf("please input the interval:");

scanf("%d", &interval);

if(number < interval || number <= 0 || interval <= 0)

{

printf("input error!!!\n");

return 0;

}

head = (struct boy *) malloc(sizeof(struct boy));

head->num = 1;

head->next = NULL;

current = head;

printf("Boy numbers:%d,", head->num);

for (i = 2; i <= number; i++)/* 建立单向循环链表 */

{

p = (struct boy *) malloc(sizeof(struct boy));

p->num = i;

p->next = NULL;

current->next = p;

current = p;

printf(" %d,", p->num);

}

printf("\n");

current->next = head;

current = head;

printf("Out queue order:");

while (current->next != current) /*loop to find the winner */

{

for (i = 1; i < interval; i++) /* 查找报数为interval的小孩结点 */

{

p = current;

current = current->next;

}

p->next = current->next; /* 删除报数为interval的小孩结点 */

printf(" %d,", current->num);

free(current); /* 释放报数为interval的小孩结点空间 */

current = p->next;

}

printf("\n");

printf("No.%d boy's won!\n", current->num);

getch();

return 0;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有