c语言解决约瑟夫问题(C语言解决约瑟夫死亡游戏问题,用calloc函数和数组,要求简洁明了)

本文目录
- C语言解决约瑟夫死亡游戏问题,用calloc函数和数组,要求简洁明了
- C语言中用数组解约瑟夫问题
- C语言约瑟夫问题
- c语言数组指针的约瑟夫杯问题 求助大佬
- 约瑟夫环问题怎么解决啊请用C语言写代码,谢谢!
- 关于循环队列实现约瑟夫环(C语言)的问题
C语言解决约瑟夫死亡游戏问题,用calloc函数和数组,要求简洁明了
bool a表示第i人还活着
int n,m,i=0,s=0,t=0;
cin》》n》》m; //人数n,报数m
do
{
i=i%n+1; //数组模拟环状
if(a)t++; //第i个人还活着则报数
if(t==m) //当前报的数是m
{
s++; //死亡人数+1
cout《《i《《’\t’; //输出被杀人编号
a=0; //此处人已死,设置为0
t=0; //计数器清零
}
}while(s!=n); //直到所有人都被杀死为止
C语言中用数组解约瑟夫问题
#include《stdio.h》
#include《stdlib.h》
void main()
{
int y(int n,int m);
int p,q,r;
printf("请输入参选人的个数p和开始的位置q:\n");
scanf("%d%d",&p,&q);
r=y(p,q);
printf("最后那个人的初始位置是:%d\n",r);
}
int y(int n,int m)
{
int i,j=0,s=0,l;
int *a=(int *)malloc(sizeof(int));
int *b=(int *)malloc(sizeof(int));
for(i=0;i《n;i++)
{
a=i+1;
}
a=-1;
for(i=0;j!=n;i++)
{
if(a==-1)
i=0;
if(a!=-1)
s++;
if(s==m)
{
b;
a=0;
j++;
s=0;
}
}
for(i=0;i《n;i++)
{
printf("%5d",b);
}
printf("\n");
l=b;
return l;
}
扩展资料:
大体思路如下:
①、read(a)
②、b:=1,c:=1{b为某一组的元素个数,c为累计所加到的数}
③、while c《a do (b:=b*2,c:=b+c){超过目标时停止加数}
⑥、c:=c-b{退到前一组}
⑦、x:=a-c{算出目标为所在组的第几个元素}
⑧、ans:=x*2-1{求出该元素}
⑨、write(ans)
C语言约瑟夫问题
约瑟夫问题:
#include《iostream.h》
struct
node
{
int
data;
node
*pnext;
};
void
main()
{
int
n,k,m,i;
node
*p,*q,*head;
cout《《"输入n的值:";
cin》》n;
cout《《"输入起始报数人号码k的值:";
cin》》k;
cout《《"输入
数到m出列的m的值:";
cin》》m;
head=(node*)new
node;
//确定头结点
p=head;
for(i=1;i《=n-1;i++)
//赋初值
{
p-》data=i;
p-》pnext=(node*)new
node;
//为下一个新建内存
p=p-》pnext;
}
p-》data=n;
//最后一个单独处理
p-》pnext=head;
//指向头,形成循环链表
p=head;
while(p-》data!=(p-》pnext)-》data)
//p-》data==(p-》pnext)-》data表示只剩下一个结点的
{
while(p-》data
!=k)
//寻找编号为k的结点
p=p-》pnext;
if(m==1)
{
for(i=1;i《=n;i++)
{
cout《《p-》data《《’\\t’
;
p=p-》pnext
;
}
cout《《’\
’;
return;
}
else
for(i=1;i《m-1;i++)
//开始报数
{p=p-》pnext;}
//找到报m-1的结点
q=p-》pnext;
//q为报m的结点
cout《《q-》data《《"\\t";
//输出报m的结点的值
k=(q-》pnext)-》data;
//k为下一个报数的起点
p-》pnext=q-》pnext;
//删除报m的结点
}
cout《《p-》data《《’\
’;
//输出最后一个结点的值
}
c语言数组指针的约瑟夫杯问题 求助大佬
char*p=NULL; p = malloc(100); 这样报 error C2106: “=”: 左操作数必须为左值 error C20…
约瑟夫环问题怎么解决啊请用C语言写代码,谢谢!
#include"MyNode.h" //文件1
Node::Node( )
{
next = NULL;
}
Node::Node(Node_entry item, Node *add_on)
{
entry = item;
next = add_on;
}
--------------
#include《iostream.h》 //文件2
typedef int Node_entry;
struct Node {
// data members
Node_entry entry;
Node *next;
// constructors
Node( );
Node(Node_entry item, Node *add_on = NULL);
};
---------------------
#include"MyNode.h" //主程序
#include《iostream.h》
void main(){
int n,m;
Node *p=NULL, *p_head=NULL ,*p_tmp=NULL;
cout《《"This is a YSF(n,m) problem."《《endl;
//n people, m count, both n and m should 》= 1
do{
cout《《"Please input the n(n》1).";
cin》》n;
}while(n《1);
do{
cout《《"Please input the m(m》1).";
cin》》m;
}while(m《1);
//construct the circular link structure
for(int i=n;i》=1;i--){
p_head = new Node(i,p);
p=p_head;
}
//get to the tail, which is n.
while(p-》next)p=p-》next;
//connect to a circular
p-》next=p_head;
//go with the circular
for(i=1;i《n;i++){ //do n-1 times to get rid of n-1 elements.
for(int k=1;k《m;k++)p=p-》next;
p_tmp=p-》next;
p-》next=p_tmp-》next;
cout《《endl《《"Get rid of:"《《p_tmp-》entry;
delete p_tmp;
}
//Print the left element
cout《《endl《《"The left element is:"《《p-》entry《《endl;
}
我这个学期也搞数据结构
程序可以运行的
关于循环队列实现约瑟夫环(C语言)的问题
首先,p是指向头结点的,或者说第一个结点,然后:
p-》next = p; //p-》next也指向头结点,也就是头结点的next指向自己,那么一开始只有一个结点时这就已经是一个循环链表了,后面再插入结点时只要尾结点的next指向头结点,就能保持它仍然是循环链表。
node *r = p;//用r来指向尾结点,r=p,因为p-》next=p,所以也就是r-》next==p,尾结点的next指向头结点。
for循环中:
q-》next = r-》next;//把新增结点的next指向头结点,其实等价于q-》next=p,因为r-》next==p,尾结点的next是指向头结点的。
r-》next = q;//把原先的尾结点的next指向新增结点。
r = q;//把尾结点改为新增结点,因为上面已经将新增结点的next指向头结点,所以这个操作过后,尾结点的next仍然指向头结点,也就保持了链表仍然是循环链表。

更多文章:
androidapp源码免费下载(如何实现APK的反编译得到APK的源码)
2026年9月22日 18:00
service pack 3(操作系统版本升级(SP) Service Pack 3当中的“Service Pack 3”是什么意思)
2026年9月22日 10:20
html代码怎么写大佬教程(html网页的题来个大佬,写代码,题目在图上)
2026年9月22日 10:10
结构体内又一个struct(c++ 在结构体中再嵌入一个结构体如何调用)
2026年9月22日 09:40
cocos creator中文(cocoscreator和cocoscreator3d的区别)
2026年9月22日 02:30




