| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1943 人关注过本帖
标题:猴子报大王的程序怎么编啊
只看楼主 加入收藏
kaifeng05
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2005-5-11
收藏
得分:0 
是呀!C就是要多想,看看他们多会呀,要放弃老师呀

学.....佛......有恒
2005-05-16 21:21
fengfeng0222
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-5-26
收藏
得分:0 
#include<stdio.h>
#define N 20
main()
{
  int i,j=0,k=0,n,men[N]={0}; //初始在队列的为0
  printf("input n;\n");
  scanf("%d",&n);
    for(i=0;;i++)
      {
         if(men[i%n]==0)
           j++;
          if(j==1)
            {
              men[i%n]=1;    //以1标记为退出的
              k++;            //记退出个数,当k=n-1时跳出循环
              j=0;
            }
         if(k==n-1)break;
       }
    for(i=0;i<n;i++)
     if(men[i]==0)
       printf("%d left!\n",i+1);  //输出最后没有退出的一个位置
    getch();
}
2005-05-26 21:17
Rank: 1
等 级:新手上路
帖 子:255
专家分:0
注 册:2005-4-25
收藏
得分:0 
main()
{
static int a[100];
int i,n,k,m,temp;
scanf("%d%d",&k,&m);

for(i=0;i<k;i++)a[i]=i+1;

n=0;
i=0;
temp=k;
while(temp!=a[i])
{
if(a[i])n++;

if(n%m==0)a[i]=0;else if(a[i])temp=a[i];

if(i==k-1)i=0;else i++; }

printf("%d",temp) ;
getch();
}
我的最简单了 输入两个数即得结果 每一个数输入猴子总数 第二个数输入第几个猴子出列

/bbs/showimg.asp?BoardID=5&filename=2005-4/2005427111228529.jpg" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=5&filename=2005-4/2005427111228529.jpg');}" onmousewheel="return imgzoom(this);" alt="" /> 欢迎加入C语言QQ群698156 我们都是菜鸟乃至新手 坚信有一天定能展翅高飞 因为有着努力的决心 衷心盼望你的到来 让我们一起进步
2005-05-27 22:24
huwenjie
Rank: 1
等 级:新手上路
帖 子:84
专家分:0
注 册:2005-3-10
收藏
得分:0 

函数: #include <stdio.h> #include <stdlib.h> #include "MyHeadFile.H"

struct Monkey *CreateList( int MonkeyNumber ) //根据用户输入的猴子数生成循环链表。较数组的优点在于从理论上可以不确定猴子数量的多少(只要你内存够大,CPU够彪悍) { struct Monkey *head = NULL; struct Monkey *tail = NULL; struct Monkey *temp = NULL;

for ( int i = 0; i < MonkeyNumber; i++ ) { temp = ( struct Monkey *)malloc(sizeof(struct Monkey)); if ( temp == NULL ) { printf("Out Of Memory!\n"); return head; } temp->pNext = head;

if ( head == NULL ) { head = tail = temp; head->MonkeyID = 1; } else { tail->pNext = temp; tail = temp; tail->MonkeyID = i + 1; } } return head; }

void ViewList( struct Monkey *head ) { struct Monkey *p = head;

do { printf("ID:%d\n",p->MonkeyID); p = p->pNext ; } while ( p != head ); }

//释放内存 void FreeList( struct Monkey *head ) //定义*PTEMP作为保留下一节点指针的变量 { struct Monkey *p = head; struct Monkey *ptemp = p;

do { ptemp = p->pNext; free(p); p = ptemp; } while (p != head); } //删除链表单元 struct Monkey *DeleteMonkey( struct Monkey **head, int MonkeyNumber ) //第一个参数为指针的指针,为了改变主函数中的HEAD值 { struct Monkey *pDel = NULL; //删除目标 struct Monkey *pTEMP = NULL; //用做保存上一节点的临时变量 struct Monkey *pFREE = NULL; //用做保存用于释放的地址节点的临时变量 int i;

if ( MonkeyNumber == 1 ) //输入报数为一的时候猴子为最后一只 { pDel = *head; do { pTEMP = pDel->pNext; free(pDel); pDel = pTEMP; } while (pDel->pNext != *head); pDel->pNext = *head = pDel; //完成循环链表 return *head; } while ( (*head)->pNext != *head ) //循环删除节点直到仅盛下一个单元即(*head)->pNext != *head { pDel = *head; for ( i = 0; i < MonkeyNumber - 1; i ++ ) { pTEMP = pDel; pDel = pDel->pNext; //向后数猴子 } pFREE = pDel; pTEMP->pNext = pDel->pNext; *head = pDel->pNext ; //重新定位头,准备开始重新报数 free(pFREE); pFREE = NULL; }

return *head; }

[此贴子已经被作者于2005-10-27 0:46:11编辑过]

2005-10-27 00:36
huwenjie
Rank: 1
等 级:新手上路
帖 子:84
专家分:0
注 册:2005-3-10
收藏
得分:0 

Main函数 /*猴子选大王之循环链表版*/ #include <stdio.h> #include <stdlib.h> #include "MyHeadFile.H"

void main(void) { int MonkeyNumber = 0; int CountNumber = 0; struct Monkey *pMonkey = NULL;

printf("Input Monkey Number:"); //输入猴子数量 scanf("%d",&MonkeyNumber);

printf("Input Count Number:"); //输入报数周期 scanf("%d",&CountNumber);

pMonkey = CreateList( MonkeyNumber ); //建立循环链表 // ViewList( pMonkey );

pMonkey = DeleteMonkey( &pMonkey, CountNumber );

ViewList( pMonkey ); FreeList( pMonkey );

}

[此贴子已经被作者于2005-10-27 0:41:24编辑过]

2005-10-27 00:37
huwenjie
Rank: 1
等 级:新手上路
帖 子:84
专家分:0
注 册:2005-3-10
收藏
得分:0 

头文件:"MyHeadFile.H" #ifndef INCLUDE_MYHEADFILE_H #define INCLUDE_MYHEADFILE_H

struct Monkey //定义结构体 { int MonkeyID; struct Monkey *pNext; };

//函数声明

struct Monkey *CreateList( int MonkeyNumber ); void FreeList( struct Monkey *head ); void ViewList( struct Monkey *head ); struct Monkey *DeleteMonkey( struct Monkey **head, int MonkeyNumber );

#endif

2005-10-27 00:38
huwenjie
Rank: 1
等 级:新手上路
帖 子:84
专家分:0
注 册:2005-3-10
收藏
得分:0 
这个是用循环链表做的!可能代码有点长。不过注释已经写的比较清楚了!编码规范方面基本遵循的是林锐博士的《高质量C/C++编程》一书中所提到的观点。由于是用C编译器编写的,所以没有使用断言函数!
2005-10-27 00:43
jsbjzy
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2005-10-27
收藏
得分:0 
题目应该是报单数的出列吧?
2005-10-27 14:59
jsbjzy
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2005-10-27
收藏
得分:0 

程序如下,最简单了,只要输入猴子数量,就能立刻告诉你结果! #include <stdio.h> void main() {

int j=1,n;

printf("Please input monkeys's number:\n"); scanf("%d",&n); loop: j=j*2; if(j<=n) goto loop; j=j/2; printf("last speak '1' 's monkey is No.%d",j); }

2005-10-27 15:19
huwenjie
Rank: 1
等 级:新手上路
帖 子:84
专家分:0
注 册:2005-3-10
收藏
得分:0 
以下是引用jsbjzy在2005-10-27 14:59:18的发言: 题目应该是报单数的出列吧?
怎么出列又有什么关系嘛~ 只是修改一下判断条件而已!
2005-10-27 19:25
快速回复:猴子报大王的程序怎么编啊
数据加载中...
 
   



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

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