⑴ c语言描述怎么用循环队列实现猴子选大王
#include<stdio.h>
#include<stdlib.h>
typedefstructnode//定义链表节点类型
{
intdata;
structnode*next;
}linklist;
intmain()
{
inti,n,k,m,total;
linklist*head,*p,*s,*q;
printf("请输入猴子总数:");//读入问题条件
scanf("%d",&n);
printf("请输入开始计数的猴子数:");
scanf("%d",&k);
printf("请输入数:");
scanf("%d",&m);
head=(linklist*)malloc(sizeof(linklist));//创建循环链表,头节点也存信息
p=head;
p->data=1;
p->next=p;
for(i=2;i<=n;i++)//初始化循环链表
{
s=(linklist*)malloc(sizeof(linklist));
s->data=i;
s->next=p->next;
p->next=s;
p=p->next;
}
p=head;
for(i=1;i<k;i++)//找到第k个节点
{
p=p->next;
}
total=n;//保存节点总数
printf(" 失序:");
q=head;
while(total!=1)//只剩一个节点时停止循环
{
for(i=1;i<m;i++)//报数过程,p指向要删除的节点
{
p=p->next;
}
printf("[%d]",p->data);//打印要删除的节点序号
while(q->next!=p)//q指向p节点的前驱
{
q=q->next;
}
q->next=p->next;//删除p节点
s=p;//保存被删除节点指针
p=p->next;//p指向被删除节点的后继
free(s);//释放被删除的节点
total--;//节点个数减一
}
printf(" 猴子王是第[%d]只 ",p->data);//打印最后剩下的节点序号
free(p);
system("pause");
return0;
}
⑵ 有N只猴子选大王,选举的办法是:排成一排,从头到尾报数,报到3的倍数(
这个是C语言编写,
题目是输入两个正整数 n 和 m( (1<m<n<=50)),有 n 个人围成一圈,按顺序从 1 到 n 编号。从第一个人开始报数,报数 m 的人退出圈子,下一个人从 1 开始重新报数,报数 m 的人退出圈子。如此循环,直到留下最后一个人。请按退出顺序输出退出圈子的人的编号,以及最后一个人的编号。
提示:将每个人的编号存入数组,从第一个人开始报数,输出报数 m 的人的编号,并将该编号清除为0,重复这样的操作直至只剩下一个不为0的数,该数就是最后一个人的编号。
输出使用语句:printf("No%d: %d\n", no, *p);
可以计算无限次,每次都有时间
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#define MAX 100000
typedef struct node
{
int data;
struct node *next;
}Node;
typedef Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
clock_t start, stop;
void init(List *L) //initilize the list L
{
*L=(Node*)malloc(sizeof(Node));
(*L)->data=-1;
(*L)->next=NULL;
}
void insert(Position p,int x){// insert the elemnet x after the position of p
PtrToNode q,r;
q=(Node*)malloc(sizeof(Node));
q->data=x;
r=p->next;
q->next=r;
p->next=q;
}
void delect(Position p){// delect the element after p
PtrToNode tmp;
tmp=p->next;
p->next=tmp->next;
free(tmp);
}
void MakeList(List *L,int n){//make the list
int i;
Position p=L;
for(i=1;i<=n;i++){
insert(p,i);
p=p->next;
}
}
void main()
{
int i,j,n,m;
Position p,q,r;
List L;
double ration;
while(scanf("%d%d",&n,&m)!=EOF){//input the number to slect
if(n>MAX){
printf("The number is too large!\n");
continue;
}
init(&L);//initilze the list
MakeList(L,n);//make the list
start = clock();
q=p=L->next;// p and q are the first position of the list
for(i=0;i<n;i++){
for(j=1;j<m-1;j++){//find the porper position of p
p=p->next;
if(p==NULL)p=q;
}
r=p->next; // r is the position of the element we select
if(r==NULL)r=q;
if(i!=n-1)printf("No%d: %d\n", i+1, r->data);//print the element
else printf("Last No is: %d\n",r->data);
if(p->next==NULL){
delect(L);
q=L->next;//delect the position of p->next
}
else delect(p);
p=p->next;
if(p==NULL)p=q;
}
stop = clock();
ration = ((double)(stop - start))/CLK_TCK;
printf("The time is %f\n",ration);
}
}
⑶ C语言 猴子选大王 高手纠错啊。。。。。。
#include<stdio.h>
#include<malloc.h>
int main()
{
struct king{
int data;
struct king *next;
};
int i,m,n;
scanf("%d%d",&n,&m);
struct king *p,*head,*t,*tmp;
head=(struct king*)malloc(sizeof(struct king));
head->next=NULL;
head->data=1;
p=head; //循环链表
for (i=2;i<=n;i++)
{
t=(struct king*)malloc(sizeof(struct king));
t->data=i;
t->next=NULL;
p->next=t;
p=p->next;
}
p->next=head;
p=head; //删除链表
while(p->next!=p)
{
for(i=1;i<m-1;i++)
p=p->next;
tmp=p->next;
p->next=p->next->next;
p=tmp->next;
free(tmp);
}
printf("%d",p->data);
getchar();
getchar();
return 0;
}
//
p=tmp->next;
你那个是指向下下个节点了
⑷ 【急求】C语言 猴子选大王的问题 应该很简单
以前做的约瑟夫环,改改就能用
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
struct MAN {
int RANK;
int CODE;
struct MAN * nextptr;
};
typedef struct MAN MAN;
typedef MAN * MANPTR;
void tittle();/**/
void begin();/**/
void printlink(MANPTR startptr);/**/
bool isempty(MANPTR startptr);/**/
void setfree(MANPTR *startptr );/**/
void insert(int sequence,int number,MANPTR *startptr);/**/
int main()
{
MANPTR startptr = NULL;
int code;
char choice;
char elsechar;
char elsechar2;
int number,sequence,i;
MANPTR currentptr,preptr,timeptr;
tittle();
begin();
scanf("%c%c",&choice,&elsechar);
if(elsechar!='\n'){
printf("\tTry to enter again\n");
elsechar2=getchar();
while(elsechar2!='\n'){elsechar2=getchar();}
}/*check cahr*/
while(choice!='3'||elsechar!='\n'){
if(elsechar=='\n'){
switch(choice){
/*创建游戏*********************************************************** 添加链表--->常规*/
case '1':
setfree(&startptr);
sequence=1;
printf("Enter intiger,and enter -1 to end \n");
scanf("%d",&number);
while(number!=-1)
{
insert(sequence,number,&startptr);
sequence++;
scanf("%d",&number);
}
getM:
printf("Enter the value of M : \n");
scanf("%d",&code);
if(code<=0){
printf("\n\tM should lager than zero.\n");
goto getM;
}
printlink(startptr);
getchar();
break;
/*******************************************************************************************/
/*开始游戏****** 链表最后--->连接到start--->||start开始数,运行||--->断开链表*/
case '2':
if(isempty(startptr)){
printf("\n\tGame has not be created!\n");
break;
}
printf("The order to quit is : \n");
preptr=NULL;
currentptr=startptr;
while(currentptr!=NULL){
preptr=currentptr;
currentptr=currentptr->nextptr;
}
preptr->nextptr=startptr;
//preptr=NULL;
currentptr=startptr;
i=0;
while(i==0){
while(code>1){
code--;
preptr=currentptr;
currentptr=currentptr->nextptr;
}
if(preptr==currentptr->nextptr){i=1;}
timeptr=currentptr;
code=currentptr->CODE;
printf("%d ",currentptr->RANK);
preptr->nextptr=currentptr->nextptr;
currentptr=currentptr->nextptr;
free(timeptr);
}
printf("%d",preptr->RANK);
/* currentptr=startptr;
while(currentptr->nextptr->RANK != 1){currentptr=currentptr->nextptr;}
currentptr->nextptr=NULL;*/
//printlink(startptr);
//setfree(&startptr);
//printlink(startptr);
break;
/*********************************************************************************************/
default:
printf("\tTry to enter again\n");
break;
}
}
begin();
scanf("%c%c",&choice,&elsechar);
if(elsechar!='\n'){
printf("\tTry to enter again\n");
elsechar2=getchar();
while(elsechar2!='\n'){elsechar2=getchar();}
}/*check char*/
}
printf("\tEnd");
getchar();
return 0;
}
void tittle()
{
printf("\n*************************\n");
printf("*\t约瑟夫环游戏\t*\n");
printf("*************************\n");
}
void begin()
{
printf("\n1.创建游戏。\n2.开始游戏。\n3.结束游戏。\n");
}
bool isempty(MANPTR startptr)
{
return startptr == NULL;
}
void printlink(MANPTR startptr)
{
if(startptr==NULL){printf("The game has not be created");}
else{
printf("The order is : \n");
while(startptr != NULL){
printf("%d (%d) ---> ",startptr->CODE,startptr->RANK);
startptr = startptr->nextptr;
}
printf("StartOne\n\n");
}
}
void setfree(MANPTR *startptr )
{
MANPTR timeptr;
while(*startptr != NULL){
timeptr=*startptr;
*startptr=(*startptr)->nextptr;
free(timeptr);
}
}/*MANPTR *startptr */
void insert(int sequence,int number,MANPTR *startptr)
{
MANPTR newptr;
MANPTR preptr;
MANPTR currentptr;
newptr = (MANPTR)malloc(sizeof(MAN));
if(newptr!=NULL){
newptr->CODE = number;
newptr->RANK = sequence;
newptr->nextptr=NULL;
//printf("%d",number);
preptr=NULL;
currentptr=*startptr;
while(currentptr!=NULL){
preptr=currentptr;
currentptr=currentptr->nextptr;
}
if(preptr==NULL){*startptr=newptr;}
else{preptr->nextptr=newptr;}
}
else{printf("No memory available!When insert %c",number);}
}
⑸ 清华大学出版社《c语言从入门到精通实例版》 和《 c语言从入门到精通》 内容上有什么区别
实例版注重从实例中总结编程经验,后者则强调编程原理的理解
《C语言从入门到精通》以零基础讲解为宗旨,用实例引导读者深入学习,采取“基础知识→核心技术→趣味题解→项目实战”的讲解模式,深入浅出地讲解C语言的各项技术及实战技能。《C语言从入门到精通》第1篇【基础知识】主要讲解步入C的世界、常量与变量、数据类型、运算符和表达式、程序控制结构和语句、输入和输出、数组与字符串、算法与流程图等;第2篇【核心技术】主要讲解C语言中的函数、函数中的变量、指针、指针进阶、文件、编译与预处理指令、库函数、位运算、结构体和联合体、数据结构等;第3篇【趣味题解】主要讲解哥德巴赫猜想、猴子选大王游戏、迷宫求解、背包问题求解、火车车厢重排、哈夫曼编码的实现、8皇后问题的实现、商人过河游戏、K阶斐波那契序列的实现、最短路径的实现等经典数据结构问题的解决;第4篇【项目实战】主要讲解实战前的项目规划以及5个项目的实战开发,包括通讯录、图书管理系统、简易网络通信系统、学生成绩管理系统、酒店管理系统等;第5篇【王牌资源】在DVD光盘中赠送了丰富的资源,诸如C语言标准库函数查询手册、C语言常用查询手册、C源码大放送、《C语言从入门到精通》【练一练】答案、C程序员职业规划、全国计算机等级考试二级C考试大纲及应试技巧、C程序员面试技巧、C常见面试题、C常见错误及解决方案、C开发经验及技巧大汇总等。
另外光盘中还包含37小时的全程同步视频教学录像及7小时的指导录像(包括《C语言从入门到精通)》各章上机指导录像及所有范例运行指导录像)。
《C语言从入门到精通》适合任何想学习C语言的人员,无论您是否从事计算机相关行业、是否接触过C语言,通过学习,均可快速掌握C语言的开发方法和技巧。《C语言从入门到精通(实例版)》从初学者的角度出发,通过通俗易懂的语言,丰富多彩的实例,详细介绍了使用Visual C++ 6.0(部分使用Turbo C)进行C语言应用程序开发应该掌握的各方面技术。全书共分14章,包括初识C语言、C语言基础、顺序与选择结构程序设计、循环控制、数组、函数、指针、结构体与共用体、算法、位运算、预处理、文件、图形图像、商品信息管理系统。书中所有知识都结合具体实例进行介绍,涉及的程序代码给出了详细的注释,可以使读者轻松领会C语言应用程序开发的精髓,快速提高开发技能。另外,本书除了纸质内容之外,配书光盘中还给出了海量开发资源库,主要内容如下:
语音视频讲解:总时长17小时,共193段 实例资源库:881个实例及源码详细分析
模块资源库:15个经典模块开发过程完整展现 项目案例资源库:15个企业项目开发过程完整展现
测试题库系统:616道能力测试题目 面试资源库:371个企业面试真题
PPT电子教案
⑹ C语言程序设计,猴子选大王
#include<stdio.h>
#include<stdlib.h>
main()
{ int a[50];
int i,j,M,N,t=0;
printf("input two number.\n");
scanf("%d %d",&N,&M);
for(i=0;i<N;i++)
a[i]=i+1;
for(j=1,i=0;;j++,i++)
{
if(i==N)i=0;
if(a[i]==0){j--;continue;}
if(j%M==0){a[i]=0;t++;}
if(N-t==1)break;
}
for(i=0;i<N;i++)
if(a[i]!=0) printf("猴王是第%d个.\n",a[i]);
system("pause");
}
试试...