| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1876 人关注过本帖
标题:想问个相关约瑟夫问题,
只看楼主 加入收藏
xcdboy
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-9-16
收藏
 问题点数:0 回复次数:4 
想问个相关约瑟夫问题,
今天在课堂里老师留下了个题要我们做

15个人围成一个圈,报数1.2.3,逢报到3的就退出这圈,问最后一个留下的是几号?
1、用下标做
2、换成针做
3、链表
  
这题要怎么搞?

请大家帮帮忙!谢谢哈~~~~~~~~
  
搜索更多相关主题的帖子: 约瑟夫 
2005-09-16 11:41
jackrain
Rank: 1
等 级:新手上路
帖 子:182
专家分:0
注 册:2005-9-4
收藏
得分:0 

1、下标 #include <stdio.h> #include <stdlib.h>

#define N 15 #define M 3

void main() { int person[N], i = 0,number = 1, count = 0; for(i = 0; i < N; i++) person[i] = i+1; i = 0; while(number < N) { if(person[i] != 0) { count++; if(count == M) { person[i] = 0; count = 0; number++; } } i++; if(i == N) i = 0; } printf("The last person is:"); for(i = 0; i < N; i++) if(person[i] != 0) { printf("%d\n",person[i]); break; } system("pause"); } 2、指针 #include <stdio.h> #include <stdlib.h> #define N 15 #define M 3

void main() { int person[N+1], count = 0, number = 1, i = 0; int * p = person; for(i = 0; i < N; i++) *(p+i) = i+1; *(p+i) = -1; while(number < N) { if(*p != 0) { count ++; if(count == M) { *p = 0; count = 0; number ++; } } p++; if( *p == -1) p = person; } p = person; while(*p != -1) { if(*p != 0) { printf("The last person is:%d\n",*p); break; } p++; } system("pause"); } 3、循环链表 #include <stdio.h> #include <stdlib.h> #define N 15 #define M 3

typedef struct node { int index; struct node * next; } Node, *pNode; void initlist(pNode * list); void output(pNode list); void main() { pNode headlist = NULL; initlist(&headlist); output(headlist); system("pause"); } void initlist(pNode * list) { pNode temp,temp1; int i = 0; temp = (pNode)malloc(sizeof(Node)); if(temp == NULL) { printf("Malloc Failure!\n"); exit(-1); } temp1 = temp; while(i < N) { temp->next = (pNode)malloc(sizeof(Node)); if(temp->next == NULL) { printf("Malloc Failure!\n"); exit(-1); } temp = temp->next; temp->index = i + 1; i++; } temp->next = temp1->next; *list = temp1->next; free(temp1); } void output(pNode list) {

pNode temp1,temp2; temp1 = list; temp2 = temp1; int number = 1,count = 0; while(number < N) { count++; if(count == M) { temp2->next = temp1->next; count = 0; free(temp1); temp1 = temp2->next; number ++; continue; } temp2 = temp1; temp1 = temp1->next; } printf("The last person is:%d\n",temp1->index); free(temp1); }


程序和身体一样,健壮,再健壮! 我爱C++
2005-09-17 12:32
独孤逍遥
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2005-9-25
收藏
得分:0 
哦,强啊

物以方圆 义薄云天 何以载物 四海纵横
2005-09-26 15:51
gino
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2005-10-5
收藏
得分:0 
这么厉害啊~~
解答就找你鸟~

2005-10-12 15:36
激情依旧
Rank: 1
等 级:新手上路
威 望:2
帖 子:524
专家分:0
注 册:2005-4-4
收藏
得分:0 

一句话。上机运行去。 #include<iostream> using namespace std; struct List{ int data; List *next; };

void InitList(List *&l){ l=NULL; }

void OrderInsertList(List *&l,int item){ List *pt=new List; pt->data=item; pt->next=NULL; if(l==NULL||item<l->data){ pt->next=l; l=pt; return; } List *p=l,*q=p->next; while(p){ if(item<p->data)break; q=p; p=p->next; } pt->next=p; q->next=pt; }

void InsertList(List *&l,int item){ List *pt=new List; pt->data=item; /*注意pt的next*/ static List *tail=NULL; if(l==NULL){ l=pt; l->next=l; tail=pt; } else{ pt->next = tail->next; tail ->next = pt; tail=pt; } }

int LenList(List *l){ int i=0; List *p=l->next; while(l!=p){ p=p->next; i++; } return i+1; }

void DeleteList(List *&l,int item){ List *del=l->next; List *before=NULL; while(del != l){ if(del->data==item)break; else { before=del;del=del->next; } } if(l->data ==del->data) { before->next = l->next; l = l->next; del->next = NULL; delete del; } else { before->next = del->next; del ->next =NULL; delete del; } }

void OutputList(List *l){/*注意看这里*/ List *p = l->next; cout<<"打印链表:"<<endl; cout<<l->data<<" "; while(p != l) { cout<<p->data<<" "; p = p->next; } cout<<endl; }

void Order(List *&l,int n){ List *p=l; int x=LenList(p); int i=1; while(1){ if(n==i){ cout<<p->data<<"-->"; DeleteList(p,p->data); x--; i=1; } if(x==1)break; i++; p=p->next; } cout<<"王者: "<<p->data<<endl; }

int main(){ List *a; InitList(a); for(int i=1;i<14;i++) InsertList(a,i); cout<<LenList(a)<<endl; OutputList(a); Order(a,3); return 0; }


生是编程人!!!!死是编程鬼!!!!颠峰人生!!!焚尽编程!!! 爱已严重死机!情必须重新启动!情人已和服务器断开连接!网恋也需要重新拨号!-----激情依旧
2005-10-22 00:42
快速回复:想问个相关约瑟夫问题,
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014921 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved