小猴編程密碼
㈠ c++編程猴子選大王 猴子按1…、n編號順時針方向圍坐一圈,從第1號開始按1、…m報數,凡報到m號的退出圈外
p=q是改變頭結點,讓新插入的處於鏈表頭 如果忽略頭Head->next = head就是單鏈表了
㈡ 用C++編程 猴子選大王
#include <iostream>
using namespace std;
template <class datatype> class LinkList;
template <class datatype>
class Node
{
friend class LinkList<datatype>;//友元類
private:
datatype data;//計猴子號
Node<datatype> *next;
};
template <class datatype>
class LinkList
{
public:
LinkList();
void monkey(int m); //建立有m個元素的單鏈表
datatype Get(int a); //取單鏈表中第i個結點的元素值
datatype Delete(int n); //在單鏈表中刪除第n個結點
private:
Node<datatype> *head,*tail; //單鏈表的結構指針
};
template <class datatype>
LinkList<datatype>:: LinkList( )
{head=new Node<datatype>; head->next=NULL;}
template <class datatype>
void LinkList<datatype>::monkey(int m)
{
int i;//整型變數i,用於計數
Node<datatype> *p,*q;//聲明結構指針
p=new Node<datatype>;//為p分配空間
p->data=1; //初始化p結點data域為1
p->next=NULL;//初始化p結點next域為空
head=p;//鏈表頭指針head賦值為p
q=p; //q賦值為p
for (i=2; i<=m; i++) //用循環結構構造鏈表
{
p=new Node<datatype>;//為p配內存空間
p->data=i; //初始化p結點data域為i,表示猴子號
q->next=p; //將p點加到鏈表尾部
q=p; //讓指向鏈表尾部結點
p->next=NULL; //鏈表尾部為空
}
tail=q;//鏈表尾
tail->next=head;//鏈表尾部指向鏈表頭,形成循環鏈表
}
template <class datatype>
datatype LinkList<datatype>::Delete(int n)
{
Node<datatype> *p,*q;
int j=0;
q=tail; //指向循環鏈表尾部
cout<<"被刪除的猴子號碼依次為:"<<endl;
while (q!=q->next) //剩餘結點數不為1,則繼續循環
{
p=q->next;//p賦值給下一個相鄰結點
j++;
if(j%n==0)
{
cout<<p->data<<ends;
q->next=p->next;//刪除此結點
delete p;//釋放空間
p=NULL;
}
else q=p;//q指向相鄰的下一個結點p
}
cout<<endl;
head=q;//head指向結點q,q為鏈表中剩餘的一個結點
return head->data;
}
template <class datatype>
datatype LinkList<datatype>::Get(int a)
{
Node<datatype> *p;
int j;//計數器
p=head->next; j=1; //或p=head; j=0;
while (p && j<a)
{
p=p->next; //工作指針p後移
j++;
}
if (!p) throw "a值不合法";
else return p->data;
}
void main()
{
int m,n;
LinkList<int>mon;
cout<<"請輸入猴子的總數:"<<endl;
cin>>m;
cout<<"請輸入要刪除猴子的所報的數:"<<endl;
cin>>n;
mon.monkey(m);
mon.Delete(n);
cout<<"猴王是:"<<mon.Get(1)<<"號"<<endl;
}
㈢ 猴子撈月,c語言編程
#include"stdio.h"
#include"conio.h"
#definemax30001
main()
{
inti,k,m,n,num[max],*p,h,count,c;
FILE*fp;
fp=fopen("LeftQueue.txt","w+");
printf("1Pleaseinputthetotalnumber(beginwithno.1)");
scanf("%d",&n);
printf("");
scanf("%d",&count);
printf("");
scanf("%d",&c);
fprintf(fp,"編號從1到%d,報%d者出列,每列存放%d個數據。 出列順序如下: ",n,count,c);
p=num;
for(i=0;i<n;i++)
*(p+i)=i+1;
i=0;
k=0;
m=0;
h=0;
while(m<n-1)
{
if(*(p+i)!=0)k++;
if(k==count)
{
fprintf(fp,"%7d",*(p+i));
*(p+i)=0;
k=0;
m++;
h++;
if(h%c==0)fprintf(fp," ");
}
i++;
if(i==n)i=0;
}
while(*p==0)p++;
printf(" 1%disleft.%dnumberleftthequeue.",*p,h);
fprintf(fp," 最後留下的數為:%d;共%d個數離開。",*p,h);
fclose(fp);
getch();
}
㈣ c語言編程解決小猴爬山
上山下山應該是一樣的,都是讓電腦一個值一個值去判斷。我的想法是讓電腦先判斷出所有可能的組合,比如2+3+5和3+2+5是一個概念,然後建立一個排列組合的函數,對組合進一步處理。上山為例:
#include<stdio.h>
intjc(inta)//階乘
{
inti,j=1;
for(i=a;i>0;i--)
{
j=j*i;
}
returnj;
}
intmain(intargc,char*argv[])
{
intn;//方法數
constintt=50;//台階數
inti,j,k;
for(i=0;i<=25;i++)//2*25=50
for(j=0;j<17;j++)//3*16=48
for(k=0;k<13;k++)//4*12=28
{
if(2*i+3*j+4*k==t)//兩步、三步或四步
{
n=n+jc(i+j+k)/(jc(i)*jc(k)*jc(j));
}
}
printf("上山總數為:%d種 ",n);
return0;
}
這個程序計算出來上山有3731種