ÄÚÈÝ·¢²¼¸üÐÂʱ¼ä : 2025/1/9 4:58:39ÐÇÆÚÒ» ÏÂÃæÊÇÎÄÕµÄÈ«²¿ÄÚÈÝÇëÈÏÕæÔĶÁ¡£
ÒøÐмÒË㷨ʵÑ鱨¸æ
¡¾ÊµÑéÄ¿µÄ¡¿
£¨1£©¸ù¾ÝÉè¼ÆÌâÄ¿µÄÒªÇ󣬳ä·ÖµØ·ÖÎöºÍÀí½âÌâÄ¿£¬ÐðÊöϵͳµÄÒªÇó£¬Ã÷È·³ÌÐòÒªÇóʵÏֵŦÄÜÒÔ¼°ÏÞÖÆÌõ¼þ¡£
£¨2£©Ã÷°××Ô¼ºÐèÒªÓôúÂëʵÏֵŦÄÜ£¬Çå³þ±àдÿ²¿·Ö´úÂëµÄÄ¿µÄ£¬×öµ½ÓеķÅʸ£¬ÓÐÌõÀí²»ÒÅ©µÄÓôúÂëʵÏÖÒøÐмÒËã·¨¡£ ¡¾ÊµÑéÒªÇó¡¿
£¨1£©Á˽âºÍÀí½âËÀËø£»
£¨2£©Àí½âÀûÓÃÒøÐмÒËã·¨±ÜÃâËÀËøµÄÔÀí£» £¨3£©»áʹÓÃijÖÖ±à³ÌÓïÑÔ¡£ ¡¾ÊµÑéÔÀí¡¿ Ò»¡¢°²È«×´Ì¬
ָϵͳÄÜ°´ÕÕijÖÖ˳ÐòÈç
¶þ¡¢ÒøÐмÒËã·¨
¼ÙÉèÔÚ½ø³Ì²¢·¢Ö´ÐÐʱ½ø³ÌiÌá³öÇëÇójÀà×ÊÔ´k¸öºó£¬±íʾΪRequesti[j]=k¡£ÏµÍ³°´ÏÂÊö²½Öè½øÐа²È«¼ì²é£º
£¨1£©Èç¹ûRequesti¡ÜNeediÔò¼ÌÐøÒÔϼì²é£¬·ñÔòÏÔʾÐèÇóÉêÇ볬³ö×î´óÐèÇóÖµµÄ´íÎó¡£ £¨2£©Èç¹ûRequesti¡ÜAvailableÔò¼ÌÐøÒÔϼì²é£¬·ñÔòÏÔʾϵͳÎÞ×ã¹»×ÊÔ´£¬Pi×èÈûµÈ´ý¡£
£¨3£©ÏµÍ³ÊÔ̽×Å°Ñ×ÊÔ´·ÖÅä¸ø½ø³ÌPi£¬²¢ÐÞ¸ÄÏÂÃæÊý¾Ý½á¹¹ÖеÄÊýÖµ£º Available£Ûj£Ý¡Ã=Available£Ûj£Ý-Requesti[j£Ý;
Allocation£Ûi,j£Ý¡Ã=Allocation£Ûi,j£Ý+Requesti£Ûj£Ý; Need£Ûi,j£Ý¡Ã=Need£Ûi,j£Ý-Requesti£Ûj£Ý;
£¨4£©ÏµÍ³Ö´Ðа²È«ÐÔËã·¨£¬¼ì²é´Ë´Î×ÊÔ´·ÖÅäºó£¬ÏµÍ³ÊÇ·ñ´¦ÓÚ°²È«×´Ì¬¡£Èô°²È«£¬²ÅÕýʽ½«×ÊÔ´·ÖÅä¸ø½ø³ÌPi£¬ÒÔÍê³É±¾´Î·ÖÅ䣻·ñÔò£¬ ½«±¾´ÎµÄÊÔ̽·ÖÅä×÷·Ï£¬»Ö¸´ÔÀ´µÄ×ÊÔ´·ÖÅä״̬£¬Èýø³ÌPiµÈ´ý¡£ Èý¡¢°²È«ÐÔËã·¨
£¨1£©ÉèÖÃÁ½¸öÏòÁ¿£º
¢Ù ¹¤×÷ÏòÁ¿Work: Ëü±íʾϵͳ¿ÉÌṩ¸ø½ø³Ì¼ÌÐøÔËÐÐËùÐèµÄ¸÷Àà×ÊÔ´ÊýÄ¿£¬Ëüº¬ÓÐm¸öÔªËØ£¬ÔÚÖ´Ðа²È«Ëã·¨¿ªÊ¼Ê±£¬Work¡Ã=Available;
¢Ú Finish: Ëü±íʾϵͳÊÇ·ñÓÐ×ã¹»µÄ×ÊÔ´·ÖÅä¸ø½ø³Ì£¬Ê¹Ö®ÔËÐÐÍê³É¡£¿ªÊ¼Ê±ÏÈ×öFinish£Ûi£Ý¡Ã=false; µ±ÓÐ×ã¹»×ÊÔ´·ÖÅä¸ø½ø³Ìʱ£¬ ÔÙÁîFinish£Ûi£Ý¡Ã=true¡£
£¨2£©´Ó½ø³Ì¼¯ºÏÖÐÕÒµ½Ò»¸öÄÜÂú×ãÏÂÊöÌõ¼þµÄ½ø³Ì£º ¢Ù Finish£Ûi£Ý=false;
¢Ú Need£Ûi,j£Ý¡ÜWork£Ûj£Ý£» ÈôÕÒµ½£¬ Ö´Ðв½Öè(3)£¬ ·ñÔò£¬Ö´Ðв½Öè(4)¡£ £¨3£©µ±½ø³ÌPi»ñµÃ×ÊÔ´ºó£¬¿É˳ÀûÖ´ÐУ¬Ö±ÖÁÍê³É£¬²¢Êͷųö·ÖÅä¸øËüµÄ×Ê
1
Ô´£¬¹ÊÓ¦Ö´ÐУº
? Work£Ûj£Ý¡Ã=Work£Ûi£Ý+Allocation£Ûi,j£Ý; ? Finish£Ûi£Ý¡Ã=true; ? go to step 2;
£¨4£©Èç¹ûËùÓнø³ÌµÄFinish£Ûi£Ý=true¶¼Âú×㣬 Ôò±íʾϵͳ´¦ÓÚ°²È«×´Ì¬£»·ñÔò£¬ÏµÍ³´¦ÓÚ²»°²È«×´Ì¬¡£
¡¾ÊµÑé²½Öè¡¿
²Î¿¼ÊµÑé²½ÖèÈçÏ£º
£¨1£©²Î¿¼Í¼1-1ËùʾÁ÷³Ìͼ±àд°²È«ÐÔËã·¨¡£ ¿ªÊ¼ ³õʼ»¯WorkºÍFinish N ´æÔÚFinish[i] =false &&Need[i][j]<= Available[j] Y Finish[i]=true£¬Work[j]=Work[j]+ Allocation[i][j] N ËùÓнø³Ì¶¼ÕÒÍêÁË£¿ Y N ËùÓÐfinish¶¼Îªtrue£¿ Y Êä³öϵͳ²»°²È« Êä³ö°²È«ÐòÁРͼ1-1 °²È«ÐÔËã·¨Á÷³Ìͼ
2
£¨2£©ÒøÐмÒËã·¨Á÷³Ìͼ ¿ªÊ¼ ÊäÈë³õʼ²ÎÊý£¨×Ê Ô´·ÖÅä¼°ÇëÇóÇé ³ö´í·µ Y Requesti[j]> Need[i][j] »Ø£º N ³ö´í·µ»Ø£º(½ø³Ì×èY Requesti[j]> Available[j] Èû) N ¼Ù¶¨·ÖÅ䣺 Available[j] = Available[j] ¨C Requesti[j] Allocation[i][j]= Allocation[i][j] + Requesti[j] Need[i][j] = Need[i][j] ¨C Requesti[j] ÊÇ ·ñ ¼Ù¶¨·ÖÅäÖ®ºó£¬ÏµÍ³°² È«Â𣿠ÉêÇë³É¹¦¡£Êä³ö¸÷ÖÖÉêÇëʧ°Ü¡£ Êý¾ÝµÄ±ä»¯ ÒÔÉÏ·ÖÅä×÷·Ï£¬»Ö¸´ÔÀ´µÄ·ÖÅä״̬£º Available[j] = Available[j] + Requesti[j] Allocation[i][j]= Allocation[i][j]£Requesti[j] Need[i][j] = Need[i][j]+Requesti[j] ½áÊø ͼ1-2ÒøÐмÒËã·¨Á÷³Ìͼ
£¨3£©±àдͳһµÄÊä³ö¸ñʽ¡£
ÿ´ÎÌá³öÉêÇëÖ®ºóÊä³öÉêÇë³É¹¦Óë·ñµÄ½á¹û¡£Èç¹û³É¹¦»¹ÐèÒªÊä³ö±ä»¯Ç°ºóµÄ¸÷ÖÖÊý¾Ý£¬²¢ÇÒÊä³ö°²È«ÐòÁС£
3
£¨4£©²Î¿¼Í¼1-2ËùʾÁ÷³Ìͼ±àдÒøÐмÒËã·¨¡£ £¨5£©±àдÖ÷º¯ÊýÀ´Ñ»·µ÷ÓÃÒøÐмÒËã·¨¡£ ¡¾²Î¿¼´úÂë¡¿
²¿·Ö²Î¿¼´úÂëÈçÏ£º #include
#define M 3 //×ÊÔ´µÄÖÖÀàÊý #define N 5 //½ø³ÌµÄ¸öÊý void output(int iMax[N][M],int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N]); //ͳһµÄÊä³ö¸ñʽ
bool safety(int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N]);
bool banker(int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N]); void main() {
int i,j;
//µ±Ç°¿ÉÓÃÿÀà×ÊÔ´µÄ×ÊÔ´Êý int iAvailable[M]={3,3,2};
//ϵͳÖÐN¸ö½ø³ÌÖеÄÿһ¸ö½ø³Ì¶ÔMÀà×ÊÔ´µÄ×î´óÐèÇó int iMax[N][M]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}}; //iNeed[N][M]ÿһ¸ö½ø³ÌÉÐÐèµÄ¸÷Àà×ÊÔ´Êý
//iAllocation[N][M]ΪϵͳÖÐÿһÀà×ÊÔ´µ±Ç°ÒÑ·ÖÅä¸øÿһ½ø³ÌµÄ×ÊÔ´Êý int iNeed[N][M],iAllocation[N][M]={{0,1,1},{2,0,0},{3,0,2},{2,1,1},{0,0,2}}; //½ø³ÌÃû
char cName[N]={'a','b','c','d','e'}; bool bExitFlag=true; //Í˳ö±ê¼Ç char ch; //½ÓÊÕÑ¡ÔñÊÇ·ñ¼ÌÐøÌá³öÉêÇëʱ´«½øÀ´µÄÖµ bool bSafe; //´æ·Å°²È«Óë·ñµÄ±êÖ¾ //¼ÆËãiNeed[N][M]µÄÖµ for(i=0;i output(iMax,iAllocation,iNeed,iAvailable,cName); //Åжϵ±Ç°×´Ì¬ÊÇ·ñ°²È« bSafe=safety(iAllocation,iNeed,iAvailable,cName); //ÊÇ·ñ¼ÌÐøÌá³öÉêÇë while(bExitFlag) { cout<<\¼ÌÐøÌá³öÉêÇ룿\\nyΪÊÇ£»nΪ·ñ¡£\\n\ cin>>ch; switch(ch) { 4 case 'y': //cout<<\µ÷ÓÃÒøÐмÒËã·¨\ bSafe=banker(iAllocation,iNeed,iAvailable,cName); if (bSafe) //°²È«£¬ÔòÊä³ö±ä»¯ºóµÄÊý¾Ý output(iMax,iAllocation,iNeed,iAvailable,cName); break; case 'n': cout<<\Í˳ö¡£\\n\ bExitFlag=false; break; default: cout<<\ÊäÈëÓÐÎó£¬ÇëÖØÐÂÊäÈ룺\\n\ } } } //Êä³ö void output(int iMax[N][M],int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N]) { int i,j; cout<<\ Max \\tAllocation\\t Need \\t Available\ cout<<\ B C\\tA B C\\tA B C\\t A B C\ for(i=0;i //°²È«ÐÔËã·¨£¬½øÐа²È«ÐÔ¼ì²é£»°²È«·µ»Øtrue£¬²¢ÇÒÊä³ö°²È«ÐòÁУ¬²»°²È«·µ 5