
程序设计思路:
一、小朋友和苹果都具有多样属性(比如高度、编号、状态等,还可以扩展出姓名,重量等)。所以小朋友和苹果要定义成结构体。
二、人和苹果数量都是手动输入,因此数组大小不确定,要使用动态数组(不使用动态,就得得限制用户输入的大小)。
三、题目要求确保摘到的总数最多,从最矮的小朋友开始摘,因此小朋友的数组要进行排序。
四、递归函数实现摘苹果逻辑,每人在自己够到的范围中随机摘两个(不够就拿1个)。(递归函数每次发现一个可摘取的苹果,有50%概率看中,都没看中,默认摘取最后一个看中的苹果)。
下面是代码(控制台刷新函数中cls仅限window系统运行,其它 *** 作系统,删除或修改):
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<malloc.h>
#define AFR 7//苹果图像的行数
#define AFC 6//苹果图像的行数
#define CFR 5//小朋友图像的行数
#define CFC 6//小朋友图像的行数
typedef struct apple//表示苹果数据的结坦岁大构体
{
int aid//苹果编号
int height//苹果的高度
int status//0:表示未被摘取。1:表示已被摘取
char aframe[AFR][AFC]//表示苹果的图像
}APPE
typedef struct childern//表示小孩子的编号
{
int cid//小孩子的编号
int height//小孩子的身高
int n//小孩摘取的苹果数量让竖
char cframe[CFR][CFC]//表示小朋友的图像
APPE **appes//小孩摘取的苹果结构指针数组
}CHN
int n,m//苹果和小朋友的个数,设为全局变量
APPE *setApps()//设置苹果。成功返回结构数组,失败返回NULL
CHN *setChns()//设置小盆友。同上。
int orderChnByHeight(CHN *chns)//对小朋友数组按照身高升序排列
int getApple(APPE *appes,CHN *chns,char (*strInfo)[100])//递归,模拟小朋友依次选苹果。异常返回-1
int showFrame(APPE *appes,CHN *chns,char (*strInfo)[100])
int main()
{
int i
char (*strInfo)[100]=NULL//用于显示 *** 作流水
APPE *appes=NULL
CHN *chns=NULL
appes=setApps()
chns=setChns()
if(orderChnByHeight(chns)==-1)return 1
srand(time(NULL))
strInfo=(char (*)[100])malloc(sizeof(char *)*m*100)
for(i=0i<mi++)strInfo[i][0]=0
if(!strInfo) return 1
showFrame(appes,chns,strInfo)
return 0
}
int showFrame(APPE *appes,CHN *chns,char (*strInfo)[100])
{
static int k=1
int i,j
system("cls")
printf("\n=============每组图像靠上的数值为高度,靠下的数值为编号============\n")
printf("\n=============为确保能拿到最多的苹果,雀敏小朋友们按升序排列============\n")
for(i=0i<AFRprintf("\n"),i++)
for(j=0j<nj++)
printf("%s ",appes[j].aframe[i])
printf("\n")
for(i=0i<CFRprintf("\n"),i++)
for(j=0j<mj++)
printf("%s ",chns[j].cframe[i])
printf("\n====================================================================\n")
printf(" *** 作流水:\n")
for(i=0i<mi++)
printf("%s\n",strInfo[i])
fflush(stdin)
printf("按下任意键进行下一步。。。。。。\n")
getchar()
if(getApple(appes,chns,strInfo)==-1)return -1
if(k)showFrame(appes,chns,strInfo),k--
return 1
}
int getApple(APPE *appes,CHN *chns,char (*strInfo)[100])
{
static int i=0,aflag,cflag
int j,indexSave
if(appes==NULL||chns==NULL) return -1
if(chns[i].n==2)i++//当前小朋友拿够2个,换下一个小朋友
if(i==m)return 1//所有人均拿过,结束递归
aflag=0
for(j=0j<nj++)
if(appes[j].status==0) {aflag=1break}
if(aflag==0) return 1//所有苹果均拿完,结束递归
indexSave=-1
cflag=0
for(j=0j<nj++)
{
if(appes[j].status==0 &&appes[j].height<=chns[i].height)
{
cflag=1
indexSave=j
if(rand()%2)//每次发现,有50%概率拿取,如所有可拿苹果都没选中,选最后发现的目标
break
}
}
if(cflag)//小朋友拿起一个苹果的过程
{
appes[indexSave].status=1
//改变苹果初始图像
sprintf(appes[indexSave].aframe[6]," ")
chns[i].appes[chns[i].n]=&appes[indexSave]
chns[i].n++
if(chns[i].n==1)
{
//改变小朋友初始图像
sprintf(chns[i].cframe[0]," %c%c/ ",3,1)
sprintf(strInfo[i],"编号%d的小朋友拿取了1个苹果(编号%d)\n",chns[i].cid,chns[i].appes[0]->aid)
}
if(chns[i].n==2)
{
//改变小朋友初始图像
sprintf(chns[i].cframe[0]," %c%c%c ",3,1,3)
sprintf(strInfo[i],"编号%d的小朋友拿取了2个苹果(编号%d和编号%d)\n",chns[i].cid,chns[i].appes[0]->aid,chns[i].appes[1]->aid)
}
}
if(cflag==0 &&chns[i].n==0) sprintf(strInfo[i],"编号%d的小朋友没有能拿到的苹果,非常沮丧!\n",chns[i].cid),i++
if(cflag==0 &&chns[i].n==1) i++
return getApple(appes,chns,strInfo)
}
int orderChnByHeight(CHN *chns)
{
CHN chnTemp
int i,j
chnTemp.appes=(APPE **)malloc(sizeof(APPE*)*2)
if(!chnTemp.appes) return -1
else
{
chnTemp.appes[0]=chnTemp.appes[1]=NULL
if(chns)
for(i=0i<m-1i++)
for(j=i+1j<mj++)
if(chns[i].height>chns[j].height)
chnTemp=chns[i],chns[i]=chns[j],chns[j]=chnTemp
}
free(chnTemp.appes)
return 1
}
CHN *setChns()
{
int i
CHN *chns=NULL
printf("请输入小朋友的个数:")
scanf("%d",&m)
chns=(CHN *)malloc(sizeof(CHN)*m)
if(!chns) return NULL
printf("请输入%d个小朋友身高(不超过3位整数):\n",m)
for(i=0i<mi++)
{
chns[i].cid=i+1
scanf("%d",&chns[i].height)
chns[i].height=chns[i].height%1000//超出3位截取
chns[i].n=0
chns[i].appes=(APPE **)malloc(sizeof(APPE*)*2)
if(!chns[i].appes) return NULL
chns[i].appes[0]=chns[i].appes[1]=NULL
//设置小朋友初始图像
sprintf(chns[i].cframe[0]," \\%c/ ",1)
sprintf(chns[i].cframe[1]," / \\ ")
sprintf(chns[i].cframe[2],"-----")
sprintf(chns[i].cframe[3],"高%3d",chns[i].height)
sprintf(chns[i].cframe[4],"ID%3d",chns[i].cid)
}
return chns
}
APPE *setApps()
{
int i
APPE *appes=NULL
printf("请输入苹果的个数:")
scanf("%d",&n)
appes=(APPE *)malloc(sizeof(APPE)*n)
if(!appes) return NULL
printf("请输入%d个苹果的高度(不超过3位整数):\n",n)
for(i=0i<ni++)
{
appes[i].aid=i+1
scanf("%d",&appes[i].height)
appes[i].height=appes[i].height%1000//超出3位截取
appes[i].status=0
//设置苹果初始图像
sprintf(appes[i].aframe[0],"高%3d",appes[i].height)
sprintf(appes[i].aframe[1],"ID%3d",appes[i].aid)
sprintf(appes[i].aframe[2],"-----")
sprintf(appes[i].aframe[3]," %c ",'|')
sprintf(appes[i].aframe[4]," %c ",'|')
sprintf(appes[i].aframe[5]," %c ",'|')
sprintf(appes[i].aframe[6]," %c ",3)
}
return appes
}
#include<iostream>陵猜using namespace stdint main() {int a,n[11],i,c,x=0 for(i=1i<=10i++)
{cin>>n[i]
}
cin>>c
c=c+30 for(i=1i<旅滑=10i++)
{if(n[i]<=c)
x++
}
cout<<x return 0
}
淘淘尺镇型摘苹果
把输出j:=0
for k:=1 to m do
for i:=1 to m do
begin
if num[i]=p+1
then begin
if bool=false
then begin
write(' ',much[i])
p:=p+1
end
if bool
then begin
write(much[i])
p:=p+1
bool:=false
end
end
end
writeln
这一块芦春改成
write(much[num[1]])
for i:=2 to m do write(' ',much[num[i]])
writeln
就可以了。
下面扰坦是我写的程序
type
arr=array[1..100000]of longint
var
n,m,i,ap,j,k,p:longint
apple,num,weight,ans:arr
bool:boolean
procedure qsort(n,m:longint)
var
a,b,c,temp:longint
begin
a:=nb:=mc:=apple[(n+m)div 2]
repeat
while c>apple[a] do inc(a)
while c<apple[b] do dec(b)
if a<陪李耐=b
then begin
temp:=apple[a]apple[a]:=apple[b]apple[b]:=temp
inc(a)dec(b)
end
until a>b
if a<m then qsort(a,m)
if b>n then qsort(n,b)
end
procedure wqsort(n,m:longint)
var
a,b,c,temp:longint
begin
a:=nb:=mc:=weight[(n+m)div 2]
repeat
while c <apple[a] do inc(a)
while c >apple[b] do dec(b)
if a<=b
then begin
temp:=weight[a]weight[a]:=weight[b]weight[b]:=temp
temp:=num[a]num[a]:=num[b]num[b]:=temp
inc(a)dec(b)
end
until a>b
if a<m then wqsort(a,m)
if b>n then wqsort(n,b)
end
begin
readln(n,m)
for i:=1 to n do read(apple[i])
for i:=1 to m do
begin
read(weight[i])
num[i]:=i
end
qsort(1,n)wqsort(1,m)
i:=1j:=n
while(j>0)do
begin
ans[i]:=ans[i]+apple[j]
dec(j)i:=i mod m+1
end
write(ans[num[1]])
for i:=2 to m do write(' ',ans[num[i]])
end.
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)