作者在 2008-12-29 20:52:55 发布以下内容
前些天才做的题目,呵呵是简单的程序
有一对小老鼠,出生一周后长成一对大老鼠,两周后出生第一对小老鼠,三周后,上周出生的小老鼠变成了大老鼠,而原来的大老鼠又生出了一对小老鼠之后便死亡了,四周后,第一对出生的小老鼠(此时已经是大老鼠)又生出了一对小老鼠,此时共有三对老鼠。试编制程序,计算N周后有多少对老鼠?
基本要求:
(1)编程实现,要求N的取值可以随意变换;
(2)实物演示时要求讲出程序原理;
提高要求:
(1)能够采用一些形式动态描述老鼠出生和长大这一过程;
(2)考虑采用性能好的算法。
// This program is designed for SUBJECT 6--Reproduction of Mice
#include <iostream.h>
int SMNum(int N);
int BMNum(int N);
int BMNum(int N);
void Menu()
{
cout<<" ----- Mice Reproduction -----"<<endl;
cout<<" 1. Work out the num directly"<<endl;
cout<<" 2. Show the Working process"<<endl;
cout<<" 3. About the program"<<endl;
cout<<" 4. Quit the program"<<endl<<endl;
}
{
cout<<" ----- Mice Reproduction -----"<<endl;
cout<<" 1. Work out the num directly"<<endl;
cout<<" 2. Show the Working process"<<endl;
cout<<" 3. About the program"<<endl;
cout<<" 4. Quit the program"<<endl<<endl;
}
int BMNum(int N)
{
if (N==0)
return 0;
else if (N==1)
return 1;
else
return SMNum(N-1)+SMNum(N-2);
}
{
if (N==0)
return 0;
else if (N==1)
return 1;
else
return SMNum(N-1)+SMNum(N-2);
}
int SMNum(int N)
{
if (N==0)
return 1;
else
return BMNum(N-1);
}
{
if (N==0)
return 1;
else
return BMNum(N-1);
}
int GMNum(int N)
{
return SMNum(N-1);
}
{
return SMNum(N-1);
}
int TOTALNum(int N)
{
return BMNum(N)+SMNum(N);
}
{
return BMNum(N)+SMNum(N);
}
void Print(int N)
{
cout<<endl<<"-Now show the process-"<<endl;
for (int i=1;i<=N;i++)
{
cout<<" Week "<<i<<endl;
cout<<" Number of mice bored: "<<SMNum(i)<<endl;
cout<<" Number of mice grown: "<<GMNum(i)<<endl;
cout<<" Total number of mice: "<<TOTALNum(i)<<endl<<endl;
}
}
{
cout<<endl<<"-Now show the process-"<<endl;
for (int i=1;i<=N;i++)
{
cout<<" Week "<<i<<endl;
cout<<" Number of mice bored: "<<SMNum(i)<<endl;
cout<<" Number of mice grown: "<<GMNum(i)<<endl;
cout<<" Total number of mice: "<<TOTALNum(i)<<endl<<endl;
}
}
void main()
{
char Choice;
int InputN,QuitFlag=0;
do{
Menu();
cout<<" Please make a choice: ";
do{
cin>>Choice;
if(Choice<49||Choice>53)
cout<<" Input error! Please try again!"<<endl;
}while(Choice<49||Choice>53);
switch(Choice)
{
case 49:
cout<<endl<<"-Work out the num directly-"<<endl;
cout<<" Please intput the week: ";
do{
cin>>InputN;
}while(InputN<1);
cout<<" Total number of mice: "<<TOTALNum(InputN)<<endl<<endl<<endl<<endl;
QuitFlag=0;
break;
case 50:
cout<<endl<<"-Show the Working process-"<<endl;
cout<<endl<<"-Important Tips-"<<endl<<" If your inputs is too large, the process will take a long time."<<endl;
cout<<endl<<" Please intput the week: ";
do{
cin>>InputN;
}while(InputN<1);
Print(InputN);
cout<<"-The End-"<<endl;
QuitFlag=0;
break;
case 51:
cout<<endl<<"-About the program-"<<endl;
QuitFlag=0;
break;
case 52:
default:
cout<<endl<<"-Quit the program-"<<endl;
QuitFlag=1;
break;
}
}while(QuitFlag==0);
}
{
char Choice;
int InputN,QuitFlag=0;
do{
Menu();
cout<<" Please make a choice: ";
do{
cin>>Choice;
if(Choice<49||Choice>53)
cout<<" Input error! Please try again!"<<endl;
}while(Choice<49||Choice>53);
switch(Choice)
{
case 49:
cout<<endl<<"-Work out the num directly-"<<endl;
cout<<" Please intput the week: ";
do{
cin>>InputN;
}while(InputN<1);
cout<<" Total number of mice: "<<TOTALNum(InputN)<<endl<<endl<<endl<<endl;
QuitFlag=0;
break;
case 50:
cout<<endl<<"-Show the Working process-"<<endl;
cout<<endl<<"-Important Tips-"<<endl<<" If your inputs is too large, the process will take a long time."<<endl;
cout<<endl<<" Please intput the week: ";
do{
cin>>InputN;
}while(InputN<1);
Print(InputN);
cout<<"-The End-"<<endl;
QuitFlag=0;
break;
case 51:
cout<<endl<<"-About the program-"<<endl;
QuitFlag=0;
break;
case 52:
default:
cout<<endl<<"-Quit the program-"<<endl;
QuitFlag=1;
break;
}
}while(QuitFlag==0);
}