#includestdio.h#defineMAXPROCESS50/*×î´ó½ø³ÌÊý*/#defineMAXRESOURCE100/*×î´ó×ÊÔ´Êý*/#definetrue1#definefalse0intAVAILABLE[MAXRESOURCE];/*¿ÉÓÃ×ÊÔ´Êý×é*/intMAX[MAXPROCESS][MAXRESOURCE];/*×î´óÐèÇó¾ØÕó*/intALLOCATION[MAXPROCESS][MAXRESOURCE];/*·ÖÅä¾ØÕó*/intNEED[MAXPROCESS][MAXRESOURCE];/*ÐèÇó¾ØÕó*/intREQUEST[MAXPROCESS][MAXRESOURCE];/*½ø³ÌÐèÒª×ÊÔ´Êý*/intFINISH[MAXPROCESS];/*ϵͳÊÇ·ñÓÐ×ã¹»µÄ×ÊÔ´·ÖÅä*/intp[MAXPROCESS];/*¼Ç¼ÐòÁÐ*/intm,n;/*m¸ö½ø³Ì,n¸ö×ÊÔ´*/voidInit();intSafe();voidBank();voidmain(){Init();Safe();Bank();}/*×î´ó×ÊÔ´Êý*/voidInit()/*³õʼ»¯Ëã·¨*/{inti,j;printf(ÇëÊäÈë½ø³ÌµÄÊýÄ¿:\n);scanf(%d,&m);printf(ÇëÊäÈë×ÊÔ´µÄÖÖÀà:\n);scanf(%d,&n);printf(ÇëÊäÈëÿ¸ö½ø³Ì×î¶àËùÐèµÄ¸÷×ÊÔ´Êý,°´ÕÕ%dx%d¾ØÕóÊäÈë\n,m,n);for(i=0;im;i++)for(j=0;jn;j++)scanf(%d,&MAX[i][j]);printf(ÇëÊäÈëÿ¸ö½ø³ÌÒÑ·ÖÅäµÄ¸÷×ÊÔ´Êý,Ò²°´ÕÕ%dx%d¾ØÕóÊäÈë\n,m,n);for(i=0;im;i++){for(j=0;jn;j++){scanf(%d,&ALLOCATION[i][j]);NEED[i][j]=MAX[i][j]-ALLOCATION[i][j];if(NEED[i][j]0){printf(ÄúÊäÈëµÄµÚ%d¸ö½ø³ÌËùÓµÓеĵÚ%d¸ö×ÊÔ´Êý´íÎó,ÇëÖØÐÂÊäÈë:\n,i+1,j+1);j--;continue;}}}printf(ÇëÊäÈë¸÷¸ö×ÊÔ´ÏÖÓеÄÊýÄ¿:\n);for(i=0;in;i++){scanf(%d,&AVAILABLE[i]);}}intSafe()/*°²È«ÐÔËã·¨£¬·µ»ØÖµ£ºtrue,false*/{inti,j,k,l=0;intWork[MAXRESOURCE];/*¹¤×÷Êý×é*/for(i=0;in;i++)Work[i]=AVAILABLE[i];for(i=0;im;i++){FINISH[i]=false;}for(i=0;im;i++){if(FINISH[i]==true){continue;}else{for(j=0;jn;j++){if(NEED[i][j]Work[j]){break;}}if(j==n){FINISH[i]=true;for(k=0;kn;k++){Work[k]+=ALLOCATION[i][k];}p[l++]=i;i=-1;}else{continue;}}if(l==m){//coutϵͳÊÇ°²È«µÄendl;printf(ϵͳÊÇ°²È«µÄ\n);//cout°²È«ÐòÁÐ:endl;printf(°²È«ÐòÁÐ:\n);for(i=0;il;i++){//coutp[i];printf(%d,p[i]);if(i!=l-1){printf(--);}}returntrue;}}printf(ϵͳÊDz»°²È«µÄ\n);returnfalse;}voidBank()/*ÒøÐмÒËã·¨*/{inti,cusneed;charagain;while(1){printf(ÇëÊäÈëÒªÉêÇë×ÊÔ´µÄ½ø³ÌºÅ(×¢:µÚ1¸ö½ø³ÌºÅΪ0,ÒÀ´ÎÀàÍÆ)\n);scanf(%d,&cusneed);printf(ÇëÊäÈë½ø³ÌËùÇëÇóµÄ¸÷×ÊÔ´µÄÊýÁ¿\n);for(i=0;in;i++){scanf(%d,&REQUEST[cusneed][i]);}for(i=0;in;i++){if(REQUEST[cusneed][i]NEED[cusneed][i]){printf(ÄúÊäÈëµÄÇëÇóÊý³¬¹ý½ø³ÌµÄÐèÇóÁ¿!ÇëÖØÐÂÊäÈë!\n);continue;}if(REQUEST[cusneed][i]AVAILABLE[i]){printf(ÄúÊäÈëµÄÇëÇóÊý³¬¹ýϵͳÓеÄ×ÊÔ´Êý!ÇëÖØÐÂÊäÈë!\n);continue;}}for(i=0;in;i++){AVAILABLE[i]-=REQUEST[cusneed][i];ALLOCATION[cusneed][i]+=REQUEST[cusneed][i];NEED[cusneed][i]-=REQUEST[cusneed][i];}if(Safe()){printf(ͬÒâ·ÖÅäÇëÇó!\n);}else{printf(ÄúµÄÇëÇ󱻾ܾø!\n);for(i=0;in;i++){AVAILABLE[i]+=REQUEST[cusneed][i];ALLOCATION[cusneed][i]-=REQUEST[cusneed][i];NEED[cusneed][i]+=REQUEST[cusneed][i];}}for(i=0;im;i++){FINISH[i]=false;}printf(Äú»¹ÏëÔÙ´ÎÇëÇó·ÖÅäÂð?ÊÇÇë°´y/Y,·ñÇë°´ÆäËü¼ü\n);scanf(%c,&again);if(again=='y'||again=='Y'){continue;}break;}}