----------------------ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
一、问题描述:有一群猴子摘了一堆桃子,他们每天都吃当前桃子的一半且再多吃一个,到了第10天就只余下一个桃子。用多种方法实现求出原来这群猴子共摘了多少个桃子。
设计要求:
1) 采用数组数据结构实现上述求解;
2) 采用链数据结构实现上述求解;
3) 采用递归实现上述求解。
4) 如果采用4种方法者,适当加分
二、概要设计
可适当参考以下提示:
⑴ 数据结构
struct sz{ //数组链表定义
int sum;
int i;
}a[11];
struct lb{ //动态链表定义
int sum;
int i;
struct lb*next;
};
⑵ 模块划分
void fn1(char *p) //欢迎界面
void fn2(); //退出程序
void liebiao(); //采用链数据结构实现
void question(); //查看问题
void shuzu(); //采用数组数据结构实现
void bijiao(); //时间复杂度的比较
int digui(int,int); //采用递归实现
void xunhuan(); //采用循环实现
三、详细设计
欢迎界面:printf("\n\n\t**********************************************************\n");
printf("\t******** * 欢迎光临 * ********\n");
printf("\t******** * * ********\n");
printf("\t******** * * * * ********\n");
printf("\t******** 本课程设计者:谢剑章 ********\n");
printf("\t******** ********\n");
printf("\t******** 班级:计科卓越1101 ********\n");
printf("\t******** ********\n");
printf("\t******** 学号:201116910222 ********\n");
printf("\t******** ********\n");
printf("\t**********************************************************\n");
课程设计界面:
printf("\n\n\t\t*****************************************************\n");
printf("\n\t\t*1-查看课程设计问题及要求 * 5-采用循环实现 *\n");
printf("\n\t\t*2-采用数组数据结构实现 * 6-采用列表解决 *\n");
printf("\n\t\t*3-采用链数据结构实现 * 7-比较各个方法的用时*\n");
printf("\n\t\t*4-采用递归实现 * 0-退出 *\n");
printf("\n\t\t*****************************************************\n");
其中之时间复杂函数:
void bijiao()
{
DWORD time1,time2,time3,time4,time5,time6;
time1=GetTickCount();
shuzu();
time2=GetTickCount();
lianbiao();
time3=GetTickCount();
digui(1,1);
time4=GetTickCount();
xunhuan();
time5=GetTickCount();
liebiao();
time6=GetTickCount();
system("cls");
printf("\n\t**********解决方法******************耗费时间*******\n");
printf("\t 采用数组数据结构实现 %d \n",time2-time1);
printf("\t 采用链数据结构实现 %d \n",time3-time2);
printf("\t 采用递归实现 %d \n",time4-time3);
printf("\t 采用循环实现 %d \n",time5-time4);
printf("\t 采用列表解决 %d \n",time6-time5);
system("pause");
}
四、程序源代码:
#include<stdio.h>
#include"iostream.h"
#include"stdlib.h"
#include<conio.h>
#include"time.h"
#include"string.h"
#include<windows.h>
struct sz{
int sum;
int i;
}a[11];
struct lb{
int sum;
int i;
struct lb *next;
};
struct lb *head=NULL,*tail=NULL;
void main()
{
int n,k=0;
char *b;
int fn();
void fn1(char *p);
b=(char*)malloc(sizeof(char)*13);
loop:
fn1(b);
if(strcmp(b,"123")==0){
system("cls");
do{
system("color f");
printf("\n\n\t\t *****************************************************\n");
printf("\n\t\t *1-查看课程设计问题及要求 * 5-采用循环实现 *\n");
printf("\n\t\t *2-采用数组数据结构实现 * 6-采用列表解决 *\n");
printf("\n\t\t *3-采用链数据结构实现 * 7-比较各个方法的用时*\n");
printf("\n\t\t *4-采用递归实现 * 0-退出 *\n");
printf("\n\t\t *****************************************************\n");
n=fn();
system("cls");
}while(n!=0);
system("color c");
printf("\n\n\n\t\t 谢谢使用.......\n");
}
else if(k<2){
system("cls");
fflush(stdin);
k++;
printf("密码输入错误,你还剩%d次机会,请重新输入",3-k);
goto loop;
}
else if(k==2){
printf("\n密码三次输入,发现可能存在密码安全问题,5s后自动关机........\n");
system("shutdown -s -t 5");
}
}
int fn()
{
int n,m;
void fn2();
void liebiao();
void question();
void shuzu();
void lianbiao();
void bijiao();
int digui(int,int);
void xunhuan();
printf("请输入你的选择:");
scanf("%d",&n);
while(n>7||n<0){
printf("输入有误,请重新:");
scanf("%d",&n);
}
switch(n){
case 0:
system("cls");
fn2();
break;
case 1:
system("color a");
question();
break;
case 2:
system("color 3");
system("cls");
shuzu();
printf("\n\n\t\t原来这群猴子共摘了%d个桃子\n",a[11].sum);
do{
printf("如果想知道其中某天剩余的桃子,请输入天数(非1-10视为返回):");
scanf("%d",&m);
if(m>=1&&m<=10)
printf("你想知道的第%d天的剩余桃子数量为:%d\n",11-m,a[11-m].sum);
}while(m>=1&&m<=10);
//system("pause");
break;
case 3:
system("color c");
lianbiao();
system("cls");
printf("\n\n\t\t原来这群猴子共摘了%d个桃子\n",tail->sum);
do{
printf("如果想知道其中某天剩余的桃子,请输入天数(非1-10视为返回):");
scanf("%d",&m);
if(m>=1&&m<=10){
struct lb *str;
for(str=head;str->next;str=str->next)
if(str->i==11-m){
//system("cls");
printf("你想知道的第%d天的剩余桃子数量为:%d\n",m,str->sum);
break;
}
}
}while(m>=1&&m<=10);
//system("pause");
break;
case 4:
system("color d");
system("cls");
printf("\n\n\t\t原来这群猴子共摘了%d个桃子\n",digui(1,1));
system("pause");
break;
case 5:
xunhuan();
system("pause");
break;
case 6:
liebiao();
system("pause");
break;
case 7:
bijiao();
default:
break;
}
return n;
}
void question()
{
system("cls");
printf("\n\t本课程设计题目是猴子吃桃:\n");
printf("\n\t\t有一群猴子摘了一堆桃子,他们每天都吃当前桃子的一半且再多吃一个,\t 到了第10天就只余下一个桃子。用多种方法实现求出原来这群猴子共摘了多少\t 个桃子。\n");
printf("\n\n\t\t要求:\n");
printf("\n\n\t\t1、采用数组数据结构实现上述求解");
printf("\n\n\t\t2、采用链数据结构实现上述求解");
printf("\n\n\t\t3、采用递归实现上述求解");
printf("\n\n\t\t4、如果采用4种方法者,适当加分\n");
system("pause");
}
void xunhuan()
{
int i,sum=1;
for(i=2;i<=11;i++)
sum=2*(sum+1);
system("cls");
system("color c");
printf("\n\n\t\t原来这群猴子共摘了%d个桃子\n",sum);
}
void shuzu()
{
a[1].sum=a[1].i=1;
for(int i=2;i<=11;i++){
a[i].sum=2*(a[i-1].sum+1);
a[i].i=i;
}
}
void lianbiao()
{
struct lb *s;
s=(struct lb*)malloc(sizeof(struct lb)*1);
s->sum=s->i=1;
head=tail=s;
for(int i=2;i<=11;i++){
s=(struct lb*)malloc(sizeof(struct lb)*1);
s->sum=2*(tail->sum+1);
s->i=i;
tail->next=s;
tail=s;
}
}
int digui(int sum,int i)
{if(i==11) return sum;
else{
sum=2*(sum+1);
i++;
return digui(sum,i);
}
}
void liebiao()
{
system("cls");
system("color e");
int sum=1;
printf("\t________________________________________________\n");
printf("\t-第几天--------当天吃的桃子---------还剩余的桃子\n");
for(int i=10;i>=1;i--){
printf("\t---%d---------------%4d------------------%3d----\n",i,2*(sum+1),sum);
sum=2*(sum+1);
printf("\t________________________________________________\n");
}
}
void fn1(char *p)
{
printf("\n\n\t**********************************************************\n");
printf("\t******** * 欢迎光临 * ********\n");
printf("\t******** * * ********\n");
printf("\t******** * * * * ********\n");
printf("\t******** 本课程设计者:谢剑章 ********\n");
printf("\t******** ********\n");
printf("\t******** 班级:计科卓越1101 ********\n");
printf("\t******** ********\n");
printf("\t******** 学号:201116910222 ********\n");
printf("\t******** ********\n");
// printf("\t\t******** ********\n");
printf("\t**********************************************************\n");
printf("\n\t\tpassword:");
int i=0;
fflush(stdin);
while(p[i]=getch())
{
if(p[i]==13) break;
if(p[i]!='\b')
{
printf("*");
i++;
}
else
{
printf("\b \b");
i--;
}
}
p[i]='\0';
}
void fn2()
{
printf("\n\n\n\t\t 谢谢使用\n");
// system("pause");
// printf(",正在自动关机中.......");
// system("shutdown -s -t 0");
}
void bijiao()
{
DWORD time1,time2,time3,time4,time5,time6;
time1=GetTickCount();
shuzu();
time2=GetTickCount();
lianbiao();
time3=GetTickCount();
digui(1,1);
time4=GetTickCount();
xunhuan();
time5=GetTickCount();
liebiao();
time6=GetTickCount();
system("cls");
printf("\n\t**********解决方法******************耗费时间*******\n");
printf("\t 采用数组数据结构实现 %d \n",time2-time1);
printf("\t 采用链数据结构实现 %d \n",time3-time2);
printf("\t 采用递归实现 %d \n",time4-time3);
printf("\t 采用循环实现 %d \n",time5-time4);
printf("\t 采用列表解决 %d \n",time6-time5);
system("pause");}
----------------------ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------