📄 subject_61090.htm
字号:
<p>
序号:61090 发表者:独来毒网 发表日期:2003-11-17 18:06:46
<br>主题:大家帮忙做道题.有分
<br>内容:有A个对.每个对B个人.<BR>每个房间最多能站C个人<BR><BR>要求 所有人能进房间,而且每个对的人要在一个房间<BR><BR>求 要几个房间能把这些人装满.<BR><BR>并说明哪几个对在哪个房间..<BR><BR>如1,2,3,4,5对分别有1,4,5,10,5,2人<BR>每个房间最多站10人. 则要3个房间.房间1装3对,<BR>房间2装1对和2对. 房间3装装4对和5对...<BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:xiongli 回复日期:2003-11-17 19:09:59
<br>内容:np
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:smallbus 回复日期:2003-11-24 16:10:28
<br>内容:以下是我编的程序,<BR>按照你留的参数测试了一下。<BR>(结果稍有不同,但是完全符合题意,好像比你给的结果效果还要好些)<BR>运行时先输入队数,再输入每个房间可容纳的人数,<BR>然后陆续输入每队的人数,<BR>最后输出结果。<BR>排序和链表的插入算法写的效率不太好,你可以重新写一下。<BR>#include <stdio.h><BR>#include <stdlib.h><BR><BR>struct queuenode {<BR> int amount;<BR> int num;<BR>}a[100];<BR>struct ans {<BR> int qnum;<BR> int amount;<BR> struct ans *next;<BR>}answer[100];<BR>void sort(struct queuenode s[],int m) {<BR> int i,j,t;<BR> struct queuenode temp;<BR> for(i=0;i<m;i++) {<BR> temp=s[i],t=i;<BR> for(j=i+1;j<m;j++) {<BR> if(s[j].amount>s[i].amount) {<BR> s[i]=s[j];<BR> t=j;<BR> }<BR> }<BR> s[t]=temp;<BR> }<BR>}<BR><BR>void insert(int queuenum,int amount,int i)<BR>{<BR> struct ans *p,*q=&answer[i-1];<BR> p=malloc(sizeof(struct ans));<BR> p->qnum=queuenum;<BR> p->amount=amount;<BR> p->next=NULL;<BR><BR> p->next=q->next;<BR> q->next=p;<BR> printf(" %d,%d,%d\n",i,q->next->qnum,q->next->amount);<BR>}<BR><BR>int distribute(struct queuenode s[],int m,int n)<BR>{<BR> int i,j,temp,rooms;<BR> i=rooms=temp=0;<BR> while(i<n) {<BR> if(s[i].amount!=-1) {<BR> temp=s[i].amount;<BR> insert(s[i].num,s[i].amount,++rooms);<BR> s[i].amount=-1;<BR> j=i+1;<BR> }<BR> else {<BR> i++;<BR> continue;<BR> }<BR> while(j<n) {<BR> if(s[j].amount!=-1&&(temp+s[j].amount)<=m) {<BR> temp+=s[j].amount;<BR> insert(s[j].num,s[j].amount,rooms);<BR> s[j].amount=-1;<BR> }<BR> j++;<BR> }<BR> i++;<BR> }<BR> return rooms;<BR>}<BR><BR>void del(struct ans *a) {<BR> struct ans *p;<BR> while(a->next!=NULL) {<BR> p=a;<BR> a=a->next;<BR> free(p);<BR> }<BR> free(a);<BR>}<BR><BR>void delall(struct ans *s,int times)<BR>{<BR> int i;<BR> for(i=0;i<times;i++)<BR> del(s+i);<BR>}<BR><BR>void disp (int m)<BR>{<BR> int i;<BR> struct ans *p;<BR> printf("\nyou need %d rooms\n",m);<BR> for(i=0;i<m;i++) {<BR> p=answer[i].next;<BR> printf("\nroom %d contain\n------------------------------------\n",m);<BR> do{<BR> printf("%d queue,amount: %d\n",p->qnum,p->amount);<BR> p=p->next;<BR> } while(p!=NULL);<BR> }<BR>}<BR><BR>void main () {<BR> int i,queue,room,roomnum;<BR> clrscr();<BR> printf("input the number of queue\n");<BR> scanf("%d",&queue);<BR> printf("how many people can live in the room:");<BR> scanf("%d",&room);<BR> for(i=0;i<queue;i++) {<BR> printf("how many people in this queue:");<BR> scanf("%d",&a[i].amount);<BR> answer[i].next=NULL;<BR> a[i].num=i+1;<BR> }<BR> sort(a,queue);<BR> roomnum=distribute(a,room,queue);<BR> disp(roomnum);<BR> delall(answer,roomnum);<BR> getch();<BR>}
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -