考研问题请教!!!!

sharklee 2001-11-22 09:38:19
小刚假期同妈妈一起去书店,他选中了n本书,每本书的单价为:p1,p2,p3...pn元(均为整数)。不巧的是,妈妈只带了s(为整数)元钱,不够买这n本书(即:s<p1+p2+p3+...+pn)。妈妈同意将这s元全部用来买书,也就是小刚要从n本书中选出m本,使得这m本的价格和刚好等于s,即p1+p2+p3+...+pn=s,请你编程序将所有满足这一条件的i1,i2,i3,...im依次打印出来。
...全文
158 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sharklee 2001-11-22
  • 打赏
  • 举报
回复
我还是不太明白,你能不能讲的详细一点。
tarkey 2001-11-22
  • 打赏
  • 举报
回复
回嗍
one_add_one 2001-11-22
  • 打赏
  • 举报
回复
穷举不就可以了?

如果N很大,就用广搜

如果还慢就来双向
hsq 2001-11-22
  • 打赏
  • 举报
回复
#include "stdio.h"
#include "stdlib.h"
#define NULL 0
int *t;
void output();
void process();
void main(void)
{int *detmoney;
int i,k,money,account,sum,ini;
fprintf(stderr,"How much money in your packet?\nmoney=");
scanf("%d",&money);
printf("\n");
fprintf(stderr,"How many books do you want!\naccount=");
scanf("%d",&account);
printf("\n");
fprintf(stderr,"Please input details here:\n\t");
t=calloc(money,sizeof(int));
if (t==NULL)
{fprintf(stderr,"Out of Memories!\n");
exit(0);
}
detmoney=calloc(money,sizeof(int));
if (detmoney==NULL)
{fprintf(stderr,"Out of Memories!\n");
exit(1);
}
for (i=0;i<money;i++)
{fprintf(stderr,"detmoney[%d]=",i);
scanf("%d",&detmoney[i]);
printf("\n\t");
sum+=detmoney[i];
}
ini=0;
k=0;
process(detmoney,money,account,k,sum,ini);
free(t);
free(detmoney);
}

void process(detmoney,money,count,k,sum,ini)
int *detmoney;
int money,count,k,sum,ini;
{ int i=0;
if (ini+detmoney[i]==count)
{t[k]=detmoney[i];
output(k);
return;
}
else
if ((ini+sum>=count)&&(ini+detmoney[i]<count))
{ t[k]=detmoney[i];
process(&detmoney[i+1],money-1-i,count,k+1,sum-detmoney[i],ini+detmoney[i]);
}
if ((ini+sum>=count)&&(ini+detmoney[i+1]<=count))
process(&detmoney[i+1],money-i-1,count,k,sum-detmoney[i],ini);
}

void output(k)
int k;
{ int i;
for (i=0;i<=k;i++)
printf("%4d",t[i]);
printf("\n");
}


wangxiang0123 2001-11-22
  • 打赏
  • 举报
回复
告诉我一个邮箱地址。我过一会儿编出来给你发过去。
starfish 2001-11-22
  • 打赏
  • 举报
回复
呵呵,又看见1+1了,现在你好象很少上网了
sharklee 2001-11-22
  • 打赏
  • 举报
回复
有没有人能给出一个行之有效的方法!!!

33,025

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧