當前位置:首頁 » 編程語言 » 猴子大王C語言
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

猴子大王C語言

發布時間: 2022-08-30 16:56:04

㈠ 【急求】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語言編程)

這是典型的約瑟夫環的問題

#include <stdio.h>
int main(void)
{
int n=100, m=3, i, s=0;
for (i=2; i<=n; i++)
s=(s+m)%i;
printf ("最後留下的是原來第%d號\n", s+1);
}

㈢ 猴子選大王,C語言描述 請相信解釋我的代碼!

給你原文做了注釋。還不懂hi我
#include <stdio.h>
#include<malloc.h>
typedef struct LNode
{
int num;
struct LNode *next;
}LNode, *LinkList; //定義結點
LinkList InitList(LinkList L,int n) //初始化循環鏈表
{
LinkList p,q;
int i;
L = (LinkList)malloc(sizeof(LNode)); //頭結點
L->num = 1; //一號猴子
q=L;
for(i = 2; i <= n; i++) //從二號猴子開始生成結點
{
p = (LinkList)malloc(sizeof(LNode));
p->num = i;
q->next=p;
q=p;
}
q->next = L; //使鏈表循環起來
return L;
}
void ListDelete_L(LinkList L,int n)
{
LinkList p,q;
int j=1; //j為計數器
p=L;

while(p->next!=p) //p->next=p時是只剩一個結點。
{
while(j!=n-1) //當j=n-1時應該將該結點的下一個結點刪除。當就j!=n-1時就應該指針向後移,同時計數器加一
{
p=p->next;
j++;
}
q=p->next; //q即為被點到的猴子
p->next=p->next->next; //刪除q結點
printf("%d\n",q->num);
free(q);//釋放
j=0; //計數器清零,重新開始計數
}
printf("%d",p->num);//此時的結點就是大王
free(p);
}
int main()
{
LinkList L=NULL;
int n,m,e=0;
printf("請輸入猴子個數:");
scanf("%d",&m);
printf("請輸入n值:");
scanf("%d",&n);
if(m<n){printf("m應該 大於n請重新輸入");return 0 ;
}
L=InitList(L,m);
printf("出列的順序為:");
ListDelete_L( L,n);
return 0;
}

㈣ C語言猴子選大王問題,求指出錯誤

把while(n<17)改為while(n<16),同時把
if(i==16)
i=0;
i++;改為
i++;
if(i==17)
i=0;

㈤ 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");
}
試試...

㈥ C語言猴子選大王的問題

因為你讓猴子的編號從0開始的。
head->num=0; ——雖然這一語句被注釋掉了,的num的值還是有的(隨機),但只要不是0(且它又沒有當上大王),就不影響運行。

㈦ C語言編的約瑟夫問題(猴子選大王)求大神看哪裡錯了

for(k=0;k<n;k++){
if(sz[k]==1){
s=s+1;
if(s==m){
sz[k]=0,s=0,b=b+1;}}
if(k==n-1){
k=0;}
if(b==n-1){break;}
}
改成:
k=0;
while(b<n-1)
{
if(sz[k]==1)
{
s=s+1;
if(s==m){sz[k]=0;s=0;b=b+1;}
}
k++;
if(k==n-1){k=0;}
}

㈧ 求c語言的「猴子選大王」代碼

#include"stdio.h"
#include"stdlib.h"

typedef struct lnode{
int number;//猴子序列號
struct lnode *next;
}*linklist;

void creatlist(linklist &l,int number){
int i;
linklist p;
p=(linklist)malloc(sizeof(lnode));
l=(linklist)malloc(sizeof(lnode));
p->number=1;
l->next=p;
for(i=2;i<=number;i++){
p->next=(linklist)malloc(sizeof(lnode));
p=p->next;
p->number=i;
}
p->next=l->next;
}

int choice(){
linklist l,p;
int n;/*刪除序號,哪個猴子數到該序號,就將哪個猴子剔除*/
int m;/*猴子總數,共有多少猴子玩這個游戲*/
int i;/*循環控制變數*/
int j=1;
printf("請輸入猴子的數目(m):");
scanf("%d",&m);
creatlist(l,m);
p=l;
printf("請輸入刪除序號(n):");
scanf("%d",&n);
for(i=1;i<m;i++){
while(j%n){
p=p->next;
j++;
}
p->next=p->next->next;
j++;
}
return p->next->number;
}

void main(){
printf("猴王為:%d號猴子!\n",choice());
getchar();
getchar();
}

//注意:上述程序應在c++環境中運行