#include<stdio.h>
#include<stdlib.h>
#define SUM 6
int state[SUM];
//1代表还书 -1代表借书
//
void book(int book_lend,int book_return,int num,int count)
{
//算法结束的条件是:借书的人都借到了,即book_lend==0
if (book_lend == 0)
{
for (int i = 0; i < SUM; i++)
{
printf("%d * ",state[i]);
}
printf("\n");
printf("\b\b\b\b ");
}
//还书的人都还完了,并且还有书的时候,只能借书
if (book_return == 0 && count>0)
{
state[num] = -1;
book(book_lend - 1, book_return, num + 1, count-1);
}
//可借的条件是图书馆有书,可还的条件是有的人没有还
if (count>0&&book_return>0)
{
state[num] = -1;
book(book_lend - 1, book_return, num + 1, count-1);
state[num] = 1;
book(book_lend, book_return - 1, num + 1, count+1);
}
//图书馆的书的总数为0,并且有人没还书,只能还书
if (book_return > 0&&count==0)
{
state[num] = 1;
book(book_lend, book_return - 1, num + 1, 1+count);
}
}
int main(void)
{
book(3,3,0,0);
return 0;
}
图书馆里有没有某本书,现有6人排队,有3个人来借此书,3个人来还此书,且若到某人借书时,若无书,则立即离开,多少种排队方法能借到书?
最新推荐文章于 2021-08-13 18:07:18 发布